powershell send-message带有正文文本不使用批处理脚本

发布于 2025-01-21 14:36:28 字数 2039 浏览 0 评论 0原文

需要帮助转换powershell命令以批量运行。我可以得到以下操作正常为.ps1:

$filecontent = Get-Content -path C:\temp\status.txt | Out-string
Send-MailMessage  -to   "[email protected]"   -from   "[email protected]"  -subject "STATUS: PASS"      -body "$filecontent" -smtpServer mail2.co.test.ca.us

当我将其放入批处理时:

PowerShell  -command "$filecontent = Get-Content -path C:\temp\status.txt | Out-String" ^
Powershell  -command Send-MailMessage        ^
    -to      \"[email protected]\"        ^
    -from    \"[email protected]\"       ^
    -subject \"STATUS: PASS \"      ^
    -body    \"$filecontent\" ^
    -smtpServer     mail2.co.test.ca.us

我会得到此错误:

 外串:找不到一个位置参数接受参数“ send-mablemessage”。
在线:1个字符:55
+ ... tatus.txt |外串发送sendmailMessage -to“  “ ...
    + categoryInfo:invalidargument :( :) [out-string],parameterBindingException
    + flutlqualifyErrid:positionalParameternotfound,Microsoft.powershell.commands.outstringcommand
 

在批处理中进行这项工作的唯一方法是将原始的powershell语法放入.ps1中并从批处理中调用它?发送邮件部分工作正常。尝试了$ filecontent Line,没有“和-raw”而不是管道和串线。

Need help converting PowerShell commands to run in batch. I can get the following to run fine as .ps1:

$filecontent = Get-Content -path C:\temp\status.txt | Out-string
Send-MailMessage  -to   "[email protected]"   -from   "[email protected]"  -subject "STATUS: PASS"      -body "$filecontent" -smtpServer mail2.co.test.ca.us

When I put it into a batch:

PowerShell  -command "$filecontent = Get-Content -path C:\temp\status.txt | Out-String" ^
Powershell  -command Send-MailMessage        ^
    -to      \"[email protected]\"        ^
    -from    \"[email protected]\"       ^
    -subject \"STATUS: PASS \"      ^
    -body    \"$filecontent\" ^
    -smtpServer     mail2.co.test.ca.us

I get this error:

Out-String : A positional parameter cannot be found that accepts argument 'Send-MailMessage'.
At line:1 char:55
+ ... tatus.txt | Out-String Send-MailMessage -to "[email protected]" -from " ...
    + CategoryInfo          : InvalidArgument: (:) [Out-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutStringCommand

Is the only way to make this work in a batch is to put the original PowerShell syntax into a .ps1 and call it from the batch? The send-mail portion works fine. Tried the $filecontent line without " and -Raw instead of the pipe and Out-string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2025-01-28 14:36:28

您应该尝试将其放入一个命令中:

PowerShell  -command "$filecontent = Get-Content -path d:\temp\status.txt | Out-String; Send-MailMessage -to \"[email protected]\" -from \"[email protected]\" -subject \"STATUS: PASS \"  -body \"$filecontent\" -smtpServer     mail2.co.test.ca.us"

因为在第二个命令$ filecontent不再存在。

You should try to put it in one command :

PowerShell  -command "$filecontent = Get-Content -path d:\temp\status.txt | Out-String; Send-MailMessage -to \"[email protected]\" -from \"[email protected]\" -subject \"STATUS: PASS \"  -body \"$filecontent\" -smtpServer     mail2.co.test.ca.us"

Because in the second command $filecontent no longer exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文