Skip to content

Instantly share code, notes, and snippets.

@jessewolcott
Created October 30, 2023 15:22
Show Gist options
  • Save jessewolcott/129ac4d9379faebb233cdf72537ad8d8 to your computer and use it in GitHub Desktop.
Save jessewolcott/129ac4d9379faebb233cdf72537ad8d8 to your computer and use it in GitHub Desktop.
ADShuffle.ps1
$Active_OU = "OU=Systems,DC=fabrikam,DC=com"
$Stale_OU = "OU=DisabledOU,DC=fabrikam,DC=com"
$Log_Path = "$PSScriptRoot"
$ErrorActionPreference = 'Stop'
$Lookback = '30'
try {
# Check if OUs Exist
Get-ADOrganizationalUnit -Identity $Active_OU |Out-Null
Get-ADOrganizationalUnit -Identity $Stale_OU |Out-null
#
$Days = (Get-Date).Adddays(-($Lookback))
$Results = (Get-ADComputer -Filter {(LastLogonTimeStamp -lt $Days) -and (Enabled -eq $true)} -SearchBase $Active_OU -Properties Name, LastLogonTimeStamp, LastLogonDate, DistinguishedName |`
#Select-Object -ExpandProperty Name |`
foreach-object {
Get-ADComputer -Identity $($_) | Disable-ADAccount
Move-ADObject -Identity $($_.DistinguishedName) -TargetPath $Stale_OU
New-Object -TypeName PSCustomObject -Property @{
"Computer" = $($_.Name)
"Disabled Successfully?" = $?
"Date Disabled" = (Get-Date -Format "MM/dd/yyyy")
"Last Logon Date" = $($_.LastLogonDate)
}
}) | Select-Object "Computer","Disabled Successfully?","Date Disabled","Last Logon Date"
if ($Results.count -ge 1){
$Results | Export-CSV -Path "$Log_Path\ComputersInactive30.csv" -Encoding UTF8 -Append -NoTypeInformation -Force -ErrorAction SilentlyContinue
}
}
catch{
Write-Host "_____________________________________________________________________"
Write-Host "ERROR during processing"
Write-Host "$_"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment