UHampton
asked on
Help with Powershell Script to email managers about expired user accounts
Having issues with the script, seems to be failing with the email code. The objective is to send managers a list of their consultants who have expired user accounts in AD.
We based this from the example ps script from the link in the first comment.
The error we are getting is:
Any ideas?
We based this from the example ps script from the link in the first comment.
# https://social.technet.microsoft.com/Forums/windowsserver/en-US/9d080c24-b2a2-4d9b-b50b-ca7fb9d95a91/account-expiration-email-notification?forum=winserverpowershell
# Find all managers and check each manager's employees for expired accounts in Consultants OU
Import-Module ActiveDirectory
$style = "<style>BODY{font-family: Arial; font-size: 10pt;}"
$style = $style + "TABLE{border: 1px solid black; border-collapse: collapse;}"
$style = $style + "TH{border: 1px solid black; background: #dddddd; padding: 5px; }"
$style = $style + "TD{border: 1px solid black; padding: 5px; }"
$style = $style + "</style>"
Get-ADUser -Filter * -Properties directReports,EmailAddress -SearchBase "OU=Consultants,OU=User Accounts,DC=Microsoft,DC=com" | ForEach {
$body = @()
If ($_.directReports) {
$managerEmailAddress = $_.EmailAddress
$_.directReports | ForEach {
$userDetails = Get-ADUser $_ -Properties AccountExpirationDate
If ( $userDetails.AccountExpirationDate ) {
If ( [datetime]($userDetails.AccountExpirationDate) -lt (Get-Date) ) {
$sendEmail = $true
$body += New-Object PsObject -Property @{
Username = $userDetails.SamAccountName
AccountExpirationDate = $userDetails.AccountExpirationDate
}
}
}
}
}
If ($sendEmail) {
$EmailMessage = $body | ConvertTo-HTML -head $style
#$EmailMessage += "<br>
#<p>This is a notification that your consultant(s) account is now expired.<br><br>
#For security reasons, please forward this email to Microsoft Customer Care at 7878@microsoft.com or 203-123-1234 option 2 to either extend the account or to disable it.<br>
#<br>
#Thank you,<br>
#Microsoft Customer Care</p>
#<br>"
Send-MailMessage -From 'AccountExpiry@Daymon.Com' -To "eric.thornton@secure-24.com" -Subject 'Consultant Account Expiration Report' -Body $EmailMessage -BodyAsHTML -Priority high -SmtpServer 'mail-relay.microsoft.net'
}
$sendEmail = $false
}
The error we are getting is:
Send-MailMessage : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Body'. Specified
method is not supported.
At C:\scripts\WeeklyEmailManagersExpiredConsultantAccounts.ps1:59 char:139
+ Send-MailMessage -From 'AccountExpiry@Microsoft.Com' -To $managerEmailAddress -Subject 'Consultant Account
Expiration Report' -Body <<<< $EmailMessage -BodyAsHTML -Priority high -SmtpServer 'mail-relay.Microsoft.net'
+ CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage
Any ideas?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nevermind, I got it. That was it, it works now. Thanks for your help!
ASKER
Provided the solution to the question about the script. Thanks!
ASKER
AccountExpirationDate Username
8/2/2017 12:00:00 AM TestExpiredScript
So the HTML email table looks nice!
Just need the email message and after that show the table with expired account(s).
Thanks