将带有转义字符的字符串分配给属性

时间:2015-08-14 10:51:55

标签: string powershell desktop-shortcut

我想将一个字符串路径设置为$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"'

0 个答案:

没有答案