Domanda

I'm trying to script something where I can get the email address of all users in an active directory security group.

What I have so far:

$Groups = Get-ADGroup -filter {Name -like "VIPEmail" } | Select-Object Name
ForEach ($Group in $Groups) {
  Get-ADGroupMember -identity $($group.name) -recursive | Select-Object samaccountname
}

Obviously this will only return the samaccountname, which it does. I replace samaccountname with EmailAddress, and it does nothing.

È stato utile?

Soluzione

To keep with your original formatting so you can see where you went wrong:

$Groups = Get-ADGroup -filter {Name -like "09-Admins" } | Select-Object Name
ForEach ($Group in $Groups) {
   Get-ADGroupMember -identity $($group.name) -recursive | Get-ADUser -Properties mail | Select-Object mail
}

The problem is that you are trying to read a property that the Get-ADGroupMember return type doesn't have (Microsoft.ActiveDirectory.Management.ADPrincipal). You need to pipe that return into Get-ADuser AND specify that you want it to pull the emailaddress property. Get-ADUser will not pull most properties of a user by default, so you need to specify any additional properties you would like to retrieve (or just select all of them with "-Properties *", but that's kind of sloppy).

Mjolinor beat me to the answer, but I figured it was worth elaborating a little on his response.

Altri suggerimenti

Something like this:'

Get-ADGroup -filter {name -like 'VIPEmail'} | 
Get-ADGroupMember -Recursive |
Get-ADUser -Properties Mail |
select -ExpandProperty Mail

I'd add unique. So when a user is in more groups only one email is shown:

Get-ADGroup -filter {Name -like "RO*" } | 
Get-ADGroupMember -Recursive | 
select -Unique| 
get-adUser -Properties mail |
select -ExpandProperty Mail
Get-ADGroupMember GroupName | get-aduser -properties mail | select mail | Sort-Object mail

If you have groups nested you can change the above to:

Get-ADGroupMember -Recursive GroupName | get-aduser -properties mail | select mail | Sort-Object mail

I went with:

Get-ADGroupMember -Identity your_ad_group_name_here -Recursive | Get-ADUser -Properties *  | Select SamAccountName,mail,EmailAddress,Manager

...just make sure you've installed the necessary powershell module so you have access to Get-ADGroupMember. You can download the WindowsTH-RSAT_WS_* specific to your version of Windows 10 here - https://www.microsoft.com/en-us/download/details.aspx?id=45520.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top