Forum Discussion

TJCooper440's avatar
TJCooper440
Copper Contributor
Aug 26, 2024

Query OU - Get Groups - Get Group Members - Export to CSV

The AI result did not work I am getting an error. The error is "Get-ADGroupMember: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input."

 

Import-Module ActiveDirectory

# Specify the OU where your groups are located
$SearchBase = "CN=test,DC=test,DC=LOC"

# Get all groups within the specified OU
$Groups = Get-ADGroup -Filter * -Properties * -SearchBase $SearchBase

# Initialize an empty array to store group members
$GroupMembers = @()

foreach ($Group in $Groups) {
$Members = $Group | Get-ADGroupMember
foreach ($Member in $Members) {
$Info = New-Object psObject
$Info | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $Group.Name
$Info | Add-Member -MemberType NoteProperty -Name "Description" -Value $Group.Description
$Info | Add-Member -MemberType NoteProperty -Name "Member" -Value $Member.Name
$GroupMembers += $Info
}
}

# Export the results to a CSV file
$GroupMembers | Sort-Object GroupName | Export-CSV C:\temp\group_members.csv -NoTypeInformation

 

 

  • TJCooper440 I think AI thinks that the #SearchBase starts in a Container (CN), but that's not the case, I guess. Should be $SearchBase = "Ou=Corp,DC=test,DC=local". I rewrote the script and tested it, should work like this:

     

     

    Import-Module ActiveDirectory
    
    # Specify the OU where your groups are located
    $SearchBase = "Ou=Test,DC=test,DC=loc"
    
    # Get all groups within the specified OU
    $Groups = Get-ADGroup -Filter * -SearchBase $SearchBase
    
    # Initialize $GroupMembers and loop through the groups and add the results to $groupmembers while running
    $GroupMembers = foreach ($Group in $Groups) {
        $Members = $Group | Get-ADGroupMember
        foreach ($Member in $Members) {
            [PSCustomObject]@{
                "GroupName"   = $Group.Name
                "Description" = $Group.Description
                "Member"      = $Member.Name
            }
        }
    }
    
    # Export the results to a CSV file
    $GroupMembers | Sort-Object GroupName | Export-CSV C:\temp\group_members.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8

     

     

    I removed the -Properties * part, too. It is unnecessary to retrieve all groups' properties when querying members... Changed to Foreach loop to be the $GroupMembers variable and used a PSCustomObject instead of adding members and using += to add $info to the $GroupMembers variable

  • TJCooper440 I think AI thinks that the #SearchBase starts in a Container (CN), but that's not the case, I guess. Should be $SearchBase = "Ou=Corp,DC=test,DC=local". I rewrote the script and tested it, should work like this:

     

     

    Import-Module ActiveDirectory
    
    # Specify the OU where your groups are located
    $SearchBase = "Ou=Test,DC=test,DC=loc"
    
    # Get all groups within the specified OU
    $Groups = Get-ADGroup -Filter * -SearchBase $SearchBase
    
    # Initialize $GroupMembers and loop through the groups and add the results to $groupmembers while running
    $GroupMembers = foreach ($Group in $Groups) {
        $Members = $Group | Get-ADGroupMember
        foreach ($Member in $Members) {
            [PSCustomObject]@{
                "GroupName"   = $Group.Name
                "Description" = $Group.Description
                "Member"      = $Member.Name
            }
        }
    }
    
    # Export the results to a CSV file
    $GroupMembers | Sort-Object GroupName | Export-CSV C:\temp\group_members.csv -NoTypeInformation -Delimiter ';' -Encoding UTF8

     

     

    I removed the -Properties * part, too. It is unnecessary to retrieve all groups' properties when querying members... Changed to Foreach loop to be the $GroupMembers variable and used a PSCustomObject instead of adding members and using += to add $info to the $GroupMembers variable

    • TJCooper440's avatar
      TJCooper440
      Copper Contributor

      Harm_Veenstra 

      I am sorry for getting back to you so late. It was a container I was querying. I tried your code with an OU and get an error at this point.

      PS C:\Users\sa-coopert> $GroupMembers = foreach ($Group in $Groups) {
      >>     $Members = $Group | Get-ADGroupMember
      >>     foreach ($Member in $Members) {
      >>         [PSCustomObject]@{
      >>             "GroupName"   = $Group.Name
      >>             "Description" = $Group.Description
      >>             "Member"      = $Member.Name
      >>         }
      >>     }
      >> }
      Get-ADGroupMember: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
      Get-ADGroupMember: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
      • Harm_Veenstra's avatar
        Harm_Veenstra
        MVP
        What does $groups give you after running it? Looks like it couldn't find groups in the specified OU, is the syntax ok?

Resources