0

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.

AeroX
  • 3,387
  • 2
  • 25
  • 39
  • not that you have to but did you declare `$messagebody` anywhere we cant see? At the end of the script what is the result of this `$messagebody.GetType().FullName`? I wonder if it is a string array as supposed to a string. – Matt Jan 16 '15 at 12:49
  • @GeoffKing You don't have the `Send-MailMessage` line in the code you gave us. Also you appear to be missing the closure curly brace (`}`) in a couple of places in that sample – AeroX Jan 16 '15 at 12:57
  • Ok here's the extra bits of code from my script @Matt `$messagebody = @" AD Import From HR Database Results "@` and `Send-MailMessage -From $smtpFrom -To $smtpTo -Subject $messageSubject -Body $messagebody -SmtpServer $smtpServer` The code tags missed a }. The code does work. I'm just not showing it all. – Geoff King Jan 16 '15 at 13:04
  • @matt $messagebody.GetType().FullName comes back as `System.String` – Geoff King Jan 16 '15 at 13:18
  • possible duplicate of [MailMessage sent string as body without newline in outlook](http://stackoverflow.com/questions/3931409/mailmessage-sent-string-as-body-without-newline-in-outlook). Sort of explains the issue and a few good ways to fix it. Not a powershell problem. – Matt Jan 16 '15 at 13:38
  • @Matt Thanks, that looks like the problem. I'll try the full stop thing then give up and send HTML if I really have to. – Geoff King Jan 16 '15 at 13:47

1 Answers1

0

@Matt is correct. As per MailMessage sent string as body without newline in outlook I just needed to add some punctuation to my output. I changed the " `r`n" to ". `r`n" and ensure all the lines in my Here-Strings ended in punctuation.

Community
  • 1
  • 1