-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathADTools_FindStaleWorkstations.ps1
29 lines (25 loc) · 1.12 KB
/
ADTools_FindStaleWorkstations.ps1
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
$DaysInactive = 180
$OUtosearch = "" #Please enter the OU DN to search here
$PCNamesToIgnore = @("*SCCM*", "*HYPERV*")
$OldAgeDescription = "***This computer has been inactive for $DaysInactive or more days***"
$time = (Get-Date).Adddays(-($DaysInactive))
$pclist = Get-ADComputer -SearchBase $OUtosearch -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name
[System.Collections.ArrayList]$removePCHolder = $pclist
#Remove computers on the ignore list from the array list
foreach($nameToIgnore in $PCNamesToIgnore){
foreach ($pc in $pclist){
$pcName = $pc.Name
if($pcName -like $nameToIgnore){
Write-Host "$pcName is on the ignore list" -ForegroundColor Yellow
$removePCHolder.Remove($pc)
}else{
#do something else
}
}
}
#Set PC Desciption for leftover old PCs
foreach($pcLeft in $removePCHolder){
Write-host "$($pcLeft.Name) description is being updated to reflect old age in AD`n"
Set-ADComputer $pc.Name -Description $OldAgeDescription
#Disable-ADAccount -Identity $pc.Name #If you want to disable as well
}