locked
Odd behavior with Get-ADGroupMember in script RRS feed

  • Question

  • Hello everyone,

    I have run into a bit of very repeatable (at least in the environment here at work) behavior in a little script I threw together and have not been able to find any articles discussing it, nor have I been able to figure out why it is behaving this way, so I wanted to share it here and see if anyone might know?

    ---------------------------------------------------------
    $groupList = 'Foo_AD_Group1,Foo_AD_Group2,Foo_AD_Group3,Foo_AD_Group4,Foo_AD_Group5'.Split(",");
    #$groupList = @("Foo_AD_Group1","Foo_AD_Group2","Foo_AD_Group3","Foo_AD_Group4","Foo_AD_Group5");
    Clear-Host;

    ForEach ($group in $groupList){
        Write-Host $group;
        Get-ADGroupMember -Identity $group | Select name;
        Write-Host "`r`n";
    }
    ---------------------------------------------------------

    The behavior I am getting is that the first/[0] element in the $groupList array is always returning no results.  My experiments have shown the following:

    1. It is always $groupList[0] (or the first element in the list when I try to use a comma delimited scheme) that returns empty.

    2. If I stop the debugger at the Get-ADGroupMember step and manually run the command against the [0] element, it returns a proper value.  Then, when I step over, it returns nothing.

    3. The other [1]+ indexes always return proper values, despite order changes and index additions/deletions.

    4. I have attempted both a full "For" loop and the "ForEach" that is in the example.  They both fail in the same way.

    5. When I copy the contents of the first array into a second array, then Write-Host on the second array, it works (this is the code that works)  --  Note: There are several formatting additions to this bit that I don't believe are relevant to the behavior, but am including them for completeness.

    ---------------------------------------------------------
    $groupList = 'Foo_AD_Group1,Foo_AD_Group2,Foo_AD_Group3,Foo_AD_Group4,Foo_AD_Group5'.Split(",");
    $outputGroup = @(0) * $groupList.Length;

    for($i=0; $i -lt $groupList.Length; $i++){
        Write-Host $groupList[$i];

        $outputGroup[$i] = Get-ADGroupMember -Identity $groupList[$i] | Select -expand name;
        $outputGroupBreakout = @($outputGroup[$i]);

        for ($j=0; $j -lt $outputGroupBreakout.Length; $j++){
            
            if ($j -eq 0){
                $outputGroupBreakout[$j] = " " + $outputGroupBreakout[$j] + "`r`n";
            }
            else{
                $outputGroupBreakout[$j] += "`r`n";
            }
        }

        Write-Host $outputGroupBreakout;
        Write-Host "`r`n";
    }
    ---------------------------------------------------------

    I am hoping the behavior repeats in other environments and that someone out there might be able to explain why it is doing this.  Thank you for your time!
    Thursday, January 25, 2018 4:32 PM

All replies

  • Your group list is wrong.  It must be an array of strings.

    $groupList = 'Foo_AD_Group1','Foo_AD_Group2','Foo_AD_Group3','Foo_AD_Group4','Foo_AD_Group5'
    ForEach ($group in $groupList) {
    	Get-ADGroupMember -Identity $group | Select name
    }

    Do not use CLS and do not waste time with semicolons.  Adding Write-Host and standard output will create havoc on the display.


    \_(ツ)_/

    Thursday, January 25, 2018 4:43 PM