none
Using if condition in pipeline to assess null entries RRS feed

  • Question

  • Hi All,

    Before I ask my question I should state that I'm still relatively fresh to the world of PowerShell, so go easy on me! :)

    I'm currently trying to find a way to extract a list of AD groups and their current members without having to build a script. Why no script, well unfortunately I work in an (anti-trust) environment where our PS execution policies are set to restricted by our main IT area so building scripts is out of the question for ad-hoc tasks.

    I've so far been able to construct something using pipes to get me very close to the result I'm after, but being a tad OCD I'm wanting to see if I can fine-tune the results I'm getting. Here's what I've got so far...

    Get-ADGroup -Filter {name -like "PartOfGroupName*"} | ForEach-Object {Write-Host $_.name -ForegroundColor red -BackgroundColor Black ; Get-ADGroupMember -Identity $_.samaccountname | Select-Object name | Sort-Object name ; if (!$_.member) {Write-Host "***Empty***"}}

    The part I'm trying to fix is when a group is encountered with no members, it writes the following text to the screen: ***Empty***.

    From what I can see in results that come back (and I assume this is the way it's processed through the pipeline) when it reaches the end of the groups member list it sees a null entry and as a result performs the write-host command. My results are currently like this:

    GroupOne
    Member_A
    Member_B
    Member_C
    ***Empty***
    GroupTwo
    Member_B
    Member_F
    Member_G
    ***Empty***
    GroupThree
    ***Empty***
    GroupFour
    Member_H
    Member_I
    Member_J
    ***Empty***

    I was wondering if there is a way to correct this so that only when it encounters a group with no members, does it then execute the write-host command? Essentially giving the following result:

    GroupOne
    Member_A
    Member_B
    Member_C
    GroupTwo
    Member_B
    Member_F
    Member_G
    GroupThree
    ***Empty***
    GroupFour
    Member_H
    Member_I
    Member_J


    Thanks in advance.

    • Moved by Bill_Stewart Monday, March 12, 2018 9:30 PM Abandoned
    Wednesday, February 7, 2018 2:39 AM

Answers

  • Sure looks like a script to me.

    Get-ADGroup -Filter {name -like 'PartOfGroupName*'} -PipeLineVariable grp | 
        Get-ADGroupMember | 
        select @{n='Group';e={$grp.Name }}, Name |
        Sort Group,Name
    


    \_(ツ)_/

    • Marked as answer by Mark_DW Wednesday, March 27, 2019 1:28 AM
    Wednesday, February 7, 2018 3:21 AM

All replies

  • Sure looks like a script to me.

    Get-ADGroup -Filter {name -like 'PartOfGroupName*'} -PipeLineVariable grp | 
        Get-ADGroupMember | 
        select @{n='Group';e={$grp.Name }}, Name |
        Sort Group,Name
    


    \_(ツ)_/

    • Marked as answer by Mark_DW Wednesday, March 27, 2019 1:28 AM
    Wednesday, February 7, 2018 3:21 AM
  • I'll give it a whirl. Thanks!
    Wednesday, February 7, 2018 3:39 AM