Hey All
I wrote a script (probably since Im fresh with powershell its dirty scripting :( ) to email users when their password is about to expire.
Get-ADUser -filter * -properties PasswordLastSet,EmailAddress,GivenName -SearchBase "OU=Users,DC=domain,DC=test" |foreach { $PasswordSetDate=$_.PasswordLastSet $maxPasswordAgeTimeSpan = $null $maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge $today=get-date $ExpiryDate=$passwordSetDate + $maxPasswordAgeTimeSpan $daysleft=$ExpiryDate-$today $display=$daysleft.days $UserName=$_.GivenName $MyVariable = @"" Dear $UserName Your password will expire in $display days.If you are not going to change it you will not be able to connect to Irdeto corporate network. If you are in Irdeto West HQ building follow the steps below to change your password: - Press CTRL+ALT+DEL - On the screen that came choose "Change password" - Type in your old password and then type the new one (be advised you cannot use one of the previously used passwords) - After the change is complete you will be prompted with information that passwor has been changed If you are using VPN: - Connect your laptop to internet - On the logon screen to windows connect to CISCO VPN prior to login - When you will receive successfull connection information login - At this moment you will be prompted to change your password With kind Regards IT Department *** This is automatically generated email - please do not reply *** ""@ send-mailmessage -to $_.EmailAddress -from NoReply@domain.test -Subject "IT Information: Your password will expire in $display days" -body $MyVariable -smtpserver msg001 }
So basically what it does - it queries domain for users and their selected attributes and then perform action to check when exactly the password will expire.
The thing which I do not know how to put in here would be how to send email only to users which password will expire 7 days or less (greater than 0 of course cause lesss would mean already expired)
If I would use IF would that be sufficient ?
MCSA / MCTS