1

I need to merge two properties into one column, name of user is in DisplayName while Name of group is stored in Name how ever both types of objects has DisplayName, Name properties so I need to show them in one column.

Suppose Group is 'Test Group'

CN : …
ObjectGUID : 123-456-XXXX
ObjectClass: group
DisplayName: 
Name: 'Test Group'

And 'Test User' Properties are 
CN: …
ObjectGUID : 789-456-XXXX
ObjectClass: user
DisplayName: 'Test User'
Name: 

I have tried using a for each looping but can't figure out the use of select statement.

Get-ADGroupMember -Identity $GroupGUID | 
ForEach-Object{
    if($_.ObjectClass -eq 'User'){
        # process user object
        $_ | Get-ADUser -Properties DisplayName
    }elseif ($_.ObjectClass -eq 'User'){
        # process group
        $_ | Get-ADGroup -Properties Name
    }
} 

The expected output need to be

MemberName          ObjectGUID
----------------    ------------------
Test Group          123-456-XXXX
Test User           789-456-XXXX

2 Answers2

1

I'd use a switch statement to process each item depending on if it is a user or group, and then replace the MemberName property depending on what it is:

$Results = Switch(Get-ADGroupMember -Identity $GroupGUID){
    {$_.ObjectClass -eq 'User'} {$_ | Get-ADUser -Prop DisplayName | Select *,@{l='MemberName';e={$_.DisplayName}} -ExcludeProperty MemberName}
    {$_.ObjectClass -eq 'Group'} {$_ | Get-ADGroup | Select *,@{l='MemberName';e={$_.Name}} -ExcludeProperty MemberName}
}
$Results|Select MemberName,ObjectGUID

Or if you really want it all done in the pipeline you could do this:

Get-ADGroupMember -Identity $GroupGUID -PipelineVariable 'Member' | ForEach{If($Member.ObjectClass -eq 'User'){$_ | Get-ADUser -Properties DisplayName}Else{$_ | Get-ADGroup}} | Select @{l='MemberName';e={If($Member.ObjectClass -eq 'User'){$_.DisplayName}Else{$_.Name}}},ObjectGUID
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
0

i think you are wanting to merge the sources into one output object.

the usual way to add values from multiple sources into one object is to use a [PSCustomObject] to build a ... custom object. [grin] i don't have any AD access, so this is done with local user & group info. you can do some fairly complex structuring by calculating what you want to work with and stuffing it all into one neatly structured object.

here's a demo of the idea using local accounts ...

$GroupName = 'Homonymic_Tuu'

$MemberList = Get-LocalGroupMember -Group $GroupName |
    Where-Object {$_.ObjectClass -eq 'User'}
$GroupInfo = Get-LocalGroup -Name $GroupName

foreach ($ML_Item in $MemberList)
    {
    $UserInfo = Get-LocalUser -Name $ML_Item.Name.Split('\')[-1]
    if ([string]::IsNullOrEmpty($UserInfo.LastLogon))
        {
        $LastLogon = '_Never_'
        }
        else
        {
        $LastLogon = $UserInfo.LastLogon
        }
    [PSCustomObject]@{
        Group_Name = $GroupInfo.Name
        Group_Description = $GroupInfo.Description
        User_Name = $UserInfo.Name
        User_Description = $UserInfo.Description
        User_Enabled = $UserInfo.Enabled
        User_LastLogon = $LastLogon
        }
    }

truncated output ...

Group_Name        : Homonymic_Tuu
Group_Description : Homonym - sounds like Tuu
User_Name         : 22
User_Description  : The Digit 2 Twice
User_Enabled      : True
User_LastLogon    : 2018-11-27 9:19:10 PM

[*...snip...*] 

Group_Name        : Homonymic_Tuu
Group_Description : Homonym - sounds like Tuu
User_Name         : TwoTwo
User_Description  : Repeating the name of the number after One.
User_Enabled      : True
User_LastLogon    : _Never_

the data will also export to a CSV file quite easily. [grin]

Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26