简体   繁体   English

如何在PowerShell中将所有Active Directory计算机读入阵列?

[英]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 . $ComputerSystem.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.

相关问题 如何读取设备中的所有文件和目录? - How can I read all files and directory in a device? 如何从目录中读取所有文件并将其放入现有的datagrindview中 - How can I read all files from directory and put in an existing datagrindview 我如何将从文件中读取的数据存储在新数组中并打印新数组中的所有数据 - How i can store the data read from the file in new array and print all the data from the new array 当我读取此文件时,如何读取所有记录 - When I read this file, how can i read all the records 如何从WAR中的classes目录中读取文件? - How can I read file from classes directory in my WAR? 如何将数组中的所有文件路径移动到一个目录 - How do I move all the file paths in an array to one directory 如何在Perl中获得目录中的所有文件,而不是子目录中的所有文件? - How can I get all files in a directory, but not in subdirectories, in Perl? 在 Laravel 中,如何访问存储目录中的所有文件? - In Laravel, how can I access all files inside of the storage directory? 如何在Java中迭代读取目录中的所有文件? - How to read all files in directory iteratively in Java? Java - 如何读取同一目录中的所有文件 - Java - How to read all files in the same directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM