2

我的 PowerShell V3 根据一系列问题创建一个 Office 365 用户,然后向 IT 团队发送一封包含详细信息的电子邮件。有问题的一点是:

#Generate-RandomPassword
$xpassword = Create-ComplexPassword -PasswordLength 14 -LowerAlphas 1 -UpperAlphas 1 -Numbers 1 -SpecialCharacters 1
Add-Type -AssemblyName System.Web
[System.Web.HttpUtility]::HtmlEncode($xpassword)


Write-Host 'Sending Email' -foregroundcolor yellow
$xbody =  "<head>"
$xbody += "<style>"
$xbody += "p{"
$xbody += "font-family:Verdana;"
$xbody += "font-size:10pt;"
$xbody += "margin-top:0;"
$xbody += "margin-bottom:0;"
$xbody += "}"
$xbody += "</style>"
$xbody += "</head>"
$xbody += "<p>Password: <span style=`"font-family:'Lucida Console',sans serif;font-size:10pt;color:red`">$xpassword</span>"
$xbody += "<p>License: $xk1ore1"
$xbody += "<p>Country: $xcountry"
$xbody += "<p>Town: $xtown"
$xbody += "<p>Status: $xstatus"
$xbody += "<p>Laptop User: $xlaptopuser"
$xbody += "<p>CRM User: $xcrm"
$xbody += "<p>Operations Mailbox Access: $xoperations"
$xbody += "<p>Finance Mailbox Access: $xfinance"
$xbody += "<p>IT Support Mailbox Access: $xitsupport"
$xbody += "<p>Hide from GAL: $xhidefromgal"

Send-O365MailMessage -To itsupport@blah.com -Subject "O365 Account Created" -Body "$xbody" -BodyAsHtml -Credential $cred

变量 $xpassword 可能包含需要转义的字符,也可能不需要。例如,当密码包含“<”时,我在电子邮件中将其作为一行:

Password: 4*#<(OGyq<="" span="">

我该如何满足这一点?

4

1 回答 1

5

使用 .net 库:

[System.Web.HttpUtility]::HtmlEncode()

::HtmlDecode读取编码字符串。

编辑:我可以看到你粘贴了你的脚本。并且有一个错误。方法 HtmlEncode() 返回 System.String,所以改变这个:

[System.Web.HttpUtility]::HtmlEncode($xpassword)

对此:

$xpassword=[System.Web.HttpUtility]::HtmlEncode($xpassword)

否则,您的脚本将只打印编码字符串,并且 $xpassword 保持不变。

于 2013-09-02T13:38:58.613 回答