Greetings,
I am trying to find out what is the best way get this script working. This script is looking into accounts and notify users if their account will be expiring in 30 days and again alert them at 45 days before it is deleted.
I have to pull users from multiple locations: certain AD Groups and certain OU’s. Then it will determine their time period and email them.
Here is the script:
[pre]
############################################################################################################ # $smtpServer="smtp.server.coml" $from = "Administrator <noreply@server.com>" $DaysInactive = 45 $Inactive = @() $Groups = "Group1"; "Group2"; "Group3"; $OU = "OU=Standard Users,OU=Users,OU=OUNAME,OU=OUName,OU=OUName,DC=Name,DC=Name,DC=com" # ###################################################################################################################
#-- Get todays's date and go back how many day's inactive --#$time = (Get-Date).AddDays(-($DaysInactive))
#-- Get list of users in AD Groups --#
ForEach ($Group in $Groups) {
$users = Get-ADGroupMember -Identity $Group -Recursive | Where objectclass -eq ‘user’
}
#-- Get list of users in AD specific OU --#
ForEach ($OUS in $OU) {
$usersfromou = Get-ADUser -SearchBase $OUS -Recursive | Where objectclass -eq ‘user’ | Out-GridView
}
$combined = $users + $usersfromou
$combined
#-- From the AD Group Users, we are pulling certain information --#
ForEach ($user in $combined) {
$Inactive += Get-ADUser -Identity $user -Properties LastLogonDate,Displayname,Enabled,EmailAddress | Where-Object { ($_.LastLogonDate -lt $time) } | Select-Object -Property DistinguishedName,samAccountName,Name,Displayname,Enabled,EmailAddress,LastLogonDate
}
$Inactive
#-- Set the Variables for Each AD Group User --#
ForEach ($user in $Inactive)
{
$Name = $user.Name
$emailaddresses = $user.EmailAddress
$LastLogonTime = $user.LastLogonDate.DateTime
#-- Email Subject Set Here --#
$subject = “Your account will expire soon”
#-- Email Body Set Here --#
$body ="
Dear $name,
<p> Your account will expire $messageDays<br>
To update your account, please complete the following steps:<br>
<p>1. Go to the following location: website <br>
2. At the bottom, you will see a icon called Account Login Validation, please click on this icon. <br>
If your account is disabled, please submit a ticket to enable your account.
<p>We have the following information:<br>
Email: $emailaddresses<br>
Last Date of Login: $LastLogonTime
<p>Thanks, <br>
Support Team
</P>"
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddresses -subject $subject -body $body -bodyasHTML -priority High
}
[/pre]
A couple of questions:
- How can combine the $users and $usersfromou and make sure their is no duplicates? I tried $combined = $users + $usersfromou | select -uniq but that did not work.
- How can I add notification say for 5 days prior, 4 days prior and so forth?
- How can I separate the 30 days between the 45 days? For example, I want user to get an alert at day 27 saying that they account will be expiring but at day 44 say, it will be deleted.
Thanks for any help that is offered.