Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多行上的PowerShell命令_Powershell_Batch File - Fatal编程技术网

多行上的PowerShell命令

多行上的PowerShell命令,powershell,batch-file,Powershell,Batch File,我有一个如下所示的批处理脚本: 测试蝙蝠 为了可读性,我想将“&{…}”中的代码分成多行 波斯特说,使用尾随的反勾号应该可以做到这一点,但当我写: powershell -noprofile -command "&{` $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;` $process.

我有一个如下所示的批处理脚本:

测试蝙蝠 为了可读性,我想将“&{…}”中的代码分成多行

波斯特说,使用尾随的反勾号应该可以做到这一点,但当我写:

powershell -noprofile -command "&{`
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;`
    $process.WaitForExit();`
}"
…也就是说,我用一个尾随的反勾号结束每一行,然后我得到以下错误:

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.

您可以尝试插入符号
^
,以指示当前行在下一行继续:

powershell -noprofile -command "&{"^
 "$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^
 "$process.WaitForExit();"^
 "}"
还请注意每行的前导空格以及关闭和打开


另请参见。

尝试以下方法。我认为这会有所帮助

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

这将导致语句块中出现“缺少结尾“}”。第1行字符数:4+&{^尝试在下面几行的开头添加一个空格。现在它可以工作了…在前导空格的顶部,双引号必须在行尾之前关闭,然后在下一行重新打开。我成功了,你几乎成功了,只需要删除尾随的反勾号即可。:)谢谢!我在我的示例中删除了它们…在发现它们之后:-)
powershell -noprofile -command "&{"^
 "$process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb RunAs -PassThru;"^
 "$process.WaitForExit();"^
 "}"
powershell -noprofile -command ""&{^
    $process = start-process powershell -ArgumentList '-noprofile -noexit -file GetTools.ps1' -verb
    RunAs -PassThru;^
    $process.WaitForExit();^
}""