3

I've search high and low with no luck.
Using a foreach against an array, after running a command against each of the array items I am not sure which array item the results are from.

I've attempted many variations. This code is the best example of what I have attempted.

$ADGroupVar = @(
'ADGroup1'
'ADGroup2'
'ADGroup3'
);

foreach ($ADgrouplist in $ADGroupVar)
{
Write-Host $ADgrouplist
Get-ADGroupMember ($ADgrouplist) | select name
}

ADGroup1
ADGroup2
ADGroup3

Computer5
Computer10
Computer2
Computer4
Computer20
Computer3
Computer1

Desired results are:

ADGroup1
Computer5
Computer10
ADGroup2
Computer2
Computer4
Computer20
ADGroup3
Computer3
Computer1

or

Computer5 ADGroup1
Computer10 ADGroup1
...

0

3 Answers 3

8

Stop using Write-Host. You cannot rely on its output, especially when mixed with stdout. This example should exemplify what you're trying to accomplish:

$adGroupList = 'ADGroup1', 'ADGroup2', 'ADGroup3'

foreach ($group in $adGroupList)
{
    $group
    (Get-ADGroupMember $group).Name
}
Sign up to request clarification or add additional context in comments.

Comments

3

You don't really want what you're asking for. A series of strings, some group names and some member names, where names following a group name are members is a very brittle system.

How can you distinguish group names from member names? If you change the order, by say, sorting, the group membership is lost. Working with the data in this way will be difficult later on.

You may consider using a hashtable instead:

$ADGroupVar = @(
    'ADGroup1'
    'ADGroup2'
    'ADGroup3'
);

$results = @{}
foreach ($ADgroup in $ADGroupVar)
{
    $results[$ADgroup] = Get-ADGroupMember $ADgroup | select name
}

Write-Output $results

This associates each group name with list of user names. Access them with the subscript operator [], for example:

$results['ADGroup2']

computer2
computer4
computer20

and the whole hashtable contents are displayed this way:

$results

Name                           Value                                           
----                           -----                                           
ADGroup2                       {computer2, computer4, computer20}              
ADGroup3                       {computer3, computer1}                          
ADGroup1                       {computer5, computer10}                         

Comments

0

To achieve the other 'Desired results'

Computer5 ADGroup1

Computer10 ADGroup1

You can use:

'ADGroup1', 'ADGroup2', 'ADGroup3' | ForEach-Object {
    $GroupName = $_
    Get-ADGroupMember $_ | ForEach-Object {
        "$($_.Name) $GroupName"
    }
}

or

$adGroupList = 'ADGroup1', 'ADGroup2', 'ADGroup3'
$adGroupList | ForEach-Object {
    $GroupName = $_
    Get-ADGroupMember $_ | ForEach-Object {
        "$($_.Name) $GroupName"
    }
}

or

$adGroupList = 'ADGroup1', 'ADGroup2', 'ADGroup3'
foreach ($adGroup in $adGroupList) {
    Get-ADGroupMember $adGroup | ForEach-Object {
        "$($_.Name) $adGroup"
    }
}

or

$adGroupList = 'ADGroup1', 'ADGroup2', 'ADGroup3'
foreach ($adGroup in $adGroupList) {
    $adGroupMembers = (Get-ADGroupMember $adGroup).Name
    foreach ($adGroupMember in $adGroupMembers) {
        "$adGroupMember $adGroup"
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.