windows - 使用PowerShell创建启动PowerShell脚本的快捷方式

标签 windows powershell wsh

我需要在Windows 2019 Server上为Eclipse应用程序创建快捷方式,以便它运行PowerShell脚本而不是打开Eclipse。该脚本首先运行一些命令,然后打开Eclipse应用程序。当编辑Eclipse的现有快捷方式时,可以将目标修改为:

"powershell.exe -ExecutionPolicy bypass C:\temp\eclipse-fix.ps1 -WindowStyle Hidden"
手动完成后,效果很好。但是,当我尝试使用PowerShell脚本执行相同操作以自动化该过程时,出现错误。
脚本内容:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy bypass C:\cfn\temp\eclipse-fix.ps1 -WindowStyle Hidden"
$Shortcut.IconLocation = "%SystemDrive%\eclipse\eclipse.exe"
$Shortcut.Save()
返回错误:
PS C:\Users\Administrator> $WshShell = New-Object -comObject WScript.Shell
PS C:\Users\Administrator> $Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
PS C:\Users\Administrator> $Shortcut.TargetPath = "powershell.exe -File C:\cfn\temp\eclipse-fix.ps1"
Value does not fall within the expected range.
At line:1 char:1
+ $Shortcut.TargetPath = "powershell.exe -File C:\cfn\temp\eclipse-fix. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException

PS C:\Users\Administrator> $Shortcut.IconLocation="%SystemDrive%\eclipse\eclipse.exe, 0"
PS C:\Users\Administrator> $Shortcut.Save()
结果:
图标已创建,但没有目标值。
有谁知道我想让它起作用的东西吗?还有其他我可能不知道的选择吗?
谢谢。

最佳答案

您需要将ArgumentsTargetPath分开指定

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Users\Administrator\Desktop\Eclipse.lnk")
$Shortcut.TargetPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$Shortcut.Arguments = "-ExecutionPolicy bypass C:\cfn\temp\eclipse-fix.ps1 -WindowStyle Hidden"
$Shortcut.IconLocation = "%SystemDrive%\eclipse\eclipse.exe"
$Shortcut.Save()
您也可以只使用powershell.exe代替完整路径。两者都会起作用。

关于windows - 使用PowerShell创建启动PowerShell脚本的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64071978/

相关文章:

mysql - Windows Insider 服务每 30 分钟自动启动一次,导致 mysql 崩溃

windows - 批处理文件 : What's the best way to declare and use a boolean variable?

powershell - 调用运算符&的命令是什么?

arrays - 如何从 powershell 中的扩展类型数组中提取/匹配文件类型扩展

javascript - 无法为.HTA页面中带有JS的元素设置新的innerHTML

windows - Amazon S3 的简单且可扩展的非托管替代方案

r - 在 VSCode 中为 "Error: pandoc version 1.12.3 or higher is required",但在 R Studio 中有效

python - virtualenv 激活不起作用

php - 获取我的IP地址

jscript - 相当于JScript运行时中的窗口