I need to send a number of emails with addresses captured via Get-ADUser. But I would need to grab a number of fields from the cmdlet to personalize the mail.
Basically I’d like to add the outputs from Select-Object to a $UserInfo splat and then call my Send-ReturnMail function with the splat as a parameter.
The Send-ReturnMail function uses UserPrincipalName as mail address, GivenName and SurName are used in the mail body for personalization and Manager is grabbed to send a cc to the manager of the user.
Foreach ($UserName in $UserNameArray) {
$ADUser = Get-ADUser -Server [SERVERNAME] -Identity [USERNAME] -Properties Manager
$SendMailMessageParams = @{
from = 'sender'
to = $ADUser.mail
subject = "Mail regarding $ADUser.Manager"
body = 'a lot of very important text'
smtpserver = mail.server.se
}
Send-MailMessage @SendMailMessageParams
}
It still sounds like you need to manipulate the hash before passing it to Send-MailMessage. The $PSBoundParameters is a hash table that you can add and remove and manipulate, so you could remove FrrstName and LastName, build the body and add it to $PSBoundParameters:
Had a lot of help in another thread that tied into this, so there’s code here that’s not directly related to this question, but I thought it was better to present it in context. Basically it went from a splat to using [PSCustomObject].
Here’s the (non-final) solution… Will still need to test it all properly.