1

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
7
  • Why not use Get-ADComputer -Filter * -Properties * for instance ? Commented Feb 1, 2017 at 18:56
  • I want something that works with both an input file OR by querying AD using the same looping syntax. I'm not sure how what you're describing would work. Commented Feb 1, 2017 at 18:57
  • Parameter Sets? Commented Feb 1, 2017 at 18:57
  • 2
    Please show us what your input file looks like Commented Feb 1, 2017 at 18:58
  • Get-ADComputer all the way...! Commented Feb 1, 2017 at 19:16

2 Answers 2

2

I solved it. To get a string from System.DirectoryServices.DirectorySearcher, you could just do a loop as in my example, and do:

foreach ($Computer in $Reader) {
     $ComputerString = $Computer.Properties.Name
}
Sign up to request clarification or add additional context in comments.

Comments

1
$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. 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.

Edit:

You need the ActiveDirectory module loaded for Get-ADComputer to be available (Import-Module ActiveDirectory).

... and you need RSAT installed for the ActiveDirectory PowerShell module to be available.

Edit 2:

Well without RSAT installed, obviously, this answer becomes pointless.

3 Comments

Is there a reason I would be getting The term 'Get-ADComputer' is not recognized as the name of a cmdlet, function, script file, or operable program.?
I cant guarantee that module will be installed on the peoples' computers who run this. Is there another way?
Sad. Updating again, I cannot run tests with System.DirectoryServices.DirectorySearcher right now but I'm sure someone around knows how to get an array of hostnames out of it : ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.