Currently scanning multiple specific OU’s in multiple domains from AD. I have this code set to scan for pc names & export it to various csv files based on specific name filters. I use these lists for other tasks.
Primarily, I’d like to export sorted by name (often named by building / dept / floor or type – such as BLDGA3Fxxxx, BLDGB4Fxxxxx, HR2Fxxxxx, xxxxKIOSK, etc)
Secondarily, would be great to be able to export to separate files by partial name – NameFilter1.csv, NameFilter2.csv, Etc.
Anyone able to assist on this?
$OUs = 'OU=WORKSTATION,DC=domain1,DC=rootdomain,DC=com','OU=WORKSTATION,DC=domain2,DC=rootdomain,DC=com','OU=WORKSTATION,DC=domain3,DC=rootdomain,DC=com'
$domains = "domain1","domain2","domain3"
$exportfiles = "Computer_ALL.txt", "Computer_Name1.txt", "Computer_Name2.txt", "Computer_name3.txt", "Computer_name4.txt"
$Filters = '*', '*NameFilter1*', 'NameFilter2*', '*NameFilter3', 'NameFilter4*'
foreach ($filter in $filters) {
# Loop through OU & domain simultaneously and get ALL workstations
$c = 0
write-host $filter
for ($i = 0; $i -lt $OUs.Count; $i++) {
write-host "Scanning All Workstations in $($OUs[$i]) on $($domains[$i])..." -BackgroundColor DarkCyan
Get-ADComputer -Filter { Name -notlike 'VDI-*' -and Name -notlike 'VM-*' -and Name -notlike 'HOS-*' -and Name -like $Filter} -SearchBase $($OUs[$i]) -Server $($domains[$i]) | ForEach-Object {
#Write-Host ($_.Name + " --- " + $_.DistinguishedName)
$_.Name | Out-File -LiteralPath $($exportfiles[$n]) -Append
$c++
}
}
write-host "$c total $filter pc's found. Saved to" $($exportfiles[$n]) -BackgroundColor DarkCyan
write-host " "
$n++
}