2015-08-14 87 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字符串中。 –

相關問題