Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Find groups of groupmembers

$
0
0

Hi, 

 

First post here and a non-english native speeking, so pardon my bad spelling and I'm new to powershell so please point out the misstakes I've made.

My objective is to get all the group members of a group, called Group1 in the code, and then see what groups, called Group2 in the script, that they are members of. 

The initial script works and a out-gridview shows the groupmembership but my problem starts with exporting the results to CSV.

------------------------------ SCRIPT START -----------------------------------

$ou = "OU=Groups,DC=domain,DC=local"

$ProcGroup = Get-ADGroup -filter {cn -like "*Group1*"} -SearchBase $ou

$ProcMembers = @()

$ProcObject = @()

 

 

foreach ($Group in $ProcGroup){

        $PROCADmember = Get-ADGroupMember -Identity $Group

        foreach ($member in $PROCADmember)

                {

        $dnsmember = ((Get-ADUser $member -Properties memberof | select -ExpandProperty memberOf)  -like "*group2*")

        $ProcObject = New-Object psobject -Property @{

            "Proc Group" = $Group | ForEach-Object {$_}

            "User" = $member.name | ForEach-Object {$_}

            "DNS Group" = $dnsmember | ForEach-Object {$_} 

                            }

                $ProcMembers += $ProcObject

                }

        }

------------------------------ SCRIPT END -----------------------------------

If a user in i.e Sales-Group1-North is a member of Marketing-Group2-North then the output is as expected, but if  a user in Sales-Group1-North is a member of Marketing-Group2-North and AfterSales-Group2-North all I get is System.Object[].

My expectations of the output would be something similar to:

Proc Group                           User                 DNS Group
CN=Sales-Group1-North..      John Doe          CN=AfterSales-Group2-North..., CN=Marketing-Group2-North..

CN=Sales-Group1-North..      Jane Doe          CN=..Marketing-Group2-North...

But instead I get the following output

Proc Group                         User                 DNS Group
CN=Sales-Group1-North..      John Doe          System.Object[]

CN=Sales-Group1-North..      Jane Doe          CN=Marketing-Group2-North...

 

I hope that you can understand my ramblings :)

 

/Joseph


Viewing all articles
Browse latest Browse all 6937

Trending Articles