簡體   English   中英

如何在PowerShell中將所有Active Directory計算機讀入陣列?

[英]How can I read all Active Directory computers into an array in PowerShell?

當我做:

# Gets all domain-joined computer names and properties in one object
$strCategory = "computer"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")

$colProplist = "name"

foreach ($i in $colPropList) {
    $objSearcher.PropertiesToLoad.Add($i)
}

$Reader = $objSearcher.FindAll()

foreach ($Computer in $Reader) {
   Write-Host $Computer
}

$ComputerSystem.DirectoryServices.SearchResult 如何查詢所有計算機的Active Directory並將其字符串值存儲到這樣的數組中?

我需要保留foreach語法(即foreach $Computer in $Reader )因為我的代碼是這樣的, $Reader也可以接受這樣的計算機文件:

$Reader = Get-Content ((Resolve-Path $iL).Path)

總之,我想要一些適用於輸入文件或通過使用相同的循環語法查詢AD的東西。

編輯 :我的輸入文件是一個文本文件,用新行分隔計算機名稱,如下所示:

win7box1
win7box2
win10box3

我解決了 要從System.DirectoryServices.DirectorySearcher獲取字符串,您可以像我的示例中那樣執行循環,並執行以下操作:

foreach ($Computer in $Reader) {
     $ComputerString = $Computer.Properties.Name
}
$Reader = Get-ADComputer -Filter * | Select-Object -ExpandProperty samAccountName

foreach ($Computer in $Reader) {
   #WMI processing
}

應該做的伎倆。 不需要更多。

但是,出於性能原因,您可以使用System.DirectoryServices.DirectorySearcher 使用真正的過濾器,而不是*

我猜你從這里拿了你的代碼,如果你之前沒有嘗試過其他任何東西,它不是PowerShell提供的最簡單的選擇。

編輯:

您需要為Get-ADComputer加載的ActiveDirectory模塊可用( Import-Module ActiveDirectory )。

...並且您需要安裝RSAT才能使ActiveDirectory PowerShell模塊可用。

編輯2:

沒有安裝RSAT,很明顯,這個答案毫無意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM