简体   繁体   中英

Line continuation in `cmd` script to run `powershell` command

I would like to change this one long line into multiple lines in the cmd script.

powershell.exe -Command "Send-MailMessage -To 'pwatson@company.com' -From 'pwatson@company.com' -Subject 'Standard Extract Request' -SmtpServer 'mail.company.com' -Attachment 'file.txt'"

Using the cmd line continuation character, "^", does not work. Likewise, using the powershell line continuation character, "`", also fails.

THIS DOES NOT WORK

powershell.exe -Command "Send-MailMessage ^
  -To 'pwatson@company.com' ^
  -From 'pwatson@company.com' &
  -Subject 'Standard Extract Request' ^
  -SmtpServer 'mail.company.com' ^
  -Attachment 'file.txt'"

Just remove the " around the Command and change the & by a ^test, like this:

powershell.exe -Command Send-MailMessage ^
  -To 'pwatson@company.com' ^
  -From 'pwatson@company.com' ^
  -Subject 'Standard Extract Request' ^
  -SmtpServer 'mail.company.com' ^
  -Attachment 'file.txt'

Don't do that. If you want to wrap PowerShell statements, put them in a .ps1 file:

Send-MailMessage `
  -To 'pwatson@company.com' `
  -From 'pwatson@company.com' `
  -Subject 'Standard Extract Request' `
  -SmtpServer 'mail.company.com' `
  -Attachment 'file.txt'

and run that file:

powershell.exe -File "C:\path\to\your.ps1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM