【发布时间】: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