windows - 使用 Powershell 创建快捷方式以使用 Powershell 运行
问题描述
我有点失落。我想要一个 powershell 脚本,它创建一个链接到另一个 powershell 脚本的快捷方式。该快捷方式应该能够以管理员身份运行,并且目标应该如下所示。我是这样手动制作的,它可以工作。
目标:C:\system32\windowspowershell\v1.0\powershell.exe -executionpolicy bypass -noexit "TARGETPATH\test.ps1"
这是我的代码,但参数出现在目标路径的后面而不是前面。
是否还有可能将默认徽标替换为特定徽标?即powershell之一
有什么建议么?
谢谢!
#Read current path
function Get-ScriptDirectory {
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$installpath = Get-ScriptDirectory
#create shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$installpath\EXE.lnk")
$Shortcut.TargetPath = """$installpath\test.ps1"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "$installpath"
$Shortcut.Save()
解决方案
包括图标和运行方式
## Q:\Test\2018\04\27\SO_50057555.ps1
#Read current path
function Get-ScriptDirectory {
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$installpath = Get-ScriptDirectory
$RunScript= "Test.ps1"
$ShCutLnk = "PwSh $RunScript.lnk"
#create shortcut
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$installpath\$ShCutLnk")
$Shortcut.TargetPath = "C:\Windows\System32\windowspowershell\v1.0\powershell.exe"
$Shortcut.IconLocation = "C:\Windows\System32\windowspowershell\v1.0\powershell.exe,0" # icon index 0
$Shortcut.Arguments = "-Nop -Executionpolicy bypass -NoExit ""$installPath\$RunScript"""
$Shortcut.WorkingDirectory = "$installpath"
$Shortcut.Save()
## Make the Shortcut runas Administrator
## Source: https://stackoverflow.com/questions/28997799/how-to-create-a-run-as-administrator-shortcut-using-powershell
$bytes = [System.IO.File]::ReadAllBytes("$installpath\$ShCutLnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$installpath\$ShCutLnk", $bytes)
推荐阅读
- tensorflow - 当我在 Tensorflow 上使用 Keras API 连接两个模型时,模型的输入张量必须来自 `tf.layers.Input`
- django - django - 当关系已经存在时添加到 M2M 关系
- javascript - 全局变量未携带 Javascript FCF
- reactjs - 如何使用 React Router DOM 将 id 更改为 URL 的 slug
- android - 如何在 Google Play Store 上私下发布应用程序?
- laravel - 缺少路线 5.6 所需的参数
- angular - 从 API 中选择选项数据源
- java - 无法将 JSON 值反序列化为类型
- vba - 用于导出/复制粘贴到单独工作簿的 Excel 宏
- git - 使用 git merge / git merge-base,可靠地确定分支是否是功能分支