Reputation: 341
My PowerShell V3 creates an Office 365 user based on a series of questions, and then sends an email to the IT team with the details. The problematic bit is:
#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 [email protected] -Subject "O365 Account Created" -Body "$xbody" -BodyAsHtml -Credential $cred
The variable $xpassword may include characters that need escaping, and may not. When the password contains a '<' for example, I get this as one line in my email:
Password: 4*#<(OGyq<="" span="">
How can I cater for this?
Upvotes: 2
Views: 2619
Reputation: 13141
Use .net library:
[System.Web.HttpUtility]::HtmlEncode()
and ::HtmlDecode
to read encoded strings.
Edit: I can see that you pasted your script. And there's an error. The method HtmlEncode() return System.String, so change this:
[System.Web.HttpUtility]::HtmlEncode($xpassword)
To this:
$xpassword=[System.Web.HttpUtility]::HtmlEncode($xpassword)
Otherwise your script will only print the encoded string, and $xpassword stays the same.
Upvotes: 5