Forum Discussion
dannytveria
Oct 26, 2021Brass Contributor
Disable \ Remove old computer accounts
Hi,
I have a script for disabling & removing old computer accounts.
Import-Module ActiveDirectory
# Set the Parameters since last logon
$ForDisable = "C:\Logs\Computers Accounts\Disable-$((Get-Date).ToString('dd-MM-yyyy')).csv"
$ForDelete = "C:\Logs\Computers Accounts\Delete-$((Get-Date).ToString('dd-MM-yyyy')).csv"
# Automated way (includes never logged on computers)
function Get-BadPC{
param(
[parameter(mandatory=$true)]$Days,
[parameter(mandatory=$true)]
[ValidateSet('Delete','Disable')]$Action
)
$InactiveDate = (Get-Date).Adddays(-($Days))
$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=staff ,DC=local" | Where-Object {($_.distinguishedname -notlike "*,OU=Servers,*") -and ($_.distinguishedname -notlike "*,OU=Test,*") -and ($_.distinguishedname -notlike "*,OU=IT,*") -and ($_.distinguishedname -notlike "*,OU=Laptops,*") -and ($_.distinguishedname -notlike "*,CN=Computers,*") -and ($_.distinguishedname -notlike "*,CN=Managed Service Accounts,*") }
$Computers_For_Action | Export-Csv "C:\Logs\Computers Accounts\$($Action)-$((Get-Date).ToString('dd-MM-yyyy')).csv" -NoTypeInformation -Encoding UTF8
switch ($action){
Disable {$Computers_For_Action | Disable-ADAccount }
Delete {$Computers_For_Action | Remove-ADComputer -Confirm:$False }
}
}
Get-BadPC -Days 180 -Action Disable
Get-BadPC -Days 365 -Action Delete
The script working great.
the problem I get every day is the same computer accounts that have already been disabled or deleted on the previous day.
My goal is to receive a CSV file with the computer account that is disabled or removed on the same day.
thanks for the help.
Ok , test it and let me know if this help:
$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=staff ,DC=local" | Where-Object {($_.Enabled -eq $true) -and ($_.distinguishedname -notlike "*,OU=Servers,*") -and ($_.distinguishedname -notlike "*,OU=Test,*") -and ($_.distinguishedname -notlike "*,OU=IT,*") -and ($_.distinguishedname -notlike "*,OU=Laptops,*") -and ($_.distinguishedname -notlike "*,CN=Computers,*") -and ($_.distinguishedname -notlike "*,CN=Managed Service Accounts,*") }
- farismalaebSteel ContributorHi,
You will need to modify the filter and add the modifieddate property in the search criteria.- dannytveriaBrass ContributorHow do I do it?
- farismalaebSteel Contributor
Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=staff ,DC=local" | Where-Object {($_.whenChanged -lt (Get-Date).AddDays(-1)) -and ($_.distinguishedname -notlike "*,OU=Servers,*") -and ($_.distinguishedname -notlike "*,OU=Test,*") -and ($_.distinguishedname -notlike "*,OU=IT,*") -and ($_.distinguishedname -notlike "*,OU=Laptops,*") -and ($_.distinguishedname -notlike "*,CN=Computers,*") -and ($_.distinguishedname -notlike "*,CN=Managed Service Accounts,*") }
dannytveria So the following what you need to add
($_.whenChanged -lt (Get-Date).AddDays(-1)