2015-08-14 88 views
0

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

奇怪的是,试图重现一个新的自定义对象上的这种行为,这个问题没有按” t出现。只需运行以下命令:

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

回答

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"' 
+0

非常感谢您指出这一点。我必须留意这一点。我在窗口中使用普通链接项目,并在目标中使用params,并将属性视图GUI中的链接项目复制到字符串TargetPath字符串中。 –

相关问题