Normally, there is one disabled computer object under
OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN
However, this is not present in the CSV file.
In addition, as seen in my output, I am receiving duplicate results.
OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN
Objects under OU are in CSV.
What can be done for this type of OU?
$LastLogonDate = (Get-Date).AddDays(-90)
$OUs = @(
'OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN'
'OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN'
)
$inactiveComputers = @()
foreach ($OU in $OUs) {
$inactiveComputers += Get-ADComputer -Properties LastLogonTimeStamp `
-Filter {LastLogonTimeStamp -lt $LastLogonDate -and serviceprincipalname -notlike "*MSClusterVirtualServer*" -and Enabled -eq $false } `
-SearchBase $OU |
Select-Object distinguishedName,Name, @{Name='LastLogonTimeStamp'; Expression={[DateTime]::FromFileTime($_.LastLogonTimeStamp)}}
}
$inactiveComputers
My output:
"distinguishedName","Name","LastLogonTimeStamp"
"CN=computer19,OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN","computer19","26.09.2019 19:59:31"
"CN=computer19,OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN","computer19","26.09.2019 19:59:31"
"CN=computer22,OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN","computer22","14.04.2020 09:58:57"
"CN=computer22,OU=COMPUTERS,OU=DISABLED COMPUTERS,OU=DISABLED,DC=CONTOSO,DC=DOMAIN","computer22","14.04.2020 09:58:57"
Use -SearchScope OneLevel
, COMPUTERS
OU is in DISABLED COMPUTERS
OU thus you get duplicated computers, you're querying it twice, there is nothing special about it.
Get-ADComputer ... -SearchBase $OU -SearchScope OneLevel | Select-Object ...