我有一个自动脚本,用于将用户从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
}
答案 0 :(得分:0)
您可能需要按如下方式构建代码。而不是在顶部一次定义你的身体,当它从上到下解析时,你将它放在你的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
}
答案 1 :(得分:0)
您的问题是 和 您声明变量
$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
的正文,并添加HomePage
和AccountPassword
,方法与尝试$googusr
和$googpwd
的方式相同。 Send-MailMessage
保持不变。
请注意格式运算符
由于您的$body
字符串中已经有大括号,因此需要对其进行转义。在你看到{
的地方,它们会加倍到{{
,而相反的支撑也是如此。我这样做是为了让ForEach循环清洁。