我正在向我的用户创建中添加一个新的 powershell 部分,即向他们的主管发送电子邮件,告知他们已创建。但是,出于某种原因,它将凭据发送给列表中的下一位主管,而不是该行中的主管,并且列出的第一位主管正在获取标题作为信息。CSV 的构造如下:
1.Lastn | Firstn | 程序 | 密码。| 管理员
2.Duck | Daffy | ........ 998 | ChangeMe | Bob.Dole
3.Bunny| Buggs|.........878| ChangeMe | Elmer.Fudd
脚本如下:
$Users = Import-Csv "C:\PSScripts\Create\users.csv" -header("lastname","firstname","program","password","supervisor")
foreach ($User in $Users)
{
$OU = "OU=users,DC=Contoso,DC=local"
$UserFirstname = $User.Firstname
$UserLastname = $User.LastName
$Supervisor = $User.Supervisor
$subject = "New Account Creation Completed"
$body = "Good Morning! This e-mail is to alert you that your new staff person $SAM has been granted Contoso.org e-mail access. To log in to outlook web application, their username is $SAM and their temporary password is ChangeMe . They will be prompted to change it upon successful login."
$smtp = "10.10.1.79"
$SAM = $UserFirstname + "." + $UserLastname
Send-MailMessage -to [email protected] -Subject $subject -body $body -SmtpServer $smtp -from [email protected]
}
但是,主管“Bob.Dole”会收到“Buggs.Bunny”电子邮件凭证,主管 Elmer.Fudd 会收到“Firstname.Lastname”电子邮件,这就是 $Sam 的构造方式。因此,凭证将发送给下一行的主管,而最后一位主管会收到“firstname.lastname”电子邮件,而不是发送给个人。此外,我收到了无法送达的电子邮件,称电子邮件帐户[电子邮件保护]是无效的,所以它似乎误解了某些东西,尽管它在执行命令后会发送给 $supervisor。如果我在“Supervisor”标题下插入一个空白单元格,那么主管列就会向下移动一个,它会向我发出一个错误,必须指定收件人,但一旦向下移动,它就会将凭据发送给正确的主管。
答案1
问题是$SAM
您在对 的分配中引用它之后又将其分配给$body
。
$Users = Import-Csv "C:\PSScripts\Create\users.csv" -header("lastname","firstname","program","password","supervisor")
foreach ($User in $Users)
{
$OU = "OU=users,DC=Contoso,DC=local"
$UserFirstname = $User.Firstname
$UserLastname = $User.LastName
$Supervisor = $User.Supervisor
$subject = "New Account Creation Completed"
#moved before $body= .... $SAM ...
$SAM = $UserFirstname + "." + $UserLastname
$body = "Good Morning! This e-mail is to alert you that your new staff person $SAM has been granted Contoso.org e-mail access. To log in to outlook web application, their username is $SAM and their temporary password is ChangeMe . They will be prompted to change it upon successful login."
$smtp = "10.10.1.79"
Send-MailMessage -to [email protected] -Subject $subject -body $body -SmtpServer $smtp -from [email protected]
}