12

I have a batch script that looks like this:

Test.bat

@echo off:: Starts a PowerShell session that starts a PowerShell process with administrator privilegespowershell -noprofile -command "&{$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;$process.WaitForExit();}"

I would like to split up the code inside the "&{ ... }" over multiple line for readability.

This post says that using a trailing backtick should do the trick, but when I write:

powershell -noprofile -command "&{`    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`    $process.WaitForExit();`}"

...that is, I end each line with a trailing backtick, then I get the following error:

Incomplete string token.At line:1 char:4+ &{` <<<<    + CategoryInfo          : ParserError: (`:String) [], ParentContainsErrorRecordException    + FullyQualifiedErrorId : IncompleteString'$process' is not recognized as an internal or external command,operable program or batch file.'$process.WaitForExit' is not recognized as an internal or external command,operable program or batch file.'}"' is not recognized as an internal or external command,operable program or batch file.

What am I doing wrong?

SOLUTION:

powershell -noprofile -command "&{"^    "$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^    "$process.WaitForExit();"^    "}"
askedJun 20, 2012 at 12:09
Janne Beate Bakeng's user avatar

2 Answers2

11

You may try the caret^ to indicate that the current line continues on the next one:

powershell -noprofile -command "&{"^ "$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^ "$process.WaitForExit();"^ "}"

Please note also the leading space as well as the closing and opening" on each line.

See alsoLong commands split over multiple lines in Windows Vista batch (.bat) file.

Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJun 20, 2012 at 12:17
marapet's user avatar
Sign up to request clarification or add additional context in comments.

5 Comments

That results in: "Missing closing '}' in statement block. At line:1 char:4 + &{^ <<<< + CategoryInfo : ParserError: (CloseBraceToken:TokenId) [], ParentContainsErrorRecord Exception + FullyQualifiedErrorId : MissingEndCurlyBrace '$process' is not recognized as an internal or external command, operable program or batch file."
Try adding a space at the beginning of the following lines.
Now it works... on top of the leading space, double-quotes have to be closed before the end of line and reopened on the next line.
I got it to work, you almost had it, just need to remove the trailing backticks. :) Thanks!
I did remove them in my example... after having spotted them :-)
1

Try the following. I think this will help.

powershell -noprofile -command ""&{^    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb    RunAs -PassThru;^    $process.WaitForExit();^}""
Peter Mortensen's user avatar
Peter Mortensen
31.4k22 gold badges110 silver badges134 bronze badges
answeredJun 20, 2012 at 12:30
singh's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.