variable outupt in email formatting

This topic contains 8 replies, has 4 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 2 years, 5 months ago.

  • Author
    Posts
  • #16403
    Profile photo of Jeremy Anderson
    Jeremy Anderson
    Participant
    $contractemp = Get-ADUser -Filter { manager -eq $listmanager} -SearchBase "OU=MIS Users,OU=Users,OU=Accounts,OU=ON24,DC=on24,DC=com" -Properties Manager | select-object -ExpandProperty name
    $body ="
        Dear $managername,
         Our records show that the following are active contractors that report to you:
        
        $contractemp 
        
    
    Send-Mailmessage -smtpServer $smtpServer -from $from -to $recipient -subject $subject -body $body -bodyasHTML 
    

    All this works as expected but the out put of $contractemp looks bad. How do I format with commas between the names, or even better in a list?

    Example of the current output.

    [blockquote]Dear Jeremy Anderson,
    Our records show that the following are active contractors that report to you:

    Neil Fallon Bill Manspeaker
    [/blockquote]

    What I would like it to look like...

    [blockquote]Dear Jeremy Anderson,
    Our records show that the following are active contractors that report to you:

    Neil Fallon
    Bill Manspeaker

    [/blockquote]

  • #16406
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    You would do a join and add a carriage return at the end of the names:

    $managerName = "Manager Dude"
    $users = Get-ADUser -Filter * | Select -ExpandProperty Name -First 5
    
    $body = @"
    
    Dear $managername,
    
    Our records show that the following are active contractors that report to you:
     
    $($users -join "`r`n") 
    
    "@
    
    $body
    
    
  • #16437
    Profile photo of Jeremy Anderson
    Jeremy Anderson
    Participant

    I cant quite get this figured out. All the changes I make results in the same output.

        Dear Jeremy Anderson,
        Our records show that the following are active contractors that report to you:
    
        Neil Fallon Bill Manspeaker

    I can not get the names to list like this

    Neil Fallon 
    Bill Manspeaker]
    

    For what its worth, it outputs fine on screen, just not in the email.
    Here is my code:

    
    $listmanagers = Get-ADUser -Filter * -SearchBase ",OU=Accounts,OU=Contoso,DC=Contoso,DC=com" -Properties Manager| where-object { $_.manager -ne $null} | %{(Get-AdUser $_.Manager)} | select-object -Unique
    ForEach ($listmanager in $listmanagers)
    {
    
    
    $contractemp = Get-ADUser -Filter { manager -eq $listmanager} -SearchBase "OU=Accounts,OU=Contoso,DC=contoso,DC=com" -Properties Manager | Select -ExpandProperty name | Out-String
    $managername = get-aduser $listmanager | select-object -ExpandProperty name
    
    $subject="Contractor DataBase Reminder"   
    
    $body = 
    @"
        Dear $managername,
         Our records show that the following are active contractors that report to you:
        
        $($contractemp -join "`r`n")
        
        Please respond to this email stating whether or not the above users will continue to be employed after the end of this month.
        
        Thanks, IT 
        
    "@
        
    
     Send-Mailmessage -smtpServer $smtpServer -from $from -to $recipient -subject $subject -body $body -bodyasHTML 
    
    
    

    Thanks for all your help on these, I have learned a ton trying to put this together.

  • #16438
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    Try taking off the -bodyasHTML switch and see if that corrects the behavior.

  • #16440
    Profile photo of H Man
    H Man
    Participant

    your missing the foreach loop closing }

  • #16441
    Profile photo of H Man
    H Man
    Participant

    your missing the foreach loop closing }

  • #16453
    Profile photo of Jeremy Anderson
    Jeremy Anderson
    Participant

    Boom, that is the trick. Removing the BodyAsHTML.

    Of course, now I need to remove the HTML tags, but thats a minor issue.

    Is there some required reading on "body as html" that might help me in the future?

    Hman – thanks, I have the } in my script, it just did not make the copy and paste.

    Thanks for all the help everyone.

  • #16458
    Profile photo of Simon Wåhlin
    Simon Wåhlin
    Participant

    If you want to keep using HTML, try joining the names like Rob suggests but using the HTML-tag for newline instead.
    Like this:

    $($users -join '{br /}')
    

    EDIT: The {br /}-tag was removed from my post so I realized it didn"t make much sense, hope it works better now. It is supposed to be instead of { and }

  • #16479
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    HTML is another whole beast. It gives more flexibility with colors, tables, images, etc., but it's somewhat more complex. Personally, I get a pre-designed mail template that is test across most mail platforms and use that as a template for my email communications. Powershell does have ConvertTo-HTML, which has some limitations on styling. Don Jones wrote an e-Book and some functions to combat some of these items (for HTML Reporting) , but it's really about just putting the time in to test the formatting. There is a lot of reading on HTML Emails and the basic premise is to keep things simple. If it's a internal email, in the past I've kept images on a file share that was shared with everyone and pointed them there. My company went to a cloud-based email and then email could be accessed on laptops, home computers, tablets, etc., which makes things more difficult from a CSS styling perspective and images need to be embedded, which Send-MailMessage cannot do by default and Dave W. created a function for embedding functionality. As I said, HTML email can be complex and send you down a rabbit hole. Anywho, trying to keep things simple, so here is a basic setup for HTML. The script is sending it to a HTML file, so I make changes and then hit F5 on the browser to see the changes until it's right. Then you have to send it to the mail client to see how it renders. Have fun!!

    $managerName = "Manager Dude"
    $users = Get-ADUser -Filter * | Select Name -First 5 
    $css=@"
        {style}
            BODY{background-color:AliceBlue;}
            TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
            TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:black;color:white}
            TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:lightgray}
        {/style}
    "@
    
    
    $splat =@{
     Property= "Name"
     Title=   "Bug Managers"
     Pre=     "Dear $managername,Our records show that the following are active contractors that report to you:{br /}{br /}"
     Post =   "{br}For details, contact Corporate IT."
     Head =   $css
    }
    
    $body = $users | ConvertTo-Html @splat
    $body > C:\Users\Jeremyt\Desktop\test.html

    Edit: Gah!! Editor stole the {style} tags in the CSS example and {br /}. Change it to the standard format when testing...

You must be logged in to reply to this topic.