Reputation: 23
I have a Windows batch file for cmd.exe. It has a single line of code to invoke the PowerShell Send-MailMessage cmdlet and exit back to cmd.exe. Currently the line reads like:
PowerShell -Command "Send-MailMessage -To [email protected], [email protected] -Subject 'A rather lengthy text' -From [email protected] -SmtpServer hostname.subdomain.mycompany.com -UseSsl -Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'UserName',('SecretePassword1234'|ConvertTo-SecureString -AsPlainText -Force)) -Body 'A static text.'"
It works great. But I find it difficult to edit it in NotePad or any text editor because the line is way too long. What I want is to use the CMD.EXE's caret ^ character to break up the long string within the double quotes into multiple lines. However, the PowerShell.EXE command requires that:
PowerShell -Command <string>
The above syntax requires that CMD.EXE parse everything after -Command to be a SINGLE string, aka a single argument. I've tried the following that didn't work:
PowerShell -Command "Semd-MailMessage Blab Blab " ^
"More Blah Blah"
If I break up the line in my batch file as above, CMD.EXE will parse the first part of the string as one argument, and the string on the next line as another argument. Wrong. So what should I do?
Upvotes: 1
Views: 5671
Reputation: 67216
I think this is the simplest and clearest way to solve this problem because this method does not require additional files nor additional options, just plain PowerShell code:
PowerShell ^
Send-MailMessage ^
-To [email protected], [email protected] ^
-Subject 'A rather lengthy text' ^
-From [email protected] ^
-SmtpServer hostname.subdomain.mycompany.com ^
-UseSsl ^
-Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'UserName',('SecretePassword1234'^|ConvertTo-SecureString -AsPlainText -Force)) ^
-Body 'A static text.'
Just be sure to properly escape special Batch characters, like ^|
.
Upvotes: 1
Reputation: 38604
I can agree as you've indicated that this doesn't work:
PowerShell -Command "Semd-MailMessage Blab Blab " ^
"More Blah Blah"
…and also agree that this doesn't either:
@Echo Off
PowerShell -Command "Get-WmiObject -Class Win32_Product "^
"| Select-Object -Property Name | Sort-Object Name"
Pause
However this does:
@Echo Off
PowerShell -Command "Get-WmiObject -Class Win32_Product "^
"| Select-Object -Property Name | Sort-Object Name"
Pause
…note the very small but seemingly very important single space at the beginning of the continuation line!
Upvotes: 2
Reputation: 14290
You either set variables in a batch file or you can do something completely hokey like this.
@echo off
setlocal enabledelayedexpansion
set "pscommand="
FOR %%G IN (
"Send-MailMessage -To [email protected], "
"[email protected] "
"-Subject 'A rather lengthy text' "
"-From [email protected] "
"-SmtpServer hostname.subdomain.mycompany.com "
"-UseSsl -Credential "
"(New-Object -TypeName System.Management.Automation.PSCredential "
"-ArgumentList 'UserName',('SecretePassword1234'|ConvertTo-SecureString -AsPlainText -Force)) "
"-Body 'A static text.'"
) DO (
set "pscommand=!pscommand!%%~G"
)
PowerShell -Command "%pscommand%"
Upvotes: 0
Reputation: 5048
I would put your command into a powershell script and then call that from cmd using the following command (or something similar):
powershell.exe -noprofile -executionpolicy bypass -file "path to my file.ps1"
then your editing can all happen inside the powershell ide and you can split your command up as required; either by using variables and/or by using the ` (back tick) character. Your Powershell script could then look like the following with is a lot easier to manage; plus you get the benefit of intellisense as you edit the script:
Send-MailMessage `
-To [email protected], [email protected] `
-Subject 'A rather lengthy text' `
-From [email protected] `
-SmtpServer hostname.subdomain.mycompany.com `
-UseSsl `
-Credential (New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'UserName', ('SecretePassword1234'|ConvertTo-SecureString -AsPlainText -Force)) `
-Body 'A static text.'
Upvotes: 2