多行的PowerShell命令 [英] PowerShell command over multiple lines

查看:258
本文介绍了多行的PowerShell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a batch script that looks like this:

@echo off

:: Starts a PowerShell session that starts a PowerShell process with administrator privileges
powershell -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.

我在做什么错了?

解决方案:

powershell -noprofile -command "&{"^
    "$process = start-process powershell -ArgumentList '-noprofile -noexit -file C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
    "$process.WaitForExit();"^
    "}"

推荐答案

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

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.

另请参见 在Windows Vista批处理(.bat)文件中,长命令会分成多行 .

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

这篇关于多行的PowerShell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆