【问题标题】:Use PowerShell to create a shortcut to launch a PowerShell script使用 PowerShell 创建启动 PowerShell 脚本的快捷方式
【发布时间】:2020-09-25 22:23:41
【问题描述】:

我需要在 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()

结果:

图标已创建,但没有目标值。

有谁知道我缺少什么才能让它工作?还有其他我可能不知道的选项吗?

谢谢。

【问题讨论】:

    标签: windows powershell wsh


    【解决方案1】:

    您需要指定Arguments 与TargetPath 分开

    $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 代替完整路径。两者都行。

    【讨论】:

    【解决方案2】:

    这是我编写的一个脚本,它将在桌面上创建一个自身的快捷方式,运行时可执行(可以通过双击运行)。

    我这样做是为了让同事不必为我的 powershell 自动化脚本创建自己的快捷方式,从而逐步完成流程。

    因为我们工作中的共享驱动器对每个人来说都是不同的位置,所以我使用“Get-location”命令获取密码,然后对其进行修剪和解析,以便在快捷方式的参数字段中使用。

    为了让参数包含双引号,您必须在另一组引号之前使用转义字符`,这将允许脚本将引号发送到参数字段。没有这个就行不通。 '"'"

    确保在运行脚本时不是从 powershell IDE 运行它,否则文件路径将是错误的。您需要将脚本保存在目标源路径中,然后通过右键单击它并将其作为 powershell 脚本运行或为其创建可执行快捷方式来从那里运行它。如果您需要有关如何创建可执行的 powershell 快捷方式的说明,请告诉我。

    希望这会有所帮助!

    #Grap the pwd
    $location = Get-Location | Select Path | ft -HideTableHeaders | Out-String;
    #Trim the pwd variable of any extra spaces and unwanted characters
    $location = $location.Trim();
    $locations = $location.replace(' ' , '');
    $locations = $locations.replace("`n","");
    
    #Make the source file location my path to powershell.exe
    $SourceFileLocation = "`"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`"";
    
    # Declare where I want to place the shortcut, I placed it on the desktop of whomever is running the script with the dynamic $env:USERNAME which takes the username of whomever is running the script - You can name the shortcut anything you want at the end as long as it ends with .LNK
    $ShortcutLocation = “C:\Users\$env:USERNAME\OneDrive\Desktop\PopupSortcut.lnk”;
    
    #Create a now com
    $WScriptShell = New-Object -ComObject WScript.Shell;
    
    #create shortcut and provide the location parameter as an argument
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutLocation);
    
    #set the target path
    $Shortcut.TargetPath = $SourceFileLocation;
    
    #Add arguments that will bypass the execution policy as well as provide a path to the source powershell script (make sure that the entire argument has double quotes around it and that the internal quotes have escape characters (`) behind them, these are not apostrophes but back ticks, located on the same key as the tilde (~) key on the keyboard
    $Shortcut.Arguments = “-ExecutionPolicy Bypass -file `"$locations"+"Pop-up.ps1`"”;
    
    #Save the Shortcut
    $Shortcut.Save();
    
    
    
    
    ########################### This is the meat of our script Performing an Operation #######################
    
    
    $wshell = New-Object -ComObject Wscript.Shell;
    
    $wshell.Popup("Operation Completed",0,"Done",0x1);
    

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2018-10-08
      • 2012-03-30
      • 2021-07-10
      • 2022-10-17
      • 2019-04-16
      • 1970-01-01
      • 2021-05-12
      • 2021-10-04
      相关资源
      最近更新 更多