【发布时间】:2015-08-03 16:21:43
【问题描述】:
我创建了 Windows 上下文菜单项来执行 powershell 命令。但是,我不知道如何使用转义引号将 %1 传递给我的 Powershell 脚本。
当前注册表项:
[HKEY_CLASSES_ROOT\*\shell\Share file on FTP\Command]
@="cmd.exe /C nircmd elevate PowerShell -NoProfile -NoLogo -Command \"& {(dir \"%1\" | Copy-ToFTP -Destination Public.Screenshots).source | clip}\""
当我通过“在 FTP 上共享文件”上下文菜单项选择文件时产生以下错误:
dir : Cannot find path 'D:\Multimedia' because it does not exist.
At line:1 char:5
+ & {(dir D:\Multimedia Files\Pictures\HTPC\20150328_220120.jpg | Copy-ToOnedrive ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\Multimedia:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
奖金 我目前正在使用第 3 方工具 Nircmd 来提升 Powershell 提示(因为我的 powershell 命令需要它)。如果可能的话,我想使用本机 Windows 来代替(如果可能的话,将我的命令代码保留在一行中)....不是之后,通过创建一个 Powershell PS1 脚本来提升自己。
编辑: 下面的代码行“应该”工作..但是,我没有得到一个短名称路径,而是得到一个意外的错误结果:
我正在复制和粘贴注册表项值以使其更易于阅读(而不是 .reg 文件内容)
cmd.exe /c for %%A in (%1) do @set I=%%~sA | PowerShell.exe -NoExit -NoProfile -NoExit -NoLogo -Command "& {(dir %I% | Copy-ToFTP -Destination Public.Screenshots).source | clip}"
最终在 Powershell 中生成此输出:
dir : Cannot find path 'D:\Multimedia Files\Pictures\HTPC\:433310880:4624'
because it does not exist.
【问题讨论】:
-
我认为真正让我感到困惑的是 %~s1 在 Windows 8.1 命令解释器 cmd 下不起作用。我在 Win7 及更早版本下使用了这种缩短技术数十次。因此,即使是只有 echo %~s1 的最简单的批处理文件也会返回完整路径,而不会出现任何缩短或错误。
-
我猜语法的复杂性会很困难;或者,我现在可能已经看到了有效的答案
标签: powershell cmd registry contextmenu