Don’t do that: Too many parameters in one line is hard to read and to debug:
1 |
Send-MailMessage -From 'sender@domain.com' -To 'recipient01@domain.com' -Bcc 'recipient02@domain.com' -Subject 'Meeting' -Body 'Please find the attached file' -Attachments 'C:\plannings\meeting.xls' -DeliveryNotificationOption OnFailure, OnSuccess -SmtpServer smtp.domain.com -Credential $credentials |
Another “bad” way to is by using backticks (` escape character in Powershell) because it is hard to see and also because a whitespace after a backtick will return an error.
1 2 3 4 5 6 7 8 9 10 |
Send-MailMessage -From 'sender@domain.com'` -To 'recipient01@domain.com'` -Bcc 'recipient02@domain.com'` -Subject 'Meeting'` -Body 'Please find the attached file'` -Attachments 'C:\plannings\meeting.xls'` -DeliveryNotificationOption 'OnFailure,OnSuccess'` -Priority 'High'` -SmtpServer 'smtp.domain.com'` -Credential $credentials |
Do that: My favourite way is to use the Splatting technique (uses a hashtable to pass parameters to a command).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$parameters = @{ From = 'sender@domain.com' To = 'recipient01@domain.com' Bcc = 'recipient02@domain.com' Subject = 'Meeting' Body = 'Please find the attached file' Attachments = 'C:\plannings\meeting.xls' Dno = 'OnFailure,OnSuccess' Priority = 'High' SmtpServer = 'smtp.domain.com' } Send-MailMessage @parameters |