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-$((Ge...
- Nov 02, 2021
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,*") }
farismalaeb
Oct 26, 2021Iron Contributor
Hi,
You will need to modify the filter and add the modifieddate property in the search criteria.
You will need to modify the filter and add the modifieddate property in the search criteria.
dannytveria
Oct 26, 2021Brass Contributor
How do I do it?
- farismalaebOct 27, 2021Iron 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)
- dannytveriaOct 29, 2021Brass Contributor
Hi Faris,
I changed as you said, it didn`t help.$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=student ,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,*") -and ($_.distinguishedname -notlike "*,OU=Classes,*") -and ($_.distinguishedname -notlike "*,OU=Teacher Standing,*") -and ($_.distinguishedname -notlike "*,OU=WVD,*") -and ($_.distinguishedname -notlike "*,OU=Margolin and Chativa Teachers and Workers,*") }
I still get in the report the same computers from the first day I runed the script
- dannytveriaNov 01, 2021Brass ContributorDo you have any idea?
- dannytveriaOct 28, 2021Brass ContributorHi Faris,
I changed today the script, I will update tomorrow.
thanks