0
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\$user\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\CheckLauncher.lnk")
$Shortcut.TargetPath = "cmd.exe"
$Shortcut.Arguments = "C:\Users\Administrator\Desktop\Packages\checkLauncher.bat"

错误

Exception calling "Save" with "0" argument(s): "Unable to save shortcut "C:\Use
rs\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\
CheckLauncher.lnk"."
At C:\Users\Administrator\Desktop\Packages\Setup.ps1:22 char:15
+ $Shortcut.Save <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

我向您保证,这条路径 - C:\Users\Administrator\Desktop\Packages\checkLauncher.bat 确实存在。请帮助找出问题所在。

等待任何帮助..提前致谢

4

1 回答 1

3

我可以使用这种方法成功创建快捷方式,但是您是否在$user其他地方定义过,因为默认情况下没有定义。也许您打算使用$env:username? 或者,您可以使用$home来获取c:\users\<username>路径的一部分。更好的是使用这个 .NET 方法:

[Environment]::GetFolderPath([environment+specialfolder]::ApplicationData)

我认为您还想像这样指定参数:

$Shortcut.Arguments = "/c C:\Users\Administrator\Desktop\Packages\checkLauncher.bat"

以下对我来说很好:

$roaming = [environment]::GetFolderPath([environment+specialfolder]::ApplicationData)
$path = "$roaming\Microsoft\Windows\Start Menu\Programs\Startup\UpdateBinDir.lnk"
$WshShell = New-Object -comObject WScript.Shell
$shortcut = $WshShell.CreateShortcut($path)
$shortcut.TargetPath = "cmd.exe"
$shortcut.Arguments = "/c C:\bin\updateBinDir.bat"
$shortcut.Save()
于 2013-09-19T16:20:50.227 回答