我想我在这里有一个很好的解决方案。此外,在我自己的无能,我想我找出你的错误原因
首先,我发现一个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