【问题标题】:How do you embed a variable into HTML within powershell?如何在 powershell 中将变量嵌入到 HTML 中?
【发布时间】:2015-06-06 16:40:35
【问题描述】:

我有一个自动脚本,它将用户从 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
}

【问题讨论】:

  • 您的用户和传递变量的值就像您从管道输出定义它一样。不确定这是否是您的整个脚本,但我可能会为每个用户和密码变量write-host,如果它们出现在您的主体变量末尾抛出一个write-host,以确保其创建的文本流包含您的值预计。您导入的 CSV 是否为单个帐户条目?如果是这样,那么您的用户和通行证的格式可能应该是$newadstaff.AccountPassword 格式。
  • csv 是过去一小时内添加到 AD 的所有用户的列表。我是脚本/编程的新手。我仍在学习 powershell,所以如果您能提供示例并用伟大的 Michael Scott 的话“像我 5 岁一样向我解释它”。 :)
  • 为您创建了一个带有简短描述的答案版本。祝你好运

标签: html powershell csv active-directory powershell-4.0


【解决方案1】:

你的问题是如何在哪里你声明变量

$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} 更新了here-string。现在在循环中,我们更新$param 的主体并添加HomePageAccountPassword,就像您尝试使用$googusr$googpwd 一样。 Send-MailMessage 保持不变。

格式运算符注意事项

由于您的$body 字符串中已经有花括号,因此需要对其进行转义。在您看到{ 的地方,它们会翻倍到{{,对面的大括号也是如此。我这样做是为了让 ForEach 循环更干净。

【讨论】:

  • Matt,当我尝试这个时,我收到以下错误:格式化字符串时出错:输入字符串的格式不正确。在 line:39 char:5 + $param.Body = $body -f $user.HomePage,$user.AccountPassword + CategoryInfo : InvalidOperation: (
  • 知道了...更新即将到来。 @Mike 用答案中的整个代码再试一次。
  • 以上工作:) 谢谢!我想看看另一个例子是否也可以工作。
【解决方案2】:

您可能需要按如下方式构建代码。与其在顶部定义你的正文一次,当它从上到下解析时得到一次处理,你将它放在你的 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
    }

【讨论】:

  • 好的,这解决了错误,但是在我的测试文件中,我有 3 行用户,这是发送 3 封电子邮件,但所有 3 行都有第一个用户信息。 var 需要清空吗?
  • 第一个猜测是@param 可能不会随着$body 的更改而更新
  • 是的,我猜马特也是这么想的,我可以在这里设置一个测试,然后用上面的修改版本来响应。
  • 哈哈,没看到马特的解决方案,脑中死了。很高兴它的工作。
猜你喜欢
  • 2015-04-20
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2015-10-02
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多