Mobb Deep
Mobb Deep

Reputation: 79

Filtering Powershell output

I am trying to filter the output of the following PS script;

We use server names like:

SRV-APP-001, PRD-APP-001, TST-APP-001 etc...

$strCategory = "computer" 
$strOperatingSystem = "Windows*Server*" 

$objDomain = New-Object System.DirectoryServices.DirectoryEntry 

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
$objSearcher.SearchRoot = $objDomain

$objSearcher.Filter = ("OperatingSystem=$strOperatingSystem") 

$colProplist = "name" 
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)} 

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults) 
    { 
    $objComputer = $objResult.Properties;  
    $objComputer.name
    }

The output of this script are all the servers in the Domain.

But I want only see the servers that start with "SRV" or "PRD"

the | where { $_name -like "SRV*"} is not realy working after the $objComputer.name part.

Thank you in advance

Upvotes: 0

Views: 1008

Answers (1)

Shay Levy
Shay Levy

Reputation: 126702

Change the filter to:

"(|(name=SRV*)(name=PRD*))(OperatingSystem=Windows*Server*)"

Upvotes: 3

Related Questions