Sto provando a creare uno script che genera un elenco di computer basati su proprietà specifiche che un computer può avere. Ad esempio, sto cercando di creare un elenco di computer Windows XP e computer Windows 7, di inserire i loro nomi in un file .csv e di emettere il conteggio finale di ciascuno.PowerShell - OU filtro durante l'utilizzo di get-adcomputer
Ecco il mio codice finora
import-module ActiveDirectory
$computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties "OperatingSystem"
$i = 0
$j = 0
Foreach ($computer in $computers) {
if ($computer.operatingSystem -like "Windows 7*") {
$i++
'"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
}
elseif ($computer.OperatingSystem -like "Windows XP*") {
$j++
'"{0}","{1}","{2}"' -f $computer.Name, $computer.OperatingSystem, "$computer.DistinguishedName" | Out-file -append C:\users\admin\desktop\test.txt
}
else {
$_
}
}
write-host "$i Win 7"
write-host "$j Win xp"
$k = $i+$j
write-host "$k Total"
Output di esempio:
104 Win 7
86 Win xp
190 Total
Questo script funziona però vorrei fare un po 'meglio per essere in grado di dire quale unità organizzativa non è guardare dentro, ma non riesco a capirlo.
Se qualcuno ha qualche idea su come farlo, o anche solo per migliorare il codice sopra, mi piacerebbe sentirlo.
Grazie!
Posso aggiungere più di una stringa usando l'operatore -or? $ goodOU = $ computer |? {$ _. DistinguishedName -notlike ("* ou = evil, *" -or "* CN = BadCN, *")}. Questa non è la sintassi corretta anche se – smierdziel
esamina gli esempi, e provare questo non funziona. $ filteredComputers = $ computers | where-object {($ _. DistinguishedName -notlike "* OU = Laptops, *") -o ($ _. DistinguishedName -notlike "* CN = Resources, *") -o ($ _. DistinguishedName -notlike "* CN = Computers, * ")} – smierdziel