2016-08-10 7 views
-1

次の1行のコマンドを複数の行に変換しようとすると、何かが見当たりません。私が見つけ、検索、そして私が何をしないのですPowershell -Command over multiple linePowerShellを実行中のcmdの引用符で囲んだりエスケープしたり、cmdを実行しています

後、この変更をモデル化しようとしましたか?私はそれを簡単にテストしようとしました。最初の2行をシステムに適したものに変更してください。

===出力は、私はそれがまだ行ごとに複数のコマンドですので、例のみ働くと信じ

C:>call emailtest.bat 

12:17:24.75 C:\Users\me\t 
C:>powershell -Command "& send-mailmessage -to "[email protected]" -from '[email protected]' -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 "[email protected]" -from '[email protected]' -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. 
+1

物事はあなたがPowerShellはcmd.exeシェルスクリプト(バッチファイル)でコマンドをラップしようとしているの代わりに、直接PowerShellを使用している場合、簡単かつ単純になります。 –

答えて

0

です===

@ECHO OFF 
SET "[email protected]" 
SET "SMTPHOST=imr2.nowhere.org" 
SET "FN=C:\Windows\win.ini" 
@ECHO ON 

powershell -Command "& send-mailmessage -to "%RECIPIENT%" -from '[email protected]' -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 '[email protected]'^
-subject 'test file transfer'^
-SmtpServer '%SMTPHOST%'^
-BodyAsHTML 'The file is <a href="%FN%">here</a> for you.'^ 
"}" 

emailtest.bat。複数の行で1つのコマンドを実行しようとすると、powershellでそれらをエスケープする必要があります。ここではsplattingを使用するより良い解決法があります。

powershell -NoProfile -Command "&{"^
    "$message = @{};"^
    "$message.Subject = 'test file transfer';"^
    "$message.From = '[email protected]';"^
    "$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で追加されたのかを明確にするだけです。

+0

多くのありがとうございます。これは機能しています。 – lit

関連する問題