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 ?
    – sodawillow
    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?
    – 4c74356b41
    Commented Feb 1, 2017 at 18:57
  • 2
    Please show us what your input file looks like
    – sodawillow
    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
}
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
  • 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.? Commented Feb 1, 2017 at 19:23
  • I cant guarantee that module will be installed on the peoples' computers who run this. Is there another way? Commented Feb 1, 2017 at 19:27
  • 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 : ).
    – sodawillow
    Commented Feb 1, 2017 at 19:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.