【问题标题】:Populate Checkedlistbox with Servers用服务器填充 Checkedlistbox
【发布时间】:2018-08-30 12:46:22
【问题描述】:

我正在尝试使用我的域中的服务器填充列表,并且我取得了部分成功。我的列表中有 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)
}

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

感谢您的帮助:)

【问题讨论】:

  • 您只是想更改 datagridview 控件中的列名?

标签: powershell powershell-studio


【解决方案1】:

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

这意味着你可以简单地做

. . . 

$colResults = $objSearcher.FindAll()

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

【讨论】:

    【解决方案2】:

    我建议您改用 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)
    }
    

    【讨论】:

    • -Property * 在性能方面是个坏主意。始终返回属性 Name。如果您也想要扩展属性OperatingSystem,那么您可以像-Property OperatingSystem 一样要求它。你可以阅读默认和扩展属性here
    • @Theo PS-Newbei 我本人,谢谢你,你教会了我一些新东西:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2014-02-05
    • 2018-03-14
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多