Stange behavior when using Get-ADGroups and Get-ADGroupMember in powershell

I am running into a strange issue when using Get-ADGroups and Get-ADGroupMembers

This line works correctly (no issues)

$searchFilter = "OU=Projects,DC=my,DC=lab"

Get-ADGroup -Filter * -Properties * -SearchBase $distinguishedName | Get-ADGroupMember | Where-Object {$_.objectClass -eq "user"} | Get-ADUser | ft Name, SamAccountName, Enabled

However this block does not work

$groups = Get-ADGroup -Filter * -Properties * -SearchBase $distinguishedName 

foreach ($g in $groups) {
    Write-Host "Group:" $g.CN "(" $g.DistinguishedName ")"

        Get-ADGroupMember -Identity $g.DistinguishedName -Debug -Verbose | Where-Object {$_.objectClass -eq "user"} | Get-ADUser | ft Name, SamAccountName, Enabled
}

Returns the following exception

Microsoft.ActiveDirectory.Management.ADException: An operations error occurred ---> System.ServiceModel.FaultException`1[schemas.microsoft.com._2008._1.ActiveDirectory.CustomActions.GetADGroupMemberFault]: Active Directory returned an error processing the operation.

Anyone have any thoughts on why?

Answers


Your second one works for me. Double-check your $distinguishedName value that you are passing to -SearchBase.

I noticed that you define $searchFilter, but you never actually reference it. Is that a typo?


Need Your Help

How to invoke Actions from other Actions? (if that's wise at all)

java swing action actionlistener

I am about to refactor a Swing application from using ActionListeners to Action classes because I realized that a lot of my menu items are going to be used in a toolbar as well.