[英]Powershell Removing Quotes Argument
I'm using Start-Process
to start another instance of Powershell as an administrator but when I try to pass the argument list, whether as a variable or as a plain string, Powershell removes the quotes. 我正在使用Start-Process
以管理员身份启动Powershell的另一个实例,但是当我尝试传递参数列表时,无论是作为变量还是作为纯字符串,Powershell都会删除引号。 Below is the command I'm using: 以下是我正在使用的命令:
$argu = '-noexit "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"';
powershell Start-Process -Verb RunAs -FilePath powershell -ArgumentList $argu
This is the error I get: 这是我得到的错误:
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:1 char:88
+ ... Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\v ...
+ ~~~
+ CategoryInfo : ObjectNotFound: (x86:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Thank you in advance for any help. 预先感谢您的任何帮助。
Update: 更新:
$argu = '''-noexit ""C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat""''';
powershell Start-Process -Verb RunAs -FilePath powershell -ArgumentList $argu
This almost fixes it but now I'm getting the error above in the second window instead of the first. 这几乎可以解决它,但是现在我在第二个窗口而不是第一个窗口中收到上面的错误。
(A) From inside PowerShell: (A) 在 PowerShell 内部 :
$argu = '-noexit -command & \"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat\"'
Start-Process -Verb RunAs -FilePath powershell -ArgumentList $argu
Note: I'm not calling Start-Process
via powershell.exe
, as there is generally no need for that. 注意:我不是通过powershell.exe
调用Start-Process
,因为通常不需powershell.exe
。
The embedded "
are \\
-escaped, which is what PowerShell requires when you call its CLI (perhaps surprisingly, given that PowerShell-internally it is `
that acts as the escape character). 嵌入式"
是\\
转义的,这是什么PowerShell需要,当你调用它的CLI(也许令人惊讶,因为PowerShell的内部是`
充当转义字符)。
"
are embedded inside '...'
here, they shouldn't require extra escaping - see below. 话虽如此,但考虑到"
被嵌入在'...'
内,它们不需要多余的转义-见下文。 The file path to execute is prefixed with call operator &
, because you need it in order to execute files that are specified in quoted form. 要执行的文件路径以调用运算符&
为前缀,因为您需要它来执行以引号形式指定的文件。
Note that I've added -Command
, which is not strictly necessary in Windows PowerShell, but would be if you ran your command from PowerShell Core (which now defaults to -File
). 请注意,我添加了-Command
,这在Windows PowerShell中不是严格必需的,但是如果您从PowerShell Core (现在默认为-File
)运行命令,则将是必需的。
Alternatively, you could also specify your arguments individually, as part of an array , which is arguably the cleaner solution: 另外,您也可以作为array的一部分单独指定参数,这可以说是更简洁的解决方案:
$argu = '-noexit', '-command', '&', 'de',
'\"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat\"'
Start-Process -Verb RunAs -FilePath powershell -ArgumentList $argu
Sadly, even in this case you need the extra, embedded quoting for arguments that contain spaces, which is a known Start-Process
problem being tracked on GitHub . 不幸的是, 即使在这种情况下,您也需要对包含空格的参数使用额外的嵌入式引号 ,这是在GitHub上跟踪的一个已知的Start-Process
问题 。
PowerShell's handling of quoting when calling external programs is generally problematic; 在调用外部程序时,PowerShell的报价处理通常存在问题; the current issues are summarized in this GitHub issue . 当前问题在GitHub问题中进行了总结。
(B) From outside PowerShell ( cmd.exe
, a custom File Explorer context menu): (B)从PowerShell 外部 ( cmd.exe
,自定义文件资源管理器上下文菜单):
powershell -command Start-Process -Verb RunAs -FilePath powershell -ArgumentList '-noexit -command . ''C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat'''
single -quoting is now employed (with nested single quotes escaped as ''
), because double-quoting would substantially complicate the escaping. 单 -quoting现在采用(带有嵌套单引号转义为''
),因为双引用将基本上逸出复杂化。
.
is used instead of &
to execute the .bat
file, which avoids a problem with how the &
is parsed; 使用。代替&
执行.bat
文件,从而避免了&
的解析方式; while .
一会儿.
generally serves a different purpose than &
, the two operators behave the same when calling external programs. 通常,其目的与&
目的不同,这两个运算符在调用外部程序时的行为相同。
If you also want to set the working directory for the PowerShell session that ultimately opens elevated, you need to incorporate an explicit Set-Location
( cd
) call into the command string, because Start-Process -Verb RunAs
always defaults to the SYSTEM32 folder (even the -WorkingDirectory
parameter doesn't help in that case). 如果您还想为最终打开提升权限的PowerShell会话设置工作目录 ,则需要将显式Set-Location
( cd
)调用合并到命令字符串中,因为Start-Process -Verb RunAs
始终默认为SYSTEM32文件夹(在这种情况下,即使-WorkingDirectory
参数也无济于事。
For that to work safely, however, you must quote the directory path using double -quoting, given that file names may contain single quotes; 但是,为了使文件安全运行,您必须使用双 引号对目录路径进行引号,因为文件名可能包含单引号。 with %V
as the directory path (which File Explorer supplies to commands invoked via custom context menus), the properly escaped Set-Location
call looks like this (I wish I were kidding): 使用%V
作为目录路径(文件浏览器将其提供给通过自定义上下文菜单调用的命令),正确转义的Set-Location
调用如下所示(我希望我在开玩笑):
Set-Location \\"\\"\\"%V%\\"\\"\\"
Integrated into the full command (using Set-Location
's built-in alias cd
for brevity): 集成到完整命令中(为简便起见,使用Set-Location
的内置别名cd
):
powershell -command Start-Process -Verb RunAs -FilePath powershell -ArgumentList '-noexit -command cd \"\"\"%V%\"\"\"; . ''C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat'''
As an aside: PowerShell Core now has a -WorkingDirectory
( -wd
) CLI parameter that allows you to control the startup directory more robustly ( pwsh -wd "c:\\path\\to\\dir" ...
); 顺便说一句:PowerShell 核心现在有一个-WorkingDirectory
( -wd
)CLI参数,使您可以更有力地控制启动目录( pwsh -wd "c:\\path\\to\\dir" ...
); in fact, it was precisely the File Explorer custom context-menu use case that prompted the introduction of this parameter . 实际上,正是正是File Explorer定制上下文菜单用例促使引入了此参数 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.