Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: A positional parameter cannot be found that accepts argument “test”

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 arguement  « 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], Paramet
   erBindingException
    + 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 ?

like image 357
mounaim Avatar asked Jun 05 '17 10:06

mounaim


1 Answers

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 [email protected] -To [email protected] -Subject "whatevs" -SmtpServer "localhost" -Body "whatevs"}"
like image 137
G42 Avatar answered Sep 23 '22 16:09

G42