您看过 Powershell 的“此处字符串”功能吗?有一个讨论该功能的 technet 文章。我一直将它们用于作为东西模板的字符串。
我喜欢在此类模板中使用 C# 风格的占位符,例如 {0}。这允许对日期和货币进行精美的格式化。 (我在示例中使用了“奇特”日期格式。)
使用带有占位符的模板还意味着我不必记住以特定顺序将字符串连接在一起,也不必记住像 $firstname 这样的东西必须放在这些连接中的位置。它也应该更容易国际化,但我从未这样做过。
这是一个快速示例,您需要将其集成到循环逻辑中。
# first, stick the template into a variable for later use. Use a "here string" for ease of formatting and editting.
$bodyTemplate = @"
{0} {1}
<html><body><br> Your network password will expire in {2} day(s).</body><html>
<html><body><br>Employees of Organization, when you receive this email please visit https://scriptlogic/iisadmpwd/aexp2b.asp to reset your network password.
<br>If you are <font color =""#99000"">not employed by Organization</font>, please visit https://gateway.organization.org to reset your network password using our Citrix website.
<br>If you need assistance resetting your password, please contact the Ibformation Service Department at 867-5309
<br>If you have a portable device, smart phone, etc. that you use to access the Network the new password will need to be updated on these devices also.
<br><br>Thank you,
<br> IS Department
<br><img src='P:\Documents\PowerShell\Scripts\password\logo.jpg' alt='logo'/>
<br><br><hr>
From <b> IS Department</b>
<br>The information contained in this e-mail and any accompanying documents is confidential, may be privileged, and is intended solely for the person and/or entity to whom it is
<br>addressed (i.e. those identified in the <b> To: </b> and <b> cc:</b> box). They are the property of this organization. Unauthorized review, use, disclosure, or copying of this
communication, or any part thereof, is strictly prohibited and may be unlawful. The IT Department thanks you for your cooperation.<br>
{3:D}
<br><hr><br></body></html>
"@
# Now, loop through your users, calculate $DaysUntilExpiry, test the value and build the email to be sent
# I'm just making up some dumb values here
$daystoexpire = 42 # or whatever
$firstname = "George"
$lastname = "Washington"
$date = date
# using the template, stick the appropriate values into place and store that in a variable for convenience
$body = $bodyTemplate -f $firstname, $lastname, $daystoexpire, $date
# do whatever we want with $body
write-host $body