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
. How can I query Active Directory for all computers and store their string values into an array like this?
I need to keep the foreach
syntax (i.e. foreach $Computer in $Reader
) because my code is such that $Reader
can also accept a file of computers like so:
$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.
EDIT: My input file is a text file that separates computer names by a newline as in the following:
win7box1
win7box2
win10box3
Get-ADComputer -Filter * -Properties *
for instance ?