Reputation: 503
So after a-lot of help, there is a script that will search users that were disabled for the past 14 days + show their managers name + email and date when they were disabled:
$ou = "my-ou"
$date = (Get-Date).AddDays(-14)
$todaydate = Get-Date -DisplayHint Date
$disabledAccounts = Get-aduser -filter {Enabled -eq $false -and Modified -ge $date } -SearchBase $ou -Properties Modified,manager | select samaccountname,Modified,manager
#$ManagerName = ''
$Body = ”
<html>
<body>
<p>Dear $ManagerName,<br>
The user $userName has been disabled on $todaydate .<br
</body>
</html>”
ForEach($disabledAccount in $disabledAccounts){
$manager = get-aduser -property emailaddress,DisplayName $disabledAccount.manager
$ManagerName= $manager.Displayname
$userName = $disabledAccount.samaccountname
Send-MailMessage -To $manager.UserPrincipalName -From ‘[email protected]’ -Subject ‘Disabled account’ -Body $Body -SmtpServer ‘mysmtp’ -BodyAsHtml -Priority High
}
The problem for now that this script only sent email for 1 user when in my test i have 2 disabled users in this period of time. And the first command with "Get-Aduser" i see both of the users, and in the second one "Get-aduser" i see only 1.
Upvotes: 0
Views: 758
Reputation: 52
I answered something like this on Spice works
$ou = “my_ou”
$date = (Get-Date).AddDays(-14)
$disabledAccounts = Get-aduser -filter {Enabled -eq $false -and Modified -ge $date } -SearchBase $ou -Properties Modified,manager | select samaccountname,Modified,manager
$ManagerName = ''
$Body = ”
<html>
<body>
<p>Dear $ManagerName,<br>
The user $userName has been disabled on .<br
</body>
</html>”
ForEach($disabledAccount in $disabledAccounts){
$manager = get-aduser -property emailaddress,DisplayName $disabledAccount.manager
$ManagerName= $manager.Displayname
$userName = $disabledAccount.samaccountname
Send-MailMessage -To $manager.ManagerEmail -From ‘myemail@’ -Subject ‘Disabled account’ -Body $Body -SmtpServer ‘mysmtp server’ -BodyAsHtml -Priority High
}
Upvotes: 0
Reputation: 52
$ou = "my ou direction"
$date = (Get-Date).AddDays(-14)
$diabledAccounts = Get-ADUser -Filter {Enabled -eq $false -and Modified -ge $date } -SearchBase $ou -Properties Modified,manager | select samaccountname,Modified,@{n='Manager';e={(Get-ADUser $.manager).name}},@{n='ManagerEmail';e={(Get-ADUser $.manager -properties mail).mail}}
ForEach($diabledAccount in $diabledAccounts){
Send-MailMessage -To $diabledAccount.ManagerEmail -Subject 'Disabled account' -Body "Account $diabledAccount.samaccountname has been disabled in the last 14 days" -SmtpServer ''
}
Upvotes: 1