Für einige PowerShell-Scripte (die ich auch hier veröffentlichen werde, sobald sie fertig sind) brauche ich eine Liste aller Computer, die in unserem Active Directory bekannt sind. Nichts einfacher als das:
-
# Returns a list of all computers (running Windows XP) in the local LDAP
-
function GetComputersFromLDAP()
-
{
-
trap
-
{
-
write-host ("Error while retrieving computers from LDAP: " + $_.exception.message) -foregroundcolor "red";
-
return $false;
-
}
-
$pcs = @();
-
$dir = "LDAP://DC=subdomain,DC=example,DC=com";
-
$ldapSearcher = new-object directoryservices.directorysearcher;
-
$ldapSearcher.filter = "(objectclass=computer)";
-
$computers = $ldapSearcher.findall();
-
foreach ($computer in $computers)
-
{
-
if ($computer.properties["operatingsystem"] -eq "Windows XP Professional")
-
{
-
$pc = "" | select-object Name;
-
$pc.Name = $computer.properties["name"];
-
$pcs += $pc;
-
}
-
}
-
return ($pcs | sort-object Name);
-
}
Über das zurückgegebene Array kann man dann wie folgt iterieren:
-
foreach ($pc in $pcs)
-
{
-
write-host $pc.Name;
-
}











