powershellpowershell-studio

Populate Checkedlistbox with Servers


I am trying to populate a list with the servers in my domain, and i have partial success. There are 5 items in my list, which is as many servers as i have.

Unfortunately they are all just called [Collection]

Form is generated with Sapien Powershell Studio

$strCategory = "computer"
$strOperatingSystem = "Windows*Server*"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain

$objSearcher.Filter = ("OperatingSystem=$strOperatingSystem")

$colProplist = "name"
foreach ($i in $colPropList) { $objSearcher.PropertiesToLoad.Add($i) }

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
{

    $objComputer = $objResult.Properties;
    $objComputer.name
    $checkedlistbox1.Items.add($objComputer.name)
}

What can I do to have the proper name show up in the checkedlist.

Thanks for any assistance :)


Solution

  • The result object from DirectorySearcher.FindAll() method contains a special property named Properties that returns a typed collection containing the values of properties of the object found in the AD.

    This means that you can simply do

    . . . 
    
    $colResults = $objSearcher.FindAll()
    
    foreach ($objResult in $colResults) {
        $checkedlistbox1.Items.add($objResult.Properties['name'][0])
    }