0

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

不幸的是,它们都只是被称为[收藏]

使用 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)
}

我该怎么做才能让正确的名称出现在检查列表中。

感谢您的帮助:)

4

2 回答 2

0

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

这意味着你可以简单地做

. . . 

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults) {
    $checkedlistbox1.Items.add($objResult.Properties['name'][0])
}
于 2018-08-30T13:16:15.410 回答
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)
}
于 2018-08-30T13:16:43.967 回答