0

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 :)

lit
  • 14,456
  • 10
  • 65
  • 119
VxChemical
  • 23
  • 4

2 Answers2

0

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])
}
Theo
  • 57,719
  • 8
  • 24
  • 41
0

I suggest you use Get-ADComputer instead to get the list of your servers.

The you just loop thrue the list and add the servername to your checkedlist

$Servers= Get-ADComputer -Filter {OperatingSystem -Like 'Windows *Server*'} #-Property * #the property flag is not needed if you just want the Name (see comment from Theo)
foreach ($srv in $Servers) {
    #Unmark to debug
    #$srv.Name
    #$srv.OperatingSystem   

    $checkedlistbox1.Items.add($srv.Name)
}
  • `-Property *` is a bad idea performance wise. The property `Name` is always returned. If you want the extended property `OperatingSystem` too, then you ask for it like `-Property OperatingSystem`. You can read about default and extended properties [here](https://social.technet.microsoft.com/wiki/contents/articles/12056.active-directory-get-adcomputer-default-and-extended-properties.aspx) – Theo Aug 30 '18 at 13:19
  • @Theo PS-Newbei myself, so thank you, you taught me something new :) – Anders Norén Aug 30 '18 at 13:26