I have a script that sends an email message to users and use splatting to set the send-mailmessage parameters. My code in brief form looks like:
As you can see I have set the -To parameter of the send-mailmessage cmdlet using the $NotifyTo variable prior to actually assigning the variable. When initially testing the script I didn't expect this to work but found that it did. However, when I tried to run this script via Scheduled Tasks no message was sent. I then moved the splat table assignment into the foreach loop below the $NotifyTo assignment and it worked. Can anyone expain why this behavior occurs? Is this a scoping issue or something else I am not seeing? Thanks in advance!$NotifyMessage = @{ SMTPServer = $SMTPServer To = $NotifyTo From = $From Subject = $notifySubject Body = $NotifyBody BodyAsHTML = $true } Get-aduser -filter * -properties * | foreach { $NotifyTo = $_.mail Send-MailMessage @Notifymessage }