Powershell foreach (list the array item command referenced)

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
...

Upvotes: 3

Views: 9774

Answers (3)

veefu
veefu

Reputation: 2890

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}                         

Upvotes: 2

John Seerden
John Seerden

Reputation: 166

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"
    }
}

Upvotes: 0

Maximilian Burszley
Maximilian Burszley

Reputation: 19684

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
}

Upvotes: 7

Related Questions