使用服务器填充 Checkedlistbox

Populate Checkedlistbox with Servers

我正在尝试用我域中的服务器填充一个列表,但我取得了部分成功。我的列表中有 5 个项目,这是我拥有的服务器数量。

不幸的是他们都被称为[Collection]

表格是用 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)
}

我该怎么做才能使正确的名称显示在检查列表中。

感谢您的帮助:)

DirectorySearcher.FindAll() 方法的结果对象包含一个名为 Properties 的特殊 属性,returns 一个类型化的 集合 包含在 AD 中找到的对象的属性值。

这意味着你可以简单地做

. . . 

$colResults = $objSearcher.FindAll()

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

我建议您改用 Get-ADComputer 来获取您的服务器列表。

您只需遍历列表并将服务器名称添加到您的检查列表

$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)
}