【问题标题】:Passing argument with one single quote in it to a .PS1 script将带有一个单引号的参数传递给 .PS1 脚本
【发布时间】:2019-11-06 21:42:05
【问题描述】:

我正在测试一个 PS 脚本,该脚本将被放入 win10 右键单击​​上下文菜单中,该脚本仅接受路径信息,然后将其回显到我的终端窗口。 注册表项如下: Computer\HKEY_CLASSES_ROOT\*\shell\MyScript\command @="powershell.exe -window hidden -command .'\"G:\\path to my script\\MyScript.ps1\"' '%V'"

脚本只是$args

对于大多数常规路径,它运行良好。现在我遇到了文件名包含一个单引号的情况,例如:"D:\John's file.txt"

它会抛出一个错误:

The string is missing the terminator: '. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我应该怎么做才能解决这个问题?

【问题讨论】:

  • 对不起,我错过了(这就是我刚刚删除我的 cmets 的原因)。无论如何,在 stackoverflow 上有很多类似的问题和答案,例如:stackoverflow.com/questions/20958388/…

标签: windows powershell registry contextmenu


【解决方案1】:

我自己找到了解决方案。

  1. 根据this post 注册表项要修改为:Computer\HKEY_CLASSES_ROOT\*\shell\MyScript\command @="powershell.exe -window hidden -file \"G:\\path to my script\\MyScript.ps1\" "%V""

而不是-command,使用-file,并且%V周围的单引号应该是双引号。

2.要获取完整路径,您必须使用-join 运算符连接以空格分隔的字符串,如下所示(如果路径包含空格):

$path = $args -join " "

Powershell v6 用户可以改用join-string cmdlet。

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 2015-11-21
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    相关资源
    最近更新 更多