Forum Discussion

dmarquesgn's avatar
dmarquesgn
Iron Contributor
Mar 22, 2022

Email layout for powershell script

Hi,

 

I'm creating my first powershell scripts, and a basic one I've created is just to check if in a time period (for example, last 7 days, any AD user was created with the "PasswordNeverExpires" set to true, which is something we need to control. 

Then I send out this information by email for analysis, but the email layout is not the best one. Screenshot attached.

So is there any quick way to have an email friendly layout so it's easier to read?
This is my code:

 

 

$When = (Get-Date).Date.AddDays(-30)
$Alerts = Get-ADUser -Filter {whenCreated -ge $When} -Properties whenCreated, PasswordNeverExpires | Select-Object samaccountname, Name, Enabled, PasswordNeverExpires
Write-Host $Alerts

$messagebody = @"
    These users have been created in the last 30 days:
    $($Alerts | Out-String)
"@
Send-MailMessage -SmtpServer smtp.office365.com -Port 587 -UseSsl -From Email address removed -To Email address removed -Subject 'Alert: Users < 30 dias & PasswordNEverExpires' -Body $messagebody -Credential $mycredentials

 

Thanks

  • dmarquesgn Made a HTML table from it, looks like this on my testserver:

    (Used the code from https://techcommunity.microsoft.com/t5/core-infrastructure-and-security/script-to-send-email-alerts-on-expiring-certificates-for/ba-p/1169438 as example)

     

    Script:

     

     

    $When = (Get-Date).Date.AddDays(-30)
    $Alerts = Get-ADUser -Filter { whenCreated -ge $When } -Properties whenCreated, PasswordNeverExpires | Select-Object samaccountname, Name, Enabled, PasswordNeverExpires
    $style = @'
    <style>body{font-family:`"Calibri`",`"sans-serif`"; font-size: 14px;}
    @font-face
           {font-family:`"Cambria Math`";
           panose-1:2 4 5 3 5 4 6 3 2 4;}
    @font-face
           {font-family:Calibri;
           panose-1:2 15 5 2 2 2 4 3 2 4;}
    @font-face
           {font-family:Tahoma;
           panose-1:2 11 6 4 3 5 4 4 2 4;}
           table{border: 1px solid black; border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;}
           th{border: 1px solid black; background: #dddddd; padding: 5px; }
           td{border: 1px solid black; padding: 5px; }
           .crtsn{font-weight: bold; color: blue; }
           .crtexp{font-weight: bold; color: red; }
           .crtcn{font-weight: bold; color: orange; }
           </style>
    '@
    $mailbody += '<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8">' + $style + '</head><body>'
    $mailbody += '<p>'
    $mailbody += 'These users have been created in the last 30 days ' + "<br />"
    $mailbody += '</p>'
    $mailbody += '<p><table>'
    $mailbody += '<th>samaccountname</th><th>Name</th><th>Enabled</th><th>PasswordNeverExpires</th>'
    
    foreach ($user in $Alerts) {
        $mailbody += "<tr><td>" + $user.samaccountname + "</td><td>" + $user.Name + "</td><td>" + $user.Enabled + "</td><td>" + $user.PasswordNeverExpires + "</td></tr>"
    }
    
    $mailbody += '</table></p>'
    $mailbody += '</body>'
    $mailbody += '</html>'
    
    Send-MailMessage -SmtpServer mail.server.com -Port 587 -UseSsl -From mailfromaddress -To mailtoaddress -Subject 'Álert: Users < 30 dias & PasswordNEverExpires' -Body $mailbody -BodyAsHtml -Credential $mycredentials

     

     

     

  • dmarquesgn Made a HTML table from it, looks like this on my testserver:

    (Used the code from https://techcommunity.microsoft.com/t5/core-infrastructure-and-security/script-to-send-email-alerts-on-expiring-certificates-for/ba-p/1169438 as example)

     

    Script:

     

     

    $When = (Get-Date).Date.AddDays(-30)
    $Alerts = Get-ADUser -Filter { whenCreated -ge $When } -Properties whenCreated, PasswordNeverExpires | Select-Object samaccountname, Name, Enabled, PasswordNeverExpires
    $style = @'
    <style>body{font-family:`"Calibri`",`"sans-serif`"; font-size: 14px;}
    @font-face
           {font-family:`"Cambria Math`";
           panose-1:2 4 5 3 5 4 6 3 2 4;}
    @font-face
           {font-family:Calibri;
           panose-1:2 15 5 2 2 2 4 3 2 4;}
    @font-face
           {font-family:Tahoma;
           panose-1:2 11 6 4 3 5 4 4 2 4;}
           table{border: 1px solid black; border-collapse:collapse; mso-table-lspace:0pt; mso-table-rspace:0pt;}
           th{border: 1px solid black; background: #dddddd; padding: 5px; }
           td{border: 1px solid black; padding: 5px; }
           .crtsn{font-weight: bold; color: blue; }
           .crtexp{font-weight: bold; color: red; }
           .crtcn{font-weight: bold; color: orange; }
           </style>
    '@
    $mailbody += '<html><head><meta http-equiv=Content-Type content="text/html; charset=utf-8">' + $style + '</head><body>'
    $mailbody += '<p>'
    $mailbody += 'These users have been created in the last 30 days ' + "<br />"
    $mailbody += '</p>'
    $mailbody += '<p><table>'
    $mailbody += '<th>samaccountname</th><th>Name</th><th>Enabled</th><th>PasswordNeverExpires</th>'
    
    foreach ($user in $Alerts) {
        $mailbody += "<tr><td>" + $user.samaccountname + "</td><td>" + $user.Name + "</td><td>" + $user.Enabled + "</td><td>" + $user.PasswordNeverExpires + "</td></tr>"
    }
    
    $mailbody += '</table></p>'
    $mailbody += '</body>'
    $mailbody += '</html>'
    
    Send-MailMessage -SmtpServer mail.server.com -Port 587 -UseSsl -From mailfromaddress -To mailtoaddress -Subject 'Álert: Users < 30 dias & PasswordNEverExpires' -Body $mailbody -BodyAsHtml -Credential $mycredentials

     

     

     

    • dmarquesgn's avatar
      dmarquesgn
      Iron Contributor
      Hi,

      Thanks, that's exactly what I've been looking for.
      Now I can use the code in other alerts as well.

      Thanks
      • dmarquesgn's avatar
        dmarquesgn
        Iron Contributor
        One thing I've just remembered.
        Can I insert a signature of our area that we're using in all emails that we send to the users?
  • Newbie_Jones's avatar
    Newbie_Jones
    Brass Contributor

    Just an observation, but am I missing something here?
    The requirement from the original post is to check if any users were created without password expiries set in the last 30 days.

    Sounds like a sensible governance check.

    You should be able to do this from a single filter, and only return the users that the attribute is not set. Instead of all users that you currently have in the report. Extend the filter with an AND to include PasswordNeverExpires='true'.

    You should be able to use a select command to trim the attributes for the object if needed and just export to CSV, call the CSV PasswordNeverExpires.csv and then email that. A CSV would have the same headers as you have in the HTML report.  The CSV name hopefully would explain the context of the file.

    I'm just looking at this with my "keep it simple" head on and got a bit confused 🙂

    • Harm_Veenstra's avatar
      Harm_Veenstra
      MVP
      'So is there any quick way to have an email friendly layout so it's easier to read?' that's what I answered, he wanted a report in the mail body and not an attachment. That's how I read it, but I understand what you mean
      • dmarquesgn's avatar
        dmarquesgn
        Iron Contributor
        Yes, the main goal is achieved, but as this information in the end is to be read by some helpdesk operators, so everything that will be similar to what they are used to it's much better, due to internal cultural issues. But main issue is achieved.

Resources