我想我在這裏有一個很好的解決方案。此外,在我自己的無能,我想我找出你的錯誤原因
首先,我發現一個cleaner的方式來創建特殊文件夾的快捷方式。
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\users\user\Desktop\MacadizamianNizzut.lnk")
$Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
$Shortcut.Save()
你也可以代替mycomputer
使用mydocuments
。有關可以使用的特殊文件夾的完整列表:[enum]::GetNames([System.Environment+SpecialFolder])
。向JRV提示您對我上面的鏈接發表評論。
至於你的錯誤"Exception calling "Save" with "0" arguments : "Unable to save shortcut"
。我也有這個錯誤。在實踐中,這是因爲createshortcut
傳遞的值不是有效的路徑。我並不是說文件必須存在,但文件夾路徑確實存在。我犯了一個錯字,並得到了錯誤。使用我的例子此命令就會失敗:Test-Path ""C:\users\user\Desktop"
一些錯誤預防
我們可以做的是把快捷方式路徑分配給一個變量,並測試基於這樣的路徑。
$ShortcutPath = "C:\users\username\desktop\test.lnk"
If(Test-Path -Path (Split-Path -Path $ShortcutPath -Parent)){
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = [environment]::getfolderpath("mycomputer")
$Shortcut.Save()
} Else {
Write-Host "Unable to create shortcut. Check the path $ShortcutPath."
}
這是更簡單的使用組策略首選項,如文檔[這裏](http://blogs.technet.com/b/askds/archive/2014/02/17/adding-shortcuts-on-desktop-使用基團的策略偏好式 - 窗口8和 - 窗口8-1.aspx)。 – 2014-09-04 22:14:03
當試圖設置TargetPath(「TargetPath:參數不正確」)時,我得到一個完全不同的錯誤。嘗試設置TargetPath =「explorer.exe」和Arguments =「\/e,:: {20D04FE0-3AEA-1069-A2D8-08002B30309D}」。此外,是當前用戶的文件夾或不同用戶的快捷方式? – 2014-09-05 00:19:12
嗨,邁克。我使用我的管理員帳戶運行腳本,以在用戶桌面上創建快捷方式。下面馬特的腳本就像一個魅力,除非我用我的管理員帳戶運行腳本,我會得到錯誤信息。 – Besiktas 2014-09-05 14:07:02