0
votes

So I am trying to query a group in AD, then use that list to create a list of email addresses. I am using the below command:

Get-ADGroupMember mygroup -Server my.server.com | Select-Object SID | Get-ADUser -Identity $_.SID -Properties cn,mail -server my.server.com | Select-Object Samaccountname,mail | Format-Table Samaccountname,mail

when I run this I get the below error.

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument collection contains a null value.

At line:1 char:107

  • ... User -Identity $_.SID -Properties cn,mail -server na.ko.com | Select-Object Sama ...

    • CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser

Anyone feel like teaching a newb?

1
You should be able to skip both of your Select commands, and you need to wrap your Get-ADUser command in a ForEach{Get-ADUser ....}TheMadTechnician
Thank you @TheMadTechnician. I knew it was something simple. Silly ForEach-s.Chris
@TheMadTechnician you should post this as the answer so I can select it.Chris

1 Answers

0
votes

To make it an answer, you can skip the Select statements and run the Get-ADUSer through a ForEach loop:

Get-ADGroupMember mygroup -Server my.server.com | ForEach{ Get-ADUser -Identity $_.SID -Properties cn,mail -server my.server.com | Format-Table Samaccountname,mail