我正在尝试通过命令行执行以下命令
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"
但我收到以下错误:
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
问题是在Power Shell中直接执行时,相同的命令运行良好,但没有路径 生成此异常可能是什么问题?
答案 0 :(得分:5)
使用-command
和大括号。目前,您正在启动powershell并传递多个参数,而不是要求它执行您的代码。
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"}"
我为你设置的一些值(需要转义的字符,无效的电子邮件等)收到错误,所以替换并让它运行:
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"}"
答案 1 :(得分:2)
您需要在命令中转义双引号。否则命令处理器将其剥离,并且“test”将显示为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"