Reputation: 83
I'm trying to send a mail using PowerShell with below command:
powershell -command "& {Send-MailMessage -To "[email protected]" -From "[email protected]" -SMTPServer xxx.xx.com -Subject "report" -Body "service is running"}"
but I'm getting this error:
Send-MailMessage : A positional parameter cannot be found that accepts argument '[email protected]'. At line:1 char:20 + & {Send-MailMessage <<<< -To "[email protected] -From "[email protected] -SMTPServer xxxx.xx.com -Subject "Daily report" -Body "service is running"} + CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SendMailMessage
Upvotes: 0
Views: 1212
Reputation: 200193
As others have already mentioned, your quoting is broken. You're trying to nest double quotes inside a double-quoted string without escaping them. The unescaped nested double quotes prematurely terminate your string, causing the error you observed.
The simplest fix for this problem is to replace the nested double quotes with single quotes, since you don't seem to be using variables in that command anyway:
powershell.exe -Command "& {Send-MailMessage -To '[email protected]' -From ..."
If you want to keep using nested double quotes (e.g. because you have variables in your scriptblock, which wouldn't be expanded in single-quoted strings) you need to escape them. If you're running the command from outside PowerShell (e.g. from CMD) you can do so by using backslashes:
powershell.exe -Command "& {Send-MailMessage -To \"[email protected]\" -From ..."
If you're running the command from within PowerShell you need to escape the nested double quotes twice (once for PowerShell when it's parsing the commandline, and once for the actual command invocation):
powershell.exe -Command "& {Send-MailMessage -To \`"[email protected]\`" -From ..."
However, if you're actually running it from PowerShell you don't need the powershell.exe -Command
in the first place. Just invoke Send-MailMessage
directly:
Send-MailMessage -To "[email protected]" -From ...
Upvotes: 2