powershelladgroup

Get-ADGroup with filter isn't working for all


I got a problem with my powershell script, I need to get all users from group, I have the group id which I can use to get the group. The problem I have is that my solution isn't working for all group and I don't get what is wrong.

I have some group name eAM
eGR
eTE
eDF
eMP-arts
e-CV

The 3 first isn't working and the other one yes. Here the script I use

$like = "*" + $branch
foreach ($member in (Get-ADGroupMember (Get-ADGroup -filter {name -like $like}))){
    # Do something
}

And the error I get for which isn't working
Get-ADGroupMember : can't convert «System.Object[]» in «Microsoft.ActiveDirectory.Management.ADGroup», requiered by «Identity» param. The specified method is not supported + Get-ADGroupMember (Get-ADGroup -filter {name -like "*AM"}) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument : (:) [Get-ADGroupMember], ParameterBindingException + FullyQualifiedErrorId : cannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember

Thanks in advance for your help, MYT


Solution

  • The error thells you that Get-ADGroupMember does not accept an array of groups, but a single group. You'd also get errors if Get-AdGroup would not return any results. Instead, pipe the commands:

    Get-ADGroup -Filter {name -like $like} | Get-ADGroupMember | Foreach-Object {
    }
    

    Please note that accounts can belong to multiple groups so same member could be returned multiple times.