I'm trying to send a single mail on the body containing all the users that are soon to expire that match the Manager, the problem that i'm having is tha the script is sending to single emails for every user that have the same Manager, my boss said to me that this is no possible to be implemented like that. Could you please help me out on this script on how to send the body html email like this;
Dear $ Manager,
you have the following soon to expire users;
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
User $Name $Email $ExpirationDays
$Body
Here is the actual script i found on line and edited to my needs,
import-module ActiveDirectory;
Get-ADUser -Filter * -Properties directReports,EmailAddress,Displayname | ForEach {
$ManagerName=$_.Displayname
If ( $_.directReports ) {
$managerEmailAddress = $_.EmailAddress
$_.directReports | ForEach {
$userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires
$userName=$userdetails.Name
If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {
$sendEmail = $false
}
If ( $userDetails.AccountExpirationDate ) {
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate1=$ExpiryDate.ToShortDateString()
$today=(Get-Date)
$DaysLeft=($ExpiryDate-$today).days
$Date = Get-Date -format "yyyy-MM-dd_HH-mm-ss"
$UserList = Get-ADUser -Filter {sAMAccountName -Like "*_ext" -and enabled -eq $true} -SearchBase 'DC=example,DC=local' -properties AccountExpirationDate,manager | `Where-Object{$_.AccountExpirationDate -lt (Get-Date).AddDays(7) -and $_.AccountExpirationDate -gt (Get-Date).AddDays(1) -and $_.AccountExpirationDate -ne $null} |Select Name,UserPrincipalName,AccountExpirationDate,@{Name="ManagerEmail";Expression={(get-aduser -property emailaddress $_.manager).emailaddress}}
$ReportName = "C:\Users\myusername\Desktop\ExpiringUserReports\Expiring_Users_$Date.xlsx"
$UserList | Export-Excel $ReportName -FreezeTopRow -AutoFilter -AutoSize -BoldTopRow
$Body = "
<html>
<body>
<p>Dear $ManagerName,<br>
The user account <strong>$userName</strong>, will expire in <strong>$DaysLeft days</strong>.<br>
You are being notified because our records show that you are the primary contact Manager for this user.<br>
Please take action to extend the validity before expiration.<br />
Download the following <a href='https://examplespa-my.mysite.com/:x:/g/personal/myyser_example_com/file.xslx'> Consultants Template </a> file and send back to us with updated expiration date PO.<br>
Having any issues, don't hesitate to <font color=104160 size=5> ✉</font> us.</p>
<p>Regards<br />
<img src='https://www.example.it/example/logo.jpg' ><br>
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help Desk
Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'><o:p></o:p></span><br />
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>example
</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Via V.
</span><span style='font-size:6.0pt;font-family:
'Lucida Grande';color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
+</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
+39 06 </span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='mailto:[email protected]'><span style='font-size:9.0pt'>[email protected]</span></a></span><span
style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'> </span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='http://www.example.com/'><span style='font-size:9.0pt'>www.example.com</span></a></span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
© ®
</body>
</html>"
If ($DaysLeft -le 2 -and $DaysLeft -ge 0) {
$sendEmail = $true
}
}
If ( $sendEmail ) {
# i am not using this right now as i don't want to spam managers while i am testing script.
send-mailmessage -To $managerEmailAddress -From [email protected] -Subject "Users Expiry notification. Action is required" -body "$Body $body1" -smtpserver "example-smtp.com" -BodyAsHtml -Attachments $ReportName
# This is i am using just to see the output
"{0,-28} {1,-20} {2,-18} {3,-8}" -f $managerEmailAddress,$userdetails.SamAccountName,$ExpiryDate1,$DaysLeft
}
$sendEmail = $false
}
}
}
Found the solution, here is the final code.
import-module ActiveDirectory;
Get-ADUser -Filter * -SearchBase 'OU=TestExpire,DC=AdlabTest,DC=local' -Properties directReports,EmailAddress,Displayname | ForEach {
$ManagerName=$_.Displayname
$Body = "
<html>
<body>
<p>Dear $ManagerName<br/>
You are being notified because our records show that you are the primary contact Manager for the below listed users.<br>
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-=collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;}
TD {border-width: 1px; padding: 1px; border-style: solid; border-color: black;}
</style>
<table>
<tbody>
<tr><th>Name</th><th>UserName</th><th>AccountExpires</th></tr>";
$AddBody = "";
If ( $_.directReports ) {
#Write-Output("Processing : " + $ManagerName);
$ToEmail = $_.EmailAddress
$_.directReports | ForEach {
$userDetails = Get-ADUser $_ -Properties AccountExpirationDate,accountExpires,EmailAddress
$userName=$userdetails.Name
$userEmail=$userdetails.userPrincipalName
If( $userDetails.accountExpires -eq 0 -or $userDetails.accountExpires -eq 9223372036854775807 ) {
$sendEmail = $false
}
If ( $userDetails.AccountExpirationDate ) {
$ExpiryDate=$userDetails.AccountExpirationDate
$ExpiryDate1=$ExpiryDate.ToShortDateString()
$today=(Get-Date)
$DaysLeft=($ExpiryDate-$today).days
If ($DaysLeft -le 40 -and $DaysLeft -ge 0) {
$AddBody += "<tr><td>$userName</td> <td><a style='text-decoration:none;color: rgb(0, 0, 0);'>$userEmail</a></td> <td>$ExpiryDate1</td> </tr>";
$sendEmail = $true
}
}
}
If ( $sendEmail ) {
$Body +=$AddBody;
$Body = $Body + "</tbody>
</table><br>
Please take action as soon as possible to extend the validity before expiration.<br />
Download the following <a href='https://link_to_file.xslx'>Template</a> file and send back to us with updated expiration date PO.<br>
Having any issues, don't hesitate to <font color=104160 size=5> ✉</font> us.</p>
<p>Regards<br />
<img src='https://www.link_to_your_site/logo.jpg' ><br>
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Help Desk
Team</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'><o:p></o:p></span><br />
<b><span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business Name</span></b><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>Your Business address </span><span style='font-size:6.0pt;font-family:
'Lucida Grande';color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>T.
+tout phone</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span><br />
<span style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'>F.
+your fax</span><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<p style='min-height:10px'><span style='font-size:6.0pt;font-family:'Lucida Grande';
color:#00578C'></span>
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='mailto:helpdesk@link_your_site.com'><span style='font-size:9.0pt'>helpdesk@link_your_site.com</span></a></span><span
style='font-size:9.0pt;font-family:'Lucida Grande';color:#00578C'> </span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br />
<span style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'><a
href='http://www.link_your_site.com/'><span style='font-size:9.0pt'>http://www.link_your_site.com</span></a></span><span
style='font-size:6.0pt;font-family:'Lucida Grande';color:#042EEE'></span><br>
© ®
</body>
</html>";
send-mailmessage -To $ToEmail -From expiry@link_your_site.comm -Subject "Users Expiry notification. Action is required" -body $Body -smtpserver "link_your_site.smtp" -BodyAsHtml -Priority High
}
}
}