In this post, I will share with you a script I use to run a report on a number of special accounts in my environment. It reports on admin accounts, service accounts and computer accounts. It will collect information like the name of the account, enabled or disabled, last logon date, account expiration date, password expiration date, is the password expired, is Password Never Expires ticked, employeeIF (if used), location, etc. The collection information will be saved in a .html file and also sent by email.
To use the script, the variables below need to be set:
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | Clear-Host If (-not (Get-Module -Name 'ActiveDirectory' -ErrorAction SilentlyContinue)) { Import-Module ActiveDirectory } # Get domain specific variables $Domain = "contoso.com" $DomainData = Get-ADDomain -Server $Domain $DomainDistinguishedName = $DomainData.DistinguishedName $DomainDNSRoot = $DomainData.DNSRoot $DomainDC = $DomainData.PDCEmulator $DomainNetBIOSName = $DomainData.NetBIOSName $DomainNetlogonDir = "\" + $DomainNetBIOSName + "\NetLogon" $AdminAccountsOU = "OU=Admins," + $DomainDistinguishedName $DisabledAdminAccountsOU = "OU=Disabled," + $AdminAccountsOU $ServiceAccountsOU = "OU=Service Accounts," + $DomainDistinguishedName # Variable declaration $ErrorActionPreference = 'Stop' $WeekNumber = Get-Date -uformat %V $ReportPath = "D:\Scripts\ICT-Administration\AD-SpecialAccounts-WeeklyReport" $ReportName = "Week $WeekNumber - Weekly special accounts report - $DomainDNSRoot" $ReportFile = $ReportPath + "" + $ReportName + ".html" If ( -Not (Test-Path -Path $ReportPath)) { New-Item -ItemType directory -Path $ReportPath } ## Disabled admin accounts $ReportDisabledAdminAccounts = @() $DisabledAdminAccounts = @() $DisabledAdminAccounts = Search-ADAccount -Server $DomainDC -AccountDisabled -SearchBase $AdminAccountsOU ForEach ($DisabledAdminAccount in $DisabledAdminAccounts) { $a1 = $null $a1 = ((Get-ADUser -Server $DomainDC -Identity $DisabledAdminAccount -Properties LastLogonDate).LastLogonDate) If ($a1) { $a1 = ($a1).ToString("dd-MMM-yyyy") } Else { $a1 = "" } $b1 = $null $b1 = ((Get-ADUser -Server $DomainDC -Identity $DisabledAdminAccount -Properties AccountExpirationDate).AccountExpirationDate) If ($b1) { $b1 = ($b1).ToString("dd-MMM-yyyy") } Else { $b1 = "" } $c1 = $null $c1 = (Get-ADUser -Server $DomainDC -Identity $DisabledAdminAccount -Properties "msDS-UserPasswordExpiryTimeComputed" | ` Select-Object -Property @{Name = "ExpiryDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }).ExpiryDate If ($c1) { $c1 = ($c1).ToString("dd-MMM-yyyy") } Else { $c1 = "" } $d1 = $null $d1 = ($DisabledAdminAccount.DistinguishedName).Split(",", 2)[1] $e1 = $null $e1 = (Get-ADUser -Server $DomainDC -Identity $DisabledAdminAccount -Property EmployeeID).EmployeeID $ItemDisabledAdminAccounts = [PSCustomObject]@{ 'Name' = $DisabledAdminAccount.Name 'Enabled' = $DisabledAdminAccount.Enabled 'Last Logon Date' = $a1 'Account Expiration Date' = $b1 'Password Expiration Date' = $c1 'Employee ID' = $e1 'Account location' = $d1 } $ReportDisabledAdminAccounts += $ItemDisabledAdminAccounts } ## Enabled admin accounts $ReportEnabledAdminAccounts = @() $EnabledAdminAccounts = @() #$EnabledAdminAccounts += Get-ADUser -Server $DomainDC -SearchBase $AdminAccountsOU -Filter * -Properties * | Where-Object { $_.Enabled -eq $True -AND $_.Name -like "adm-*" } $EnabledAdminAccounts += Get-ADUser -Server $DomainDC -SearchBase $AdminAccountsOU -Filter * -Properties * | Where-Object { $_.Enabled -eq $True } ForEach ($EnabledAdminAccount in $EnabledAdminAccounts) { $a1 = $null $a1 = ((Get-ADUser -Server $DomainDC -Identity $EnabledAdminAccount -Properties LastLogonDate).LastLogonDate) If ($a1) { $a1 = ($a1).ToString("dd-MMM-yyyy") } Else { $a1 = "" } $b1 = $null $b1 = ((Get-ADUser -Server $DomainDC -Identity $EnabledAdminAccount -Properties AccountExpirationDate).AccountExpirationDate) If ($b1) { $b1 = ($b1).ToString("dd-MMM-yyyy") } Else { $b1 = "" } $c1 = $null $c1 = (Get-ADUser -Server $DomainDC -Identity $EnabledAdminAccount -Properties "msDS-UserPasswordExpiryTimeComputed" | ` Select-Object -Property @{Name = "ExpiryDate"; Expression = { [datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed") } }).ExpiryDate If ($c1) { $c1 = ($c1).ToString("dd-MMM-yyyy") } Else { $c1 = "" } $ItemEnabledAdminAccounts = [PSCustomObject]@{ 'Name' = $EnabledAdminAccount.Name 'Enabled' = $EnabledAdminAccount.Enabled 'Last Logon Date' = $a1 'Account Expiration Date' = $b1 'Password expired?' = $EnabledAdminAccount.PasswordExpired 'Password Expiration Date' = $c1 'Password Never Expires' = $EnabledAdminAccount.PasswordNeverExpires 'Employee ID' = $EnabledAdminAccount.EmployeeID 'Account location' = ($EnabledAdminAccount.DistinguishedName).Split(",", 2)[1] } $ReportEnabledAdminAccounts += $ItemEnabledAdminAccounts } ## Service accounts $ReportServiceAccounts = @() #$AllServiceAccounts = Get-ADUser -Server $DomainDC -Filter { Name -like "svc*" -or Name -like "srvc*" } -Properties Name, Enabled, LastLogonDate, PasswordNeverExpires, DistinguishedName $AllServiceAccounts = Get-ADUser -Server $DomainDC -SearchBase $ServiceAccountsOU -Filter * -Properties Name, Enabled, LastLogonDate, PasswordNeverExpires, DistinguishedName ForEach ($ServiceAccount in $AllServiceAccounts) { $a1 = $null $a1 = ((Get-ADUser -Server $DomainDC -Identity $ServiceAccount -Properties LastLogonDate).LastLogonDate) If ($a1) { $a1 = ($a1).ToString("dd-MMM-yyyy") } Else { $a1 = "" } $ItemServiceAccounts = [PSCustomObject]@{ 'Name' = $ServiceAccount.Name 'Enabled' = $ServiceAccount.Enabled 'Last Logon Date' = $a1 'Password Never Expires' = $ServiceAccount.PasswordNeverExpires 'Account location' = ($ServiceAccount.DistinguishedName).Split(",", 2)[1] } $ReportServiceAccounts += $ItemServiceAccounts } ## Computer accounts $ReportComputerAccounts = @() $AllComputerAccounts = Get-ADComputer -Server $DomainDC -Filter { Name -notlike "SS-DC*" } -Properties Name, Enabled, LastLogonDate, DistinguishedName ForEach ($ComputerAccount in $AllComputerAccounts) { $a1 = $null $a1 = $ComputerAccount.LastLogonDate If ($a1) { $a1 = ($a1).ToString("dd-MMM-yyyy") } Else { $a1 = "" } $ItemComputerAccounts = [PSCustomObject]@{ 'Name' = $ComputerAccount.Name 'Enabled' = $ComputerAccount.Enabled 'Last Logon Date' = $a1 'Account location' = ($ComputerAccount.DistinguishedName).Split(",", 2)[1] } $ReportComputerAccounts += $ItemComputerAccounts } ### Create report section : Build HTML report $ReportHTML1 = $ReportDisabledAdminAccounts | Sort-Object 'Name' | ConvertTo-Html -Fragment -PreContent "<h2>Disabled admin accounts</h2>" $ReportHTML2 = $ReportEnabledAdminAccounts | Sort-Object 'Name' | ConvertTo-Html -Fragment -PreContent "<h2>Enabled admin accounts</h2>" $ReportHTML3 = $ReportServiceAccounts | Sort-Object 'Name' | ConvertTo-Html -Fragment -PreContent "<h2>Service accounts</h2>" $ReportHTML4 = $ReportComputerAccounts | Sort-Object 'Name' | ConvertTo-Html -Fragment -PreContent "<h2>Computer accounts</h2>" $Body = " <style> TABLE {<br /> border-width: 1px;<br /> border-style: solid;b<br /> order-color: black;<br /> border-collapse: collapse;<br /> white-space:nowrap;<br /> }<br /> TH {<br /> border-width: 1px;<br /> padding: 4px;<br /> border-style: solid;<br /> border-color: black<br /> }<br /> TD {border-width: 1px;<br /> padding: 2px 10px;<br /> border-style: solid;<br /> border-color: black;<br /> }<br /> TR:NTH-CHILD(odd) {<br /> background-color:#eee;<br /> }<br /> TR:NTH-CHILD(even) {<br /> background-color:#fff;<br /> }<br /> </style> <h1>Week $WeekNumber - Weekly special accounts report - $DomainDNSRoot</h1> $ReportHTML1 $ReportHTML2 $ReportHTML3 $ReportHTML4 " ConvertTo-Html -Title "Week $WeekNumber - Weekly special accounts report - $DomainDNSRoot" -Body $Body | Set-Content $ReportFile -Force Start-Process -FilePath $ReportFile # Mail report $FromAddress = '<from email="" address'="" $toaddress="<To email address 1>" ,="" '<to="" address="" 2="">' $SMTPServer = '<smtp sserver="">' $Subject = "Week $WeekNumber - Weekly special accounts report - $DomainDNSRoot" Send-MailMessage ` -From $FromAddress ` -To $ToAddress ` -Subject $Subject ` -SmtpServer $SMTPServer ` -Attachments $ReportFile |
As always, please keep in mind this script is tailored to my environment, but can be used as a template for your environment. I do not pretend to be a PowerShell guru and as such my script may not be perfect. I am open to suggestions 🙂
If you found this script useful, I’d appreciate it if you leave a comment.