简体   繁体   English

“ Powershell启动过程-ArgumentList”中的文字引号

[英]Literal quotes in “Powershell Start-Process -ArgumentList”

I am currently attempting to create a command that opens an admin Powershell from the right click context menu. 我当前正在尝试创建从右键单击上下文菜单中打开admin Powershell的命令。 For context, context menu commands run in CMD. 对于上下文,上下文菜单命令在CMD中运行。

My issue is that I am trying to cd into the directory where the right click occurs. 我的问题是我试图将cd插入发生右键单击的目录。 The below command works just fine for most directories, but if the directory path contains a space, then it will only try to move into the portion of the path before the space, throwing an error. 下面的命令对大多数目录都适用,但是如果目录路径包含空格,则它将仅尝试移入该空格之前的路径部分,从而引发错误。 My understanding is that the current directory is passed in through %V but when I run the command echo %V using the same process, it splits paths with a space onto 2 lines, so I assume the parts of the path are stored in separate strings? 我的理解是,当前目录是通过%V传递的,但是当我使用相同的过程运行命令echo %V ,它会将路径以空格分隔为两行,因此我假设路径的各个部分存储在单独的字符串中?

Powershell -noexit "Start-Process 'C:\Users\<me>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell\Windows PowerShell.lnk' -ArgumentList '-noexit','Set-Location -literalPath `"%V`"' -Verb RunAs"

I have updated the above command to match a suggestion below, and when right clicking on the desktop (which previously worked due to a lack of spaces) I now get the following error: 我已经更新了上面的命令以匹配下面的建议,并且在桌面上单击鼠标右键(由于缺少空格,以前可以使用),现在出现以下错误:

Set-Location : Cannot find path 'C:\Users\<me>\Desktop`' because it does not exist.
At line:1 char:1
+ Set-Location -literalPath `C:\Users\<me>\Desktop`
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (C:\Users\<me>\Desktop`:String) [Set-Location], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

Note that in both of the above code blocks, <me> is my actual username. 请注意,在以上两个代码块中, <me>是我的实际用户名。

I've been tearing my hair out trying to put quotes around the path but I can't seem to get Powershell to put the quotes in due to the fact that I already use both single and double quotes. 我一直在努力尝试在路径两边添加引号,但是由于我已经使用了单引号和双引号,因此我似乎无法让Powershell放置引号。

Any help would be greatly appreciated, thanks in advance. 任何帮助将不胜感激,在此先感谢。

Edit: 编辑:

For those still looking for an answer, I ran the following Powershell script to add functional commands to my context menu: 对于仍在寻找答案的用户,我运行了以下Powershell脚本,将功能性命令添加到上下文菜单中:

$menu = 'Open Windows PowerShell Here as Administrator'
$command = "$PSHOME\powershell.exe -NoExit -NoProfile -Command ""Set-Location '%V'"""

'directory', 'directory\background', 'drive' | ForEach-Object {
    New-Item -Path "Registry::HKEY_CLASSES_ROOT\$_\shell" -Name runas\command -Force |
    Set-ItemProperty -Name '(default)' -Value $command -PassThru |
    Set-ItemProperty -Path {$_.PSParentPath} -Name '(default)' -Value $menu -PassThru |
    Set-ItemProperty -Name HasLUAShield -Value ''
}

The answer was found from How do I start PowerShell from Windows Explorer? 如何从Windows资源管理器启动PowerShell中找到了答案

If you want to avoid space issues, you can reuse " by escaping it with ` in a string. 如果要避免空间问题,可以通过在字符串中用`将其转义来重用"

For example : 例如 :

$command = "Set-Location `"C:\temp\test space`""

String will become this and spaces will be handled correctly : 字符串将变为this并且空格将被正确处理:

Set-Location "C:\\temp\\test space" 设置位置“ C:\\ temp \\ test space”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM