6

I'm trying to execute the following command via command line

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  Send-MailMessage -from "mail.me" -to "mail.me" -Subject "PS test !" -Body "Hi this is a power shell mail test" -SmtpServer "mail.domain.com"

But I'm getting the following error :

Send-MailMessage: A positional parameter cannot be found that accepts argument « test ».
Au niveau de ligne : 1 Caractère : 17

  • Send-MailMessage <<<< -from mail.me -to mail.me -Subject PS test ! -Body Hi this is a power shell mail test -SmtpServer mail.domain.com
  • CategoryInfo : InvalidArgument: (:) [Send-MailMessage], ParameterBindingException
  • FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
    .Commands.SendMailMessage

The problem is that the same command works well when directly executed in Power Shell, but without the path.

What could be the problem generating this exception ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mounaim
  • 1,132
  • 7
  • 29
  • 56
  • Isn't `Send-MailMessage` a PowerShell version 2 cmdlet? – Shaneis Jun 05 '17 at 10:54
  • I made sure it's the same path the Power Shell console is pointing to .. – mounaim Jun 05 '17 at 11:06
  • Possible duplicate of [Positional Parameter error in powershell script](https://stackoverflow.com/questions/39407004/positional-parameter-error-in-powershell-script) – Jim G. May 17 '18 at 16:16

2 Answers2

7

Use -commandand braces. Currently, you are launching powershell and passing it multiple arguments instead of asking it to execute your code.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -command "& {Send-MailMessage -from "mail.me" -to "mail.me" -Subject "PS test !" -Body "Hi this is a power shell mail test" -SmtpServer "mail.domain.com"}"

I'm getting errors for the some of values you have set (characters that need to be escaped, invalid emails etc), so replaced and got this to run:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  -Command "& {Send-MailMessage -From test@test.com -To othertest@test.com -Subject "whatevs" -SmtpServer "localhost" -Body "whatevs"}"
G42
  • 9,791
  • 2
  • 19
  • 34
  • I tried this and got ( Send-MailMessage -from mail.me -to mail.me -Subject PS test ! -Body Hi this is a power shell mail test -SmtpServer mail.domain.com ) in output , without the parameters' double quotes, and the command didn't execute – mounaim Jun 05 '17 at 11:06
  • I got it to work by adding the ( "& ) to the begining and replacing the double quotes by singles quotes, what & is useful for here ? – mounaim Jun 05 '17 at 11:55
  • 1
    @mounaim glad to hear it's working. `&` is the call operator - [see this StackOverflow post](https://stackoverflow.com/questions/42382951/whats-the-command-of-call-operator). The [ss64 link](https://ss64.com/ps/call.html) in the accepted answer is also useful - I would recommend script blocks as in their example to avoid issues with multiple commands down the line. – G42 Jun 05 '17 at 12:17
  • gms0ulman'0 Thank you so much, I've been struggling with this to get it to work – mounaim Jun 05 '17 at 12:43
2

You need to escape double quotes in your command. Otherwise command processor strips it, and your "test" appears as parameter to Send-MailMessage.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe  Send-MailMessage -from "mail.me" -to "mail.me" -Subject \"PS test !\" -Body \"Hi this is a power shell mail test\" -SmtpServer "mail.domain.com"
Vesper
  • 18,599
  • 6
  • 39
  • 61