Get answers from your peers along with millions of IT pros who visit Spiceworks.

Hello all,

A few days back i created a powershell topic cause i needed a script to create a csv file for each manager and the csv file needed to contain the list of direct reports to him and the list of AD group that user was in. Then send the csv file as an email attachment to the corresponding manager.



But now id like to contain not only the result as an attachment in the csv but also in the body of the email contain the result like the following.

Powershell Import-Module ActiveDirectory # Head $a = "<style>" $a = $a + "BODY{font-family:Arial;background-color:#fff;width: 100%}" $a = $a + "TABLE{font-family:Arial;width: 90%; border-collapse: collapse;background-color:#59a9ff;}" $a = $a + "TH{background-color: #59a9ff; color: white;height: 35px;}" $a = $a + "TD{display: table-cell;height: 25px;vertical-align: inherit;border-bottom: 1px solid #ddd;}" $a = $a + "P{font-family:Arial;position: absolute;}" $a = $a + "</style>" $managers = Get-ADUser -Filter * -Properties Name , DirectReports , EmailAddress | where { $_ . directreports -ne $Null } foreach ( $manager in $managers ) { $managername = $manager . Name $manageremail = $manager . UserPrincipalName $dreports = $manager . directreports $today = ( Get-Date ). ToString () $body = "<center><p>Report Date $today.</p></center>" $body += "<p><b>$managername</b>,</p> " foreach ( $dr in $dreports ) { $user = get-aduser $dr -properties * $members = $user . memberof -replace '^CN=(.+?),(?:OU|CN)=.+' , '$1' | %{ New-Object PSObject -Property @{ 'Group Membership' = $_ }} | convertTo-html -Head $a -Body "<H4>Group Membership.</H4>" $dreport = $dr -replace '^CN=(.+?),(?:OU|CN)=.+' , '$1' $body += " `n " $body += "<br />" $body += "<center>" $body += "<p>Direct Report: <u>$dreport</u></p>" $body += $members $body += "</center>" } Send-MailMessage -From from @domain . com -To $manager . UserPrincipalName -Subject "Managers - User Entitlements" -Body $body -SmtpServer smtp . server . domain . com -Port 25 -BodyAsHtml }

Q. Any idea, to combine this two and have the script send the email to each manager showing the HTML content of the result but also as an attachment.

Thank you all and any comment or help is gladly appreciate it.

My original post: https://community.spiceworks.com/topic/1984701-user-entitlements?page=1#entry-6813427





Edited May 13, 2017 at 18:56 UTC