需要幫助將 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
我收到此錯誤:
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
批量進行這項作業的唯一方法是將原始 PowerShell 語法放入 .ps1 并從批處理中呼叫它嗎?發送郵件部分作業正常。嘗試了沒有 " 和 -Raw 而不是管道和輸出字串的 $filecontent 行。
uj5u.com熱心網友回復:
您應該嘗試將其放在一個命令中:
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已經不存在了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/462618.html
