如何在 powershell 中将变量嵌入 HTML?
How do you embed a variable into HTML within powershell?
我有一个自动脚本,可以将用户从 csv 文件导入到 Active Directory 中。我想为每个使用用户名和密码创建的用户发送一封电子邮件给新用户管理员以进行入职。它当前正在为每个用户发送电子邮件,但未嵌入用户名和密码。
$newadstaff = Import-CSV "newad-staff.csv"
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$googusr = $_.'HomePage'
$googpwd = $_.'AccountPassword'
$Body = @"
<style>
.colorchange {color:#4F81BD;}
ind {text-indent:50px;}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: $googusr </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: $googpwd </b>
</body>
</html>
"@
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = $body
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | export- csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
Send-MailMessage @param -BodyAsHtml
}
您可能需要按如下方式构建代码。当它从上到下解析时,不是在顶部定义一次你的正文,而是将它放在你的 foreach 循环中,为每封邮件消息创建一个唯一的版本。在执行 AD 用户查询转储后,您还需要拥有 CSV 导入变量。 (实际上,您不需要将其保存为 CSV 并重新导入它,因为如果您只是简单地执行 $newadstaff = Get-AdUser ....
抱歉,我根本没有对此进行测试,但它看起来是正确的。
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = $body
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | Export-Csv "newad-staff.csv" -NoTypeInformation
$newadstaff = Import-Csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
$Body = @"
<style>
.colorchange {color:#4F81BD;}
ind {text-indent:50px;}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: $($user.HomePage) </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: $($user.AccountPassword) </b>
</body>
</html>
"@
Send-MailMessage @param -BodyAsHtml
}
您的问题是如何和在哪里声明变量
$googusr = $_.'HomePage'
$googpwd = $_.'AccountPassword'
$_
应该表示管道中的当前项目。在你赋值时 $_
不代表任何东西。您正在将 $null
分配给这两个值。
您的第一行用于 CSV 导入 $newadstaff = Import-CSV "newad-staff.csv"
。这看起来很奇怪,因为您后来用上一小时创建的新用户覆盖了该文件。从理论上讲,这没有错,只是看起来很奇怪。我在下面的代码中单独留下了那个订单。
新代码
$newadstaff = Import-CSV "newad-staff.csv"
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$Body = @"
<style>
.colorchange {{color:#4F81BD;}}
ind {{text-indent:50px;}}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: {0} </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: {1} </b>
</body>
</html>
"@
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = ""
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | export-csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
# The body of each mail is different. Set this body for the current $user
$param.Body = $body -f $user.HomePage,$user.AccountPassword
Send-MailMessage @param -BodyAsHtml
}
你永远不会用每个用户的新信息来改变正文。为了使这更容易,我用一些 -Format
运算符占位符 {0}
和 {1}
更新了此处的字符串。现在在循环中,我们更新 $param
的主体并添加 HomePage
和 AccountPassword
,就像您尝试使用 $googusr
和 $googpwd
一样。 Send-MailMessage
保持不变。
格式运算符注意事项
因为你的 $body
字符串中已经有花括号,所以需要将它们转义。在你看到 {
的地方,它们加倍到 {{
并且对面的括号也是如此。我这样做是为了让 ForEach 循环更干净。
我有一个自动脚本,可以将用户从 csv 文件导入到 Active Directory 中。我想为每个使用用户名和密码创建的用户发送一封电子邮件给新用户管理员以进行入职。它当前正在为每个用户发送电子邮件,但未嵌入用户名和密码。
$newadstaff = Import-CSV "newad-staff.csv"
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$googusr = $_.'HomePage'
$googpwd = $_.'AccountPassword'
$Body = @"
<style>
.colorchange {color:#4F81BD;}
ind {text-indent:50px;}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: $googusr </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: $googpwd </b>
</body>
</html>
"@
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = $body
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | export- csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
Send-MailMessage @param -BodyAsHtml
}
您可能需要按如下方式构建代码。当它从上到下解析时,不是在顶部定义一次你的正文,而是将它放在你的 foreach 循环中,为每封邮件消息创建一个唯一的版本。在执行 AD 用户查询转储后,您还需要拥有 CSV 导入变量。 (实际上,您不需要将其保存为 CSV 并重新导入它,因为如果您只是简单地执行 $newadstaff = Get-AdUser ....
抱歉,我根本没有对此进行测试,但它看起来是正确的。
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = $body
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | Export-Csv "newad-staff.csv" -NoTypeInformation
$newadstaff = Import-Csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
$Body = @"
<style>
.colorchange {color:#4F81BD;}
ind {text-indent:50px;}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: $($user.HomePage) </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: $($user.AccountPassword) </b>
</body>
</html>
"@
Send-MailMessage @param -BodyAsHtml
}
您的问题是如何和在哪里声明变量
$googusr = $_.'HomePage'
$googpwd = $_.'AccountPassword'
$_
应该表示管道中的当前项目。在你赋值时 $_
不代表任何东西。您正在将 $null
分配给这两个值。
您的第一行用于 CSV 导入 $newadstaff = Import-CSV "newad-staff.csv"
。这看起来很奇怪,因为您后来用上一小时创建的新用户覆盖了该文件。从理论上讲,这没有错,只是看起来很奇怪。我在下面的代码中单独留下了那个订单。
新代码
$newadstaff = Import-CSV "newad-staff.csv"
$mailpwd = ConvertTo-SecureString 'helpdeskpassword' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential 'helpdesk@google.com',$mailpwd
$Body = @"
<style>
.colorchange {{color:#4F81BD;}}
ind {{text-indent:50px;}}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div style="background-color:#3059D6; color:white; padding:20px;">
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:40pt"; align=center><b>NEW STAFF USER ACCOUNT</b></p>
</div>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:20pt">A new Google account has been created for: </p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt">Please navigate to <a href="https://google.com/accounts">Google Account Login</a> and login with the below credentials. You will be prompted to change your password after logging in:</p>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Username: {0} </b>
<p style="font-family:Tahoma, Geneva, sans-serif; font-size:12pt"><b>Password: {1} </b>
</body>
</html>
"@
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = $cred
From = 'helpdesk@google.com'
To = 'admin@google.com'
Subject = 'New User Account'
Body = ""
}
$When = ((Get-Date).Addhours(-1)).Date
Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated | export-csv "newad-staff.csv"
Foreach ($user in $newadstaff)
{
# The body of each mail is different. Set this body for the current $user
$param.Body = $body -f $user.HomePage,$user.AccountPassword
Send-MailMessage @param -BodyAsHtml
}
你永远不会用每个用户的新信息来改变正文。为了使这更容易,我用一些 -Format
运算符占位符 {0}
和 {1}
更新了此处的字符串。现在在循环中,我们更新 $param
的主体并添加 HomePage
和 AccountPassword
,就像您尝试使用 $googusr
和 $googpwd
一样。 Send-MailMessage
保持不变。
格式运算符注意事项
因为你的 $body
字符串中已经有花括号,所以需要将它们转义。在你看到 {
的地方,它们加倍到 {{
并且对面的括号也是如此。我这样做是为了让 ForEach 循环更干净。