当我尝试将以下单行命令转换为多行时,我遗漏了一些东西。我在Powershell -Command over multiple line
之后搜索,找到并尝试对此更改进行建模我错过了什么?我试图让它易于测试。只需将前两(2)行更改为适合您系统的行。
=== emailtest.bat
@ECHO OFF
SET "RECIPIENT=me@nowhere.org"
SET "SMTPHOST=imr2.nowhere.org"
SET "FN=C:\Windows\win.ini"
@ECHO ON
powershell -Command "& send-mailmessage -to "%RECIPIENT%" -from 'noreply@nowhere.org' -subject 'test file transfer' -SmtpServer '%SMTPHOST%' -BodyAsHTML 'The file is <a href="%FN">here</a> for you.'
powershell -noprofile -Command "&{"^
send-mailmessage -to "%RECIPIENT%" ^
-from 'noreply@nowhere.org' ^
-subject 'test file transfer' ^
-SmtpServer '%SMTPHOST%' ^
-BodyAsHTML 'The file is <a href="%FN%">here</a> for you.'^
"}"
===输出
C:>call emailtest.bat
12:17:24.75 C:\Users\me\t
C:>powershell -Command "& send-mailmessage -to "me@nowhere.org" -from 'noreply@nowhere.org' -subject 'test file transfer' -SmtpServer 'imr2.nowhere.org' -BodyAsHTML 'The file is <a href="C:\Windows\win.ini">here</a> for you.'
12:17:27.28 C:\Users\me\t
C:>powershell -noprofile -Command "&{" send-mailmessage -to "me@nowhere.org" -from 'noreply@nowhere.org' -subject 'test file transfer' -SmtpServer 'imr2.nowhere.org' -BodyAsHTML 'The file is href="C:\Windows\win.ini""}" 0</a 1>for
The system cannot find the file specified.
答案 0 :(得分:0)
我相信这个例子才有效,因为它仍然是每行多个命令。如果你尝试在多行上执行一个命令,那么你开始需要在powershell中转义它们,这是使用splatting的更好的解决方案。
powershell -NoProfile -Command "&{" ^
"$message = @{};" ^
"$message.Subject = 'test file transfer';" ^
"$message.From = 'noreply@nowhere.org';" ^
"$message.To = '%RECIPIENT%';" ^
"$message.SmtpServer = '%SMTPHOST%';" ^
"$message.Body = 'The file is <a href="%FN%">here</a> for you.';" ^
"$message.BodyAsHTML = $true; " ^
"Send-MailMessage @message" ^
}"
话虽如此,如果可能的话,你应该考虑把它变成ps1文件。可读性远不是很好。 BodyAsHTML
也是一面旗帜。你仍然需要设置身体。您的代码在技术上仍然可以正常工作,因为Body是一个位置参数,只是澄清它在splat中添加的原因。