I am pulling data from our MSSQL HR database and running a through a foreach loop and comparing various fields in the rowset with the AD attributes for the user. This works fine. In this loop I'm also appending to a string as I go along recording what I am doing to each user, and also recording an error message too. For example:
foreach ($user in $users) {
# Try and find the AD User
$ADUser = $(try {Get-ADUser $user.SamAccountName -Properties Office,OfficePhone,MobilePhone,Department,Title,City,PostalCode,StreetAddress} catch {$null})
#Check if we found it
if ($ADuser -ne $null) {
$ADUpdated = $false
#We did so check the HR data too before we try and use it
if (-Not ([string]::IsNullOrEmpty($user.Title))) {
if (($user.Title -ne $ADUser.Title)) { #Check if we need to update
# Update the Job Title attribute
Write-Verbose ("Updating Job Title to " + $user.Title + " for " + $user.SamAccountName + " (" + $user.DisplayName + ")")
Set-ADUser $user.SamAccountName -Title $user.Title
$messagebody += "Updating Job Title to " + $user.Title + " for " + $user.SamAccountName + " (" + $user.DisplayName + ")" + " `r`n"
$ADtitleUpdated++
$ADUpdated = $true
}
} else {
#HR Database is wrong
$HRnoTitle++
$messagebody += "Missing Job Title in HR Record for " + $user.DisplayName + " `r`n"
Write-Warning ("Missing Job Title in HR Record for " + $user.DisplayName)
if ($ADUpdated) {$ADrecordsUpdated++}
}
else {
#User is missing in AD but Exists in HR Database
$ADmissing++
Write-Warning ($user.SamAccountName + " (" + $user.DisplayName + ")" + " exists in HR Database but doesn't in AD!")
$messagebody += $user.SamAccountName + " (" + $user.DisplayName + ")" + " exists in HR Database but doesn't in AD!`r`n"
}
So the $messagebody
has a multi line string telling me if there's an issue with user records in the HR DB or if any AD accounts are absent. When I print this out with Write-Host
I get the right information. However when I send this via Send-MailMessage
the CRLFs are missing. What can I do to preserve the CRLFs? The mail client is Outlook 2010.