[英]How can I read all Active Directory computers into an array in PowerShell?
When I do: 当我做:
# 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
}
$Computer
comes out as System.DirectoryServices.SearchResult
. $Computer
以System.DirectoryServices.SearchResult
。 How can I query Active Directory for all computers and store their string values into an array like this? 如何查询所有计算机的Active Directory并将其字符串值存储到这样的数组中?
I need to keep the foreach
syntax (ie foreach $Computer in $Reader
) because my code is such that $Reader
can also accept a file of computers like so: 我需要保留
foreach
语法(即foreach $Computer in $Reader
)因为我的代码是这样的, $Reader
也可以接受这样的计算机文件:
$Reader = Get-Content ((Resolve-Path $iL).Path)
In summary, I want something that works with both an input file OR by querying AD using the same looping syntax. 总之,我想要一些适用于输入文件或通过使用相同的循环语法查询AD的东西。
EDIT : My input file is a text file that separates computer names by a newline as in the following: 编辑 :我的输入文件是一个文本文件,用新行分隔计算机名称,如下所示:
win7box1
win7box2
win10box3
I solved it. 我解决了 To get a string from
System.DirectoryServices.DirectorySearcher
, you could just do a loop as in my example, and do: 要从
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
}
Should do the trick. 应该做的伎俩。 No need for more.
不需要更多。
You could use System.DirectoryServices.DirectorySearcher
for performance reasons, though. 但是,出于性能原因,您可以使用
System.DirectoryServices.DirectorySearcher
。 Or use a real filter, not *
. 或使用真正的过滤器,而不是
*
。
I guess you took your code from here , if you have not tried anything else before, it is not the easiest choice PowerShell offers. 我猜你从这里拿了你的代码,如果你之前没有尝试过其他任何东西,它不是PowerShell提供的最简单的选择。
Edit: 编辑:
You need the ActiveDirectory module loaded for Get-ADComputer
to be available ( Import-Module ActiveDirectory
). 您需要为
Get-ADComputer
加载的ActiveDirectory模块可用( Import-Module ActiveDirectory
)。
... and you need RSAT installed for the ActiveDirectory
PowerShell module to be available. ...并且您需要安装RSAT才能使
ActiveDirectory
PowerShell模块可用。
Edit 2: 编辑2:
Well without RSAT installed, obviously, this answer becomes pointless. 没有安装RSAT,很明显,这个答案毫无意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.