简体   繁体   中英

Powershell - Filter results of varaible in pipeline

I want to limit the results of the $Groups variable below to only return members of a specific group. This code retrieves the groups a computer belongs to. I am interested only in those that are members of any AD group that contains "SCCM".

I tried line 5 but that doesn't work. What is the correct method to do this? Thanks.

    $SamAccountNames = (Get-ADComputer Computer).SamAccountName
    $OSInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName Computer
    $ServerGroupMemberShipList = Get-ADPrincipalGroupMembership $SAMAccountNames | Sort SAMAccountName 
    $Groups = $ServerGroupMemberShipList.Name 
    #$Groups = $ServerGroupMemberShipList.Name | Select {$ServerGroupMemberShipList.Name -like "SCCM"}
    Write-Host " "
    Write-Host "Checking Software Updates Compliance on" $Computer.ToUpper() -ForegroundColor Yellow -NoNewline
    Write-Host $OSInfo.Caption
    $Groups 

Rather than using the Select-Object cmdlet, try using the Where-Object cmdlet for filtering or ? for short. The $_ variable is the current object being processed by the cmdlet.

$Groups = $ServerGroupMemberShipList | Where-Object { $_.Name -like "SCCM" }

or

$Groups = $ServerGroupMemberShipList | ? { $_.Name -like "SCCM" }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM