mjzach22
mjzach22

Reputation: 29

How to pull AD user email then pass it to Function to send email using that ADuser email

Im wanting to Get all users from a security group and pull their email. Once I get their email, I then want to pass that list of email addresses to a function (ideally a for loop) to send a email to all the users in that security group. Any help is appreciated.

$users = Get-ADGroupMember -Identity 'IS-Test-GRP' -Recursive |
  Select-Object -Unique | Get-ADUser -Properties Mail | Select-Object Mail

Foreach($user in $users)
{
Send-MailMessage -From [email protected] -To $user.mail -Subject 'Migrating 
File Share' -Body 'Hello All, 
I will be migrating the share folder <   > from app02 to app03. Once 
completed I will email all users the new file path location to access the 
share. If you will be needing help creating a new shortcut, please contact 
the help desk. Any issues or questions, please let me know.  -Thank you' - 
SmtpServer 'mail.server.com'
}

Upvotes: 0

Views: 521

Answers (1)

Adam
Adam

Reputation: 4168

Try...

$users = $(Get-ADGroupMember -Identity 'IS-Test-GRP' -Recursive | Select- 
Object -Unique | Get-ADUser -Properties Mail).Mail

The only problem I see is $users will be a collection of objects. When you enumerate the collection, I don't see you specifying, which property is/has the Mail.

Upvotes: 1

Related Questions