Postie只发送第一个消息

时间:2015-07-09 15:17:45

标签: powershell

我将我的批处理脚本转换为PowerShell,除了通过Postie.exe发送确认电子邮件的最后一行之外,我已完成所有操作。

$subject = "Kiva Extract"
$msg = "Kiva_Extract has been completed for July."

& "C:\cmucwin\postie.exe" -host:myServer -from:$env:username@myDomain -to:me@myDomain -s:$subject -msg:$msg

这在CMD中运行良好,当然我只需要做很少的修改就可以转换为PowerShell。现在它可以工作,但它只发送消息和主题的第一个单词。我收到的电子邮件:

  

主题:Kiva
  消息:Kiva_Extract

如果我输出$subject$msg变量,那么它们是正确的。我做错了什么?

这是我的批处理脚本中使用的行

postie -host:myServer -from:%USERNAME%@myDomain -to:%dbaManager% -s:"Kiva Extract" -msg:"Kiva_Extract has been completed for %curmonth%."

这很好用。这是我试图开始工作的路线。我已经尝试过没有变量但它仍然无法工作。我认为这要么与“空间”有关。被视为EOF或'回车'字样的字符。或者它如何解析并且没有读取括号权利。我使用`来保持字符串中的引号,这也没有帮助。

$date = Get-Date -format Y
$subject = "Kiva Extract"
$msg = "Kiva_Extract has been completed for $date"

& "C:\cmucwin\postie.exe" -host:myServer -from:$env:username@myDomain -to:myEmail -s:$subject -msg:$msg

这是我发现的工作

& C:\cmucwin\postie.exe -host:$hostServer -from:$from -to:$to --% -s:"Kiva Extract" -msg:"Kiva_Extract has been completed for the month"

我不得不使用 - %来停止解析。显然PowerShell正在为这些论点做点什么。我现在唯一的问题是我想在消息中使用变量,但现在我不能,因为它不会解析它。有谁知道我怎么能绕过这个。

5 个答案:

答案 0 :(得分:0)

怀疑你需要用引号包装邮件。试试这个:

$subject = "Kiva Extract"
$msg = '"Kiva_Extract has been completed for July."'

& "C:\cmucwin\postie.exe" -host:myServer -from:$env:username@myDomain -to:me@myDomain -s:$subject -msg:$msg

或者这个:

$serverhost = "-host:myServer"
$from = "-from:$env:username@myDomain"
$to = "-to:me@myDomain"
$subj = '"-s:Kiva Extract"'
$msg = '-msg:"Kiva_Extract has been completed for July."'

& "C:\cmucwin\postie.exe" $serverhost $from $to $subj $msg

答案 1 :(得分:0)

您是否尝试过使用Invoke-Command和scriptblock?

这是我用于安装Google Chrome的内容。

Invoke-Command -ComputerName $pc -ScriptBlock { & cmd /c "msiexec.exe /i c:\temp\chrome\Bradley\googlechromestandaloneenterprise64.msi" }

答案 2 :(得分:0)

你必须使用postie吗?

send-mailmessage -from "User01 <user01@example.com>" -to "User02 <user02@example.com>", "User03 <user03@example.com>" -subject "Sending the Attachment" -body "Forgot to send the attachment. Sending now." -Attachments "data.csv" -priority High -dno onSuccess, onFailure -smtpServer smtp.fabrikam.com

我用我自己的设置测试它并且它可以工作:)

答案 3 :(得分:0)

在这种情况下,来自PSCX

PHP Fatal error: require(): Failed opening required '/home/y/public_html/x/index2.php' (include_path='.:/usr/lib/php:/usr/local/lib/php:/usr/local/php54/lib/php') in /home/y/public_html/x/en/index.php on line 30 实用程序非常方便。

如果我这样做:

echoargs

我明白了:

$subject = "Kiva Extract"
$msg = "Kiva_Extract has been completed for July."
& ".\EchoArgs.exe" -host:myServer -from:$env:username@myDomain -to:me@myDomain -s:$subject -msg:$msg

所以,如果我在参数中添加一些引号:

Arg 0 is <-host:myServer>
Arg 1 is <-from:xxx@myDomain>
Arg 2 is <-to:me@myDomain>
Arg 3 is <-s:Kiva>
Arg 4 is <Extract>
Arg 5 is <-msg:Kiva_Extract>
Arg 6 is <has>
Arg 7 is <been>
Arg 8 is <completed>
Arg 9 is <for>
Arg 10 is <July.>

它开始产生:

$subject = "Kiva Extract"
$msg = "Kiva_Extract has been completed for July."
& ".\EchoArgs.exe" "-host:myServer" "-from:$env:username@myDomain" "-to:me@myDomain" "-s:$subject" "-msg:$msg"

哪个适合你。

或者,您可以使用数组传递带空格的参数:

Arg 0 is <-host:myServer>
Arg 1 is <-from:xxx@myDomain>
Arg 2 is <-to:me@myDomain>
Arg 3 is <-s:Kiva Extract>
Arg 4 is <-msg:Kiva_Extract has been completed for July.>

产生几乎相同的结果。

至于其他建议,引用字符串并没有太大作用:

$a = @("-host:myServer", "-from:$env:username@myDomain", "-to:me@myDomain", "-s:Kiva Extract", "-msg:Kiva_Extract has been completed for July.");
& ".\EchoArgs.exe" $a

产生

$subject = "Kiva Extract"                                                                           
$msg = '"Kiva_Extract has been completed for July."'                                                
& ".\EchoArgs.exe" -host:myServer -from:$env:username@myDomain -to:me@myDomain -s:$subject -msg:$msg

答案 4 :(得分:0)

这可能不是您正在寻找的答案,但如果您已经在将旧批处理文件转换为PowerShell脚本,那么为什么不利用Send-MailMessage cmdlet呢?

$subject = "Kiva Extract"
$msg = "Kiva_Extract has been completed for July."

Send-MailMessage -SmtpServer "myServer" -From "$env:username@myDomain" -To "me@myDomain" -Subject $subject -Body $msg

只是我的两分钱。