none
How to list users under multiple groups and users sub groups RRS feed

  • Question

  • Hi, I am stump, which is not hard to do. i have a list of groups and i want to list the users in those groups and then in the next column lists all the citrix only groups for each user. hopefully im describing that correctly. Heres what i have but it is not listing the users groups. I am not sure how to proceed.

    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')
    
    $Groupname = "Distribution Lists"
    
    $excel = New-Object -comobject Excel.Application
    $excel.visible = $True
    $wbook = $excel.Workbooks.Add()
    
    $wsheet = $wbook.Worksheets.Item(1)
    $wsheet.Cells.Item(1,1) = "Groupname"
    $wsheet.Cells.Item(1,2) = "Member"
    $wsheet.Cells.Item(1,3) = "ACID"
    $wsheet.Cells.Item(1,4) = "Department"
    
    $range = $wsheet.UsedRange
    $range.Interior.ColorIndex = 19
    $range.Font.ColorIndex = 11
    $range.Font.Bold = $True
    $intRow = 2
    
    
    
    $groups = get-adgroup -Filter * -properties * -Searchbase "OU=Citrix,OU=Permission,OU=Groups,OU=Home Office,OU=domain,DC=Domain,DC=com"
    $targetFile = "c:\temp\$groupname $CurrentDate.csv"
    Add-Content $targetFile "Group;Member;ACID;Department"
    
    
    foreach ($group in $groups){
           $groupMembers = get-adgroupmember $group -Recursive | Get-ADUser -Properties Department, DistinguishedName| Where-Object { $_.Enabled -eq 'True' } | Select-Object Name, samaccountname, department, distinguishedname, @{n='MemberOf';e={$_.MemberOf -replace '^cn=([^,]+).+$','$1' -join '; '} 
           
          
           foreach ($groupMember in $groupMembers){
                  
                 
                  
    	      $groupName = $group.Name
                  $memberName = $groupMember.Name
                  $acid = $groupMember.samaccountname
    	      $groups = $usergroups
              #$department = $groupMember.department
                  $department = $groupMember.memberof
                  #$DistinguishedName = $gropmember.distinguishedname
    	      $line = "($groupName)--------($memberName)-----($acid)-------($department)------($usergroups)"
    	      add-content $targetFile $line
    
    	      $wsheet.Cells.Item($intRow,1) = $groupName
    	      $wsheet.Cells.Item($intRow,2) = $memberName
    	      $wsheet.Cells.Item($intRow,3) = $acid
    	      $wsheet.Cells.Item($intRow,4) = $groups
    	      $wsheet.Cells.Item($intRow,5) = $DistinguishedName
    	      $intRow++
    }
    }
    $WorkBook.EntureColumn.AutoFit()
    
    $excel.SaveAs("DL" + "name.xlsx") 
    
    $excel.Close()
    

    Tuesday, April 28, 2015 6:04 PM

Answers

  • I think it's unlikely you will find a script that meets your exact specifications.

    You will need to read the PowerShell documentation and understand how it uses the pipeline and processes objects.

    Unfortunately there is no substitute for learning the basics. There is a good Learn link at the top of this forum to help you get started.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 29, 2015 12:42 PM
    Moderator

All replies

  • Sorry, but your question doesn't make any sense.

    I would add that you need to work out exactly what information you are seeking before you start trying to do anything in Excel. That only adds unnecessary complexity into the situation.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 28, 2015 6:07 PM
    Moderator
  • Sorry, but your question doesn't make any sense.

    I would add that you need to work out exactly what information you are seeking before you start trying to do anything in Excel. That only adds unnecessary complexity into the situation.


    -- Bill Stewart [Bill_Stewart]

    Thats my fault. i thought it might not make sense but im not sure how to describe it. i am trying to output in the following format. I can get the first 2 columns to work not the third which is listing on the citrix groupmemberships seperated by ";"

    Groupname| User| citrix groups user is in

    group1 | user1 | citrix-app1; citrix-app2; citrix-app3

    group1 | user2 | citrix-app1; citrix-app2; citrix-app3

    group1 | user3 | citrix-app1; citrix-app2; citrix-app3

    group2 | user1 | citrix-app1; citrix-app2; citrix-app3

    group2 | user2 | citrix-app1; citrix-app2; citrix-app3

    group3 | user1 | citrix-app1; citrix-app2; citrix-app3

    group3 | user2 | citrix-app1; citrix-app2; citrix-app3

    group3 | user3 | citrix-app1; citrix-app2; citrix-app3


    • Edited by glacket Tuesday, April 28, 2015 7:17 PM
    Tuesday, April 28, 2015 7:15 PM
  • What is the purpose of this report?

    Keep in mind that this forum is for asking specific scripting questions rather than for requesting scripts to spec.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, April 28, 2015 7:18 PM
    Moderator
  • I understand, the purpose of the report is to  list what citrix groups users have by field office groups. at the basic level i am trying to list all users in a group and all the groups that user is in. I tried to pipe get-adgroupmember to get-aduser to get-adprincipalgroupmembership but that did not work.
    Tuesday, April 28, 2015 8:17 PM
  • I'm not sure what you need exactly.

    I would recommend searching the web and the repository for possible examples.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, April 28, 2015 8:27 PM
    Moderator
  • that's what i have been doing. i will keep searching. Thanks.
    Wednesday, April 29, 2015 12:09 PM
  • I think it's unlikely you will find a script that meets your exact specifications.

    You will need to read the PowerShell documentation and understand how it uses the pipeline and processes objects.

    Unfortunately there is no substitute for learning the basics. There is a good Learn link at the top of this forum to help you get started.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 29, 2015 12:42 PM
    Moderator
  • Hi glacket,

    First of all you are already complicating things by making the output to excel.

    You should initially figure out what gives you the correct output using simplest possible code, not necessarily in proper format.

    Once you have that, then you can go ahead and format Or maybe we can help you in that.

    How about you come up with that.

    If I have understood your requirement correct its as below:

    You have groups Group1, Group2, ..... GroupN

    The Members of the Group are various combination of User1, User2,..... UserY

    Now you have CitrixGroups1, ........ N, with again the same set of users as members.

    You are trying to find out the Users who are member of both Groups and list them.

    The output format you have is inefficient as Eg.

    You have already listed user1 in:

    group1 | user1 | citrix-app1; citrix-app2; citrix-app3

    Then again you are listing the same user1 in:

    group2 | user1 | citrix-app1; citrix-app2; citrix-app3


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Wednesday, April 29, 2015 12:48 PM
  • Hi Glacket,

    Below codes should give you headsup.

    This command will give you estimate the result count for each group:

    PS C:\Users\Administrator> Get-ADGroup -Filter {Name -like "TestGroup*"} | Select Name, @{Expression={get-adgroupmember $_  -recursive | Measure | Select -ExpandProperty Count};Label="Count"}
    
    Name                           Count
    ----                           -----
    TestGroup1                     7
    TestGroup2                     8

    Note that as said earlier we are getting duplicated results(12,13,14) for users belonging to multiple groups. Result is in order as per earlier code's count and order.

    Get-ADGroup -Filter {Name -like "TestGroup*"} | get-adgroupmember  -recursive | Select Name,@{Expression={Get-ADPrincipalGroupMembership $_ | Select -ExpandProperty Name};Label="GroupMemberOfName"}
    
    Name                                                        GroupMemberOfName
    ----                                                        -----------------
    User100                                                     {Domain Users, TestGroup1}
    User14                                                      {Domain Users, TestGroup1, TestGroup2}
    User13                                                      {Domain Users, TestGroup1, TestGroup2}
    User12                                                      {Domain Users, TestGroup1, TestGroup2}
    User11                                                      {Domain Users, TestGroup1}
    User10                                                      {Domain Users, TestGroup1}
    User1                                                       {Domain Users, TestGroup1}
    User19                                                      {Domain Users, TestGroup2}
    User18                                                      {Domain Users, TestGroup2}
    User17                                                      {Domain Users, TestGroup2}
    User16                                                      {Domain Users, TestGroup2}
    User15                                                      {Domain Users, TestGroup2}
    User14                                                      {Domain Users, TestGroup1, TestGroup2}
    User13                                                      {Domain Users, TestGroup1, TestGroup2}
    User12                                                      {Domain Users, TestGroup1, TestGroup2}

    Use below to export to CSV:

    Get-ADGroup -Filter {Name -like "TestGroup*"} | get-adgroupmember  -recursive | Select Name,@{Expression={Get-ADPrincipalGroupMembership $_ | Select -ExpandProperty Name};Label="GroupMemberOfName"} | Export-Csv C:\ListGroups.csv


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Wednesday, April 29, 2015 5:29 PM
  • Hi Glacket,

    Any Updates.


    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Monday, May 4, 2015 11:53 AM