Created
December 9, 2019 11:13
-
-
Save EvilGrinUK/5705a1d3bf6586bb687b31881251d253 to your computer and use it in GitHub Desktop.
Finds Computer Accounts in AD that have not been logged into for a configurable amount of days (90 is the default, the default expiry time for Computer Accounts). The script will also automatically delete (or optionally disable) Workstation accounts to reduce AD clutter. It will not delete Server accounts or 'Unknown Accounts' (e.g. the AAD Conn…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Import AD module - Installed as part of Windows Server Admin Tools | |
Import-Module ActiveDirectory | |
#Email settings | |
$To = @("IT Alerts <italerts@company.com>") | |
$Subject = "Inactive AD Computer Accounts" | |
$SmtpServer = "mail.company.com" | |
$From = "SCCM <SCCM@company.com>" | |
#Query AD Computers | |
$searchbase = "DC=ad,DC=company, DC=co, DC=uk" | |
$Days = (Get-Date).AddDays(-90) | |
$ADcomputers = Get-ADComputer -Properties * -Filter {enabled -eq $true} -SearchBase $searchbase | |
## Grab results as HTML tables | |
# Servers | |
$frag1 = $ADcomputers | Where {$_.OperatingSystem -Like “*server*” -and $_.lastLogonDate -lt $days} ` | |
| Select-Object Name, CanonicalName, OperatingSystem,OperatingSystemServicePack,whenCreated,LastLogonDate ` | |
| Sort-Object CanonicalName ` | |
| ConvertTo-Html -Fragment -PreContent ‘<h2>Inactive Servers (Verify and Remove Manually)</h2>’ ` | |
| Out-String | |
# Workstations | |
$frag2 = $ADcomputers | Where {($_.OperatingSystem -Like “Windows 7*” -or $_.OperatingSystem -Like “Windows 10*") -and $_.lastLogonDate -lt $days} ` | |
| Select-Object Name, CanonicalName, OperatingSystem,OperatingSystemServicePack,whenCreated,LastLogonDate ` | |
| Sort-Object CanonicalName ` | |
| ConvertTo-Html -Fragment -PreContent ‘<h2>Inactive Workstations (Automatically Disabled in AD)</h2>’ ` | |
| Out-String | |
# Other Systems | |
$frag3 = $ADcomputers | Where {$_.OperatingSystem -NotLike “Windows*” -and $_.lastLogonDate -lt $days} ` | |
| Select-Object Name, CanonicalName, OperatingSystem,OperatingSystemServicePack,whenCreated,LastLogonDate ` | |
| Sort-Object CanonicalName ` | |
| ConvertTo-Html -Fragment -PreContent ‘<h2>Inactive Other Systems (Verifty and Remove Manually)</h2>’ ` | |
| Out-String | |
# Remove / Disable Workstations | |
$RemovableADComputers = $ADcomputers | Where {($_.OperatingSystem -Like “Windows 7*” -or $_.OperatingSystem -Like “Windows 10*") -and $_.lastLogonDate -lt $days} | |
# Uncomment to disable rather than delete AD accounts | |
# $RemovableADComputers | Disable-ADAccount | |
$RemovableADComputers | Remove-ADObject -Recursive -Confirm:$false | |
$head = @" | |
<style> | |
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;} | |
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6495ED;} | |
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;} | |
</style> | |
"@ | |
## Send via email | |
$Splat = @{ | |
To =$To | |
Body ="$($InputObject | ConvertTo-HTML -head $head -PostContent $frag1,$frag2,$frag3 -PreContent “<h1>Inactive Active Directory Computer Accounts Report (Not logged in for 90 days)</h1>” )" | |
Subject =$Subject | |
SmtpServer =$SmtpServer | |
From =$From | |
BodyAsHtml =$True | |
} | |
Send-MailMessage @Splat | |
# Uncomment to Export to CSV | |
#Export-CSV InactiveComputers.csv -NoTypeInformation -Encoding UTF8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment