gpt4 book ai didi

多行的 PowerShell 命令

转载 作者:行者123 更新时间:2023-12-03 08:18:40 31 4
gpt4 key购买 nike

我有一个看起来像这样的批处理脚本:
测试.bat

@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();}"
我想将“&{ ... }”中的代码分成多行以提高可读性。
This帖子说使用尾随反引号应该可以解决问题,但是当我写:
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 C:\Dev\Powershell\Sandbox.ps1' -verb RunAs -PassThru;"^
"$process.WaitForExit();"^
"}"

最佳答案

你可以试试插入符号 ^表示当前行在下一行继续:

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

另请注意前导空间以及关闭和打开 "在每一行上。

另见 Long commands split over multiple lines in Windows Vista batch (.bat) file .

关于多行的 PowerShell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11119240/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com