Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import-module ActiveDirectory
- $emailFrom = 'someone@examples.com'
- $smtpServer = 'smtp.example.com.com'
- $adSearchContainer = 'domain.local/Users/*'
- $subject = 'your subject'
- $body = 'your body'
- $smtp = new-object Net.Mail.SmtpClient($smtpServer)
- $statusReport = "Password expiration notices sent today:`n`n"
- $users = Get-ADUser -Filter * -Properties emailaddress,'msDS-UserPasswordExpiryTimeComputed',canonicalname | where-object {$_.canonicalname -like $adSearchContainer} | Select-Object name,samaccountname,emailaddress,@{Name='ExpiresOn';Expression={[datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')}},@{Name='DaysLeft';Expression={([datetime]::FromFileTime($_.'msDS-UserPasswordExpiryTimeComputed')-(Get-Date)).Days}} | where-object {$_.DaysLeft -and $_.DaysLeft -ge 0}
- foreach ($u in $users)
- {
- $body = "";
- $subject = "";
- if ($u.DaysLeft -eq 0)
- {
- $subject = 'Your network password will expire in less than 24 hours'
- $body = @"
- From the Helpdesk-
- Your password will expire in less than 24 hours on $($u.ExpiresOn). Please change your password immediately.
- "@
- }
- elseif ($u.DaysLeft -le 1)
- {
- $subject = 'Your network password will expire in less than 48 hours'
- $body = @"
- From the Helpdesk-
- Your password will expire on $($u.ExpiresOn). Please change your password before it expires.
- "@
- }
- elseif ($u.DaysLeft -le 6)
- {
- $subject = "Your network password will expire in less than $($u.DaysLeft) days"
- $body = @"
- From the Helpdesk-
- Your password will expire on $($u.ExpiresOn). Please change your password before it expires.
- "@
- }
- elseif ($u.DaysLeft -eq 14)
- {
- $subject = 'Your network password will expire in approximately 14 days'
- $body = @"
- From the Helpdesk-
- Your password will expire on $($u.ExpiresOn). If you will be out of the office between now and then, please change your password before you leave to avoid having your password expire while you are out.
- "@
- }
- if ($subject -ne "")
- {
- $body += @"
- To change your password while you are on a computer in the office, press Control Alt Delete and click the Change Password option.
- To change your password while you are logged in to Remote Desktop, open the Start Menu and pick Windows Security from the lower right corner of the menu (right above the Log Off button). Then click Change Password.
- Remember: if you get your email on your phone or tablet, or you use Remote Desktop on an iPad or a Mac, you will need to tell your device your new network password.
- If you need help, please call the Helpdesk at x800.
- "@
- $smtp.Send($emailFrom, $u.emailaddress, $subject, $body)
- $statusReport += "$($u.emailaddress) expires on $($u.ExpiresOn) in $($u.DaysLeft) days`n"
- Write-Output "$($u.emailaddress) expires on $($u.ExpiresOn) in $($u.DaysLeft) days"
- }
- }
- $statusReport += "`n`nThis report was generated on "
- $statusReport += gc env:computername
- $smtp.Send($emailFrom, $emailFrom, 'Password Expiration Status Report', $statusReport)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement