【问题标题】:Assigning a string with escaped characters to a property将带有转义字符的字符串分配给属性
【发布时间】:2015-08-14 10:51:54
【问题描述】:

我想为 $Shortcut 对象的字符串属性设置字符串路径,但它无法正常工作。

运行此代码:

$WshShell = New-Object -comObject WScript.Shell ;
$Shortcut = $WshShell.CreateShortcut('c:\aaa.lnk');
$Shortcut.TargetPath = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'

我在设置 $Shortcut.TargetPath 时收到此错误:

Exception setting "TargetPath": "The parameter is incorrect. (Exception from HRESULT:
0x80070057 (E_INVALIDARG))"
At line:1 char:11
+ $Shortcut. <<<< TargetPath = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

奇怪的是,尝试在新的自定义对象上重现此行为,问题并未出现。只需运行以下命令:

$object = New-Object -TypeName PSObject
Add-Member -MemberType NoteProperty -Name prop -Value "aaa"
$object.prop = '"c:\Program Files\MyApp.exe" /param1: (2) /param2 "val"'

【问题讨论】:

    标签: string powershell desktop-shortcut


    【解决方案1】:

    如有疑问,请阅读documentationTargetPath 属性仅采用可执行文件的路径。参数进入Arguments 属性:

    $WshShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut('c:\aaa.lnk')
    $Shortcut.TargetPath = 'c:\Program Files\MyApp.exe'
    $Shortcut.Arguments  = '/param1: (2) /param2 "val"'
    

    【讨论】:

    • 非常感谢您指出这一点。我必须注意那个。我在目标中使用了带有参数的窗口中的普通链接项,并将这些从属性视图 GUI 复制到字符串 TargetPath 字符串中。
    猜你喜欢
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2010-10-09
    相关资源
    最近更新 更多