有谁知道如何解决此问题吗?我正在研究如何在 Active Directory (AD) 中自动创建新用户并通知用户。我编写了一个 PS 脚本,用于在 PowerShell 中从 CSV 文件导入和创建新用户,并且我希望使用 PS Send-MailMessage 选项将“用户 ID”和密码传达给每个用户。
下面的脚本可以很好地处理这个问题,但是格式看起来不太好,正如您在附件中看到的那样。
实际上,有两个问题我正在尝试解决。
$SupportEmail = "[email protected]"
$To = "$EmailAddress"
$Cc = " [email protected]""
$Attachment = "C:\Accountsetup.pdf"
$subject = "Welcome - Account Setup Credentials
$SMTPServer = "smtp.gmail.com"
$SMTPPort ="587"
$body = "Hello $DisplayName,"
$body += "This e-mail contains your user name and first-time password for signing
into our web service"
$body += "Acount UserName: [email protected]"
$body += "Temporary Password: $Password"
$body += "Please see the attached userguide on how to reset your password using your
temporary assigned password "
$Creds = (Get-Credential -Credential "$SupportEmail")
Start-Sleep 2
Send-MailMessage -From $SupportEmail -to $To -cc $Cc -Attachment
$Attachment -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl `
-Credential $Creds -DeliveryNotificationOption never
答案1
我发现这个帖子在 Stackoverflow 上将凭据传递给 Send-MailMessage
Nick 于 2012 年 9 月 17 日 16:01 回答
我喜欢为事物创建功能,并且我需要尽可能多的练习,所以我继续写下了以下内容:
Function Send-EMail {
Param (
[Parameter(`
Mandatory=$true)]
[String]$EmailTo,
[Parameter(`
Mandatory=$true)]
[String]$Subject,
[Parameter(`
Mandatory=$true)]
[String]$Body,
[Parameter(`
Mandatory=$true)]
[String]$EmailFrom="[email protected]", #This gives a default value to the $EmailFrom command
[Parameter(`
mandatory=$false)]
[String]$attachment,
[Parameter(`
mandatory=$true)]
[String]$Password
)
$SMTPServer = "smtp.gmail.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
if ($attachment -ne $null) {
$SMTPattachment = New-Object System.Net.Mail.Attachment($attachment)
$SMTPMessage.Attachments.Add($SMTPattachment)
}
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom.Split("@")[0], $Password);
$SMTPClient.Send($SMTPMessage)
Remove-Variable -Name SMTPClient
Remove-Variable -Name Password
} #End Function Send-EMail
要调用它,只需使用此命令:
Send-EMail -EmailTo "[email protected]" -Body "Test Body" -Subject "Test Subject" -attachment "C:\cdf.pdf" -password "Passowrd"
此外,对于格式的第二部分,请将<br />
标签放在行尾,而不是开始新行。
当您发送第二个 $body 属性时,您使用的格式不会发送新行,并且您需要<br />
在末尾特别声明以便在电子邮件中出现换行符,因为它使用 HTML 格式。
如果你使用我发布的函数,它将位于 -Body 标签之后
-Body "Hello $DisplayName, This e-mail contains your user name and first-time password for signing
into our web service <br /> Acount UserName: [email protected] <br /> Temporary Password: $Password <br /> Please see the attached userguide on how to reset your password using your
temporary assigned password "