Forum Discussion
Disable and removal of Computer accounts
- Sep 13, 2021
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=Test ,DC=local" | Where-Object {($_.distinguishedname -notlike "*,OU=Servers,*") -or ($_.distinguishedname -notlike "*,OU=Test,*") -or ($_.distinguishedname -notlike "*,OU=IT,*") -or ($_.distinguishedname -notlike "*,OU=Laptops,*") -or ($_.distinguishedname -notlike "*,CN=Computers,*")} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName
$Computers_For_Action | Export-Csv "C:\Users\Administrator\Desktop\$($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 356 -Action Delete
This is my script and i get the next error
Disable-ADAccount : The input object cannot be bound to any parameters for the command either because the command does not take pipel
ine input or the input and its properties do not match any of the parameters that take pipeline input.
dannytveria Hi
Yes, this is expected if the number of computer object returned is zero.
You can see the result by running the query it self
$InactiveDate = (Get-Date).Adddays(-($Days))
$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=Test ,DC=local" | Where-Object {($_.distinguishedname -notlike "*,OU=Servers,*") -or ($_.distinguishedname -notlike "*,OU=Test,*") -or ($_.distinguishedname -notlike "*,OU=IT,*") -or ($_.distinguishedname -notlike "*,OU=Laptops,*") -or ($_.distinguishedname -notlike "*,CN=Computers,*")} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName
Write-Host "The Number of hosts in your search is " -NoNewline
Write-Host "$($Computers_For_Action.count)" -ForegroundColor Green
- dannytveriaOct 17, 2021Copper Contributor
Hi Faris how r u?
I found a problem with the report CSV.
I see in the report always the computer that already disabled.
Is there any way to change the script that will report only the computer account he found every morning?
- farismalaebSep 13, 2021Steel Contributor
- dannytveriaSep 13, 2021Copper ContributorFirst of all thanks a lot Faris.
Yes, I remember you told me to remove and I actually removed this raw after the pipeline but still has not worked.
Now it`s working well
Again Thanks a lot
Stay health - farismalaebSep 12, 2021Steel Contributor
In the original script, I posted there was no Select-Object at the end of the filter.
This is what is causing the issue.
Please remove the select Object$Computers_For_Action = Search-ADAccount -AccountInactive -DateTime $InactiveDate -ComputersOnly -SearchBase "DC=Test ,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,*")}
Spoiler|Select-Object Name, LastLogonDate, Enabled, DistinguishedName - dannytveriaSep 12, 2021Copper Contributor
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=Test ,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,*")} | Select-Object Name, LastLogonDate, Enabled, DistinguishedName $Computers_For_Action | Export-Csv "C:\Users\Administrator\Desktop\$($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 356 -Action Delete
sure
Thanks
- farismalaebSep 12, 2021Steel Contributorwould you please show me the code again with the filter.
I try it on my side and its working fine. - dannytveriaSep 09, 2021Copper Contributor
- farismalaebSep 08, 2021Steel Contributorreplace the -or with -and in the
- dannytveriaSep 07, 2021Copper Contributor
Yes, I ran only the last script you shared with me.
I got a computer account that I need to disable and also computer accounts that not needed to disable.
- farismalaebSep 07, 2021Steel ContributorWhat is the output of the last script, are you able to get the result ?
- dannytveriaSep 06, 2021Copper ContributorI didn`t understand why the script not working.
as I posted earlier