In 2014 one of our techs released a hammer for a password expiry report. Over the last few years it has been updated several times, and as such we are releasing this updated hammer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
<# Get-ADUserPasswordExpiryReport By A Tech With a Hammer https://www.techwithahammer.com/?p=174 Created: December 2, 2014 Last Update: January 11th, 2018 To do Add in the synopsis so this utility is a little more useful and less, as it was still not added as of 2018 #> param( [string]$ReportPath = ".\PasswordExpiryReports", [string]$SearchBase = "OU=Users,DC=techwithahammer,DC=com", [string]$Age = 14, [boolean]$SendReport = $true, [string]$ReportFrom = "justanother@techwithahammer.com", [string]$ReportTo = "justanother@techwithahammer.com", [string]$ReportSubject = "Password Expiry Report", [boolean]$SendNotice = $true, [string]$NoticeFrom = "justanother@techwithahammer.com", [string]$NoticePath = ".\PasswordExpiryNotice.html", [string]$NoticeSubject = "Password Expiring", [string]$SMTPServer = "smtp.techwithahammer.com", [string]$SMTPPort = 25, [boolean]$QuoteOfTheDay = $true [string[]]$QuoteOfTheDayServers = @("djxmmx.net"), [boolean]$debugging = $false ) if (!(Test-Path $ReportPath)) { try { New-Item -ItemType Directory $ReportPath } catch { Throw "Report Path does not exist" Exit 5 } } $ReportEmailHeader = @" <style> Table {border-width: 1px; border-style: solid; border-color #000000; border-collapse: collapse;; padding: 2px; } th {border-width: 1px; border-style: solid; border-color #000000; background-color: #000000; color: #ffffff; padding: 2px; } td {border-width: 1px; border-style: solid; border-color #000000; background-color: #ffffff; padding: 2px; } </style> "@ if ($SendNotice) { if (!(Test-Path $NoticePath)) { Throw "Require valid notice template to send notices. $NoticePath does not exist" Exit 1 } $EmailNotice = (Get-Content $NoticePath) } try { Import-Module ActiveDirectory } Catch { Throw "Unable to load Active Directory module" Exit 1 } $DomainMaxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge if ($DomainMaxPasswordAge -eq $null -or $DomainMaxPasswordAge -le 0) { Throw "Domain does not have a password expiry policy" if ((get-addomain).domainmode -lt 3) { Throw "Unable to continue as domain mode is less than Windows2008Domain" Exit 4 } } $Users = ( get-aduser -searchbase "$SearchBase" -filter {Enabled -eq $true -and PasswordNeverExpires -eq $false} -properties DisplayName,PasswordLastSet,PasswordExpired,EmailAddress,GivenName,sn | where {$_.PasswordLastSet -ne $null} ) If ($Users -eq $null) { Throw "No users found" Exit 2 } else { if ($SendNotice -eq $true -and !(Test-Path $NoticePath)) { Throw "$NoticePath template required for sending notices" Exit 3 } $Results = @() ForEach ($User in $Users) { $UserAccountName = $User.sAMAccountName $UserDisplayName = $User.DisplayName $UserFirstName = $User.GivenName $UserLastName = $User.sn $UserEmailAddress = $User.EmailAddress $Today = (Get-Date -UFormat %c ) if ((get-addomain).domainmode -ge 3) { $UserPasswordMaxAge = (Get-ADUserResultantPasswordPolicy $User -erroraction SilentlyContinue).MaxPasswordAge } if ($UserPasswordMaxAge -eq $null) { $UserPasswordMaxAge = $DomainMaxPasswordAge } $UserPasswordExpiry = ($User.PasswordLastSet + $UserPasswordMaxAge) $UserPasswordExpiryDays = (new-timespan -start (get-date) -end $UserPasswordExpiry).Days if ($UserPasswordExpiryDays -le $Age) { $Results += New-Object PSObject -Property @{ 'Account Name' = $UserAccountName 'First Name' = $UserFirstName 'Last Name' = $UserLastName 'Password Expiry Date' = $UserPasswordExpiry 'Days Until Expired' = $UserPasswordExpiryDays } if ($SendNotice -and $UserPasswordExpiryDays -ge 0) { $UserNotice = ($EmailNotice -replace "%Username%","$UserAccountName" | Out-String) $UserNotice = ($UserNotice -replace "%DaysLeft%","$UserPasswordExpiryDays") $UserNotice = ($UserNotice -replace "%DisplayName%","$UserDisplayName") $UserNotice = ($UserNotice -replace "%FirstName%","$UserFirstName") $UserNotice = ($UserNotice -replace "%LastName%,""$UserLastName") $UserNotice = ($UserNotice -replace "%Today%","$Today") Send-MailMessage -From $NoticeFrom -To $UserEmailAddress -SMTPServer $SMTPServer -Subject $NoticeSubject -Body $UserNotice -BodyAsHTML if ($debugging) { $UserNotice } } } } } $Results = ($Results | Sort 'Password Expiry Date') $ReportDateTime = (Get-Date -UFormat %m-%d-%y ) $ReportFilePath = "$ReportPath\ADUserPasswordExpiryReport-$ReportDateTime.csv" if ($Results -ne $null) { $Results | Select 'Password Expiry Date','Account Name','First Name','Last Name','Days Until Expired' | Export-CSV $ReportFilePath -NoTypeInformation if ($debugging) { $Results } } if ($SendReport) { if ($QuoteOfTheDay) { try { $svr = $QuoteOfTheDayServers[(Get-Random -maximum $QuoteOfTheDayServers.Count)] $conn = new-object System.Net.Sockets.TcpClient($svr,17) $str = $conn.GetStream() $buf = new-object System.Byte[] 4096 $count = $str.Read($buf, 0, 4096) $QOTD = [System.Text.Encoding]::ASCII.GetString($buf,0,$count) $str.close() $conn.close() if ($debugging) { $QOTD } } catch { # do nothing } } if ($Results -ne $null) { $ReportEmailBody = ($Results | Select 'Password Expiry Date','Account Name','First Name','Last Name','Days Until Expired' | ConvertTo-HTML -Head $ReportEmailHeader | Out-String) } else { $NumberOfUsers = $Users.count $ReportEmailBody = "0 out of $NumberOfUsers users found with expiring passwords.`r`nSir, you have won this day, but for naught you know it will happen soon" if ($QOTD -ne $null) { $ReportEmailBody += "<br>`r`n<br>`r`n$QOTD" } } if(Test-Path $ReportFilePath) { Send-MailMessage -From $ReportFrom -To $ReportTo -SMTPServer $SMTPServer -Subject $ReportSubject -Body $ReportEmailBody -BodyAsHTML -Attachments $ReportFilePath } else { Send-MailMessage -From $ReportFrom -To $ReportTo -SMTPServer $SMTPServer -Subject $ReportSubject -Body $ReportEmailBody -BodyAsHTML } } |
As well, the HTML template that we used to notify our users here at Tech With a Hammer can be referenced below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
%DisplayName%, <p> This email is an automated notification that your password is going to expire in %DaysLeft% days.</br> You will receive this email daily until your password expires or you change your password. <p> Once your password expires you will lose access to network resources, so please change it soon. <p> You may change your password by logging into any office PC, Press "CTRL+ALT+DELETE" and press "Change a password". If you're out of the office click <a href="https://mail.techwithahammer.com/owa">here</a> to login to Outlook Web Access and change your password by clicking "options" and then "change password". <p> Don't forget to input your new password on your phone, iPad, etc. <p> If you require assistance or have a question about this, just reply to this email and let me know. <p> Thank you,<br> Just Another Tech With a Hammer <p> <font size="-1">Your friendly neighbourhood IT Department!</font><br> <font size="-3">This notice was sent %Today%.</font> |