Powershell: Export group members from external domain

755 Views Asked by At

I want to export users of some large groups. The groups are filled with other groups and the members of those groups are users from a trusted external domain.

When I run this script if gives an error:

$Users = Get-ADGroupMember -Identity 'Group' -recursive |
  Where {$_.ObjectClass -eq 'User'} |
  Get-ADUser -Properties SamAccountName |
  Select-Object SamAccountName

Error: The operation being requested was not performed because the user has not been authenticated.

And that's the other domain that requests authentication. How can I achieve this in the script?

Thanks

1

There are 1 best solutions below

1
On

Whenever you run an AD group cmdlet, it uses your logged-in credentials to query Active Directory. This says you need to be on a domain joined computer logged in as an AD user that has permission to query.

You are on a workgroup computer or need to authenticate to AD as a different user. Then you need to provide credentials. Like other ps cmdlets, Get-ADGroupMember has a -Ceedential parameter and This parameter allows you to specify a username and password to use for the authentication.

This will show a dialog to prompt you for your credentials:

$Users = Get-ADGroupMember -Identity 'Group' -recursive -Credential (Get-Credential) | Where {$_.ObjectClass -eq 'User'} | Get-ADUser -Properties SamAccountName | Select-Object SamAccountName

Or you can specify credentials:

$cred = New-object System.Management.Automation.Pscredential User, Password

AND -Credential $cred