Exporting Nested Group with individual group names & membership

Welcome Forums General PowerShell Q&A Exporting Nested Group with individual group names & membership

Viewing 3 reply threads
  • Author
    Posts
    • #215067
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Hi,

      First, I hope everyone who is reading this is healthy and being able to cope with this as best as possible.

      This is my first post and I wanted to thank in advance for all your help!

      I am really trying to figure this out and have tried multiple different scripts and variations, yet I am sure that I am missing something simple.

      Basically, I would like to get the membership of a nested group and all its nested group membership but I would like to have the groups to which the user belongs to.

      so report should include group name, group samaccountname, user display name, user samaccountname, user email.

      I found the script below but it throws an “identity” error.

       

      $date = Get-Date -UFormat '%Y%m%d'
      $groups = Import-Csv c:\temp\groups.csv
      $results = ForEach ($group in $groups) {
      $users = Get-ADGroupMember $groups | Select-Object -ExpandProperty SamAccountName
      ForEach ($user in $users){
      New-Object -TypeName PSObject -Property @{
      'Group'=$group.name
      'User'=$user
      }
      }
      }
      $results | Sort-Object 'Group' | Export-Csv -NoTypeInformation -Path c:\temp\testone-$date.csv
      

      The error is the following:

      Get-ADGroupMember : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported.
      

       

      Any help would be greatly appreciated, I am just really surprised I have not found something like this or that I would easily be able to modify.  I just keep running into errors and the last time I worked on PowerShell it was when the Quest CMDlets were out 🙁

      Thank you in advance!

       

    • #215076
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      That has been written many times. A search “Powershell nested group recursion” has many blogs, scripts, etc. such as:

      Recursive list of group members in AD

    • #215079
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      You should start with (re-)reading the instructions/help in the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!
      Then you should go back and fix your post by formatting your code as code using the code tags “PRE“. When you post error messages or console output or sample data you should format this as code as well, please.
      Now you can change this line of code:

      $users = Get-ADGroupMember $groups | Select-Object -ExpandProperty SamAccountName

      to this:

      $users = Get-ADGroupMember $group | Select-Object -ExpandProperty SamAccountName

      … and your code should run without error. 😉

    • #215106
      Participant
      Topics: 1
      Replies: 1
      Points: 16
      Rank: Member

      Thank you for the advice to both of you, I will make sure to edit as necessary.

      @olaf  I tried the suggestion above earlier and I still get the identity error.

      • This reply was modified 1 month, 4 weeks ago by doubolplay183.
      • This reply was modified 1 month, 4 weeks ago by doubolplay183. Reason: Replying to Olaf
Viewing 3 reply threads
  • You must be logged in to reply to this topic.