windows - 创建快捷方式以使用 Powershell 运行 Powershell

标签 windows powershell shortcut

我有点迷失了。我想要一个 powershell 脚本来创建链接到另一个 powershell 脚本的快捷方式。该快捷方式应该能够以管理员身份运行,并且目标应该如下所示。我是这样手动制作的,并且有效。

目标:C:\system32\windowspowershell\v1.0\powershell.exe -executionpolicy绕过-noexit“TARGETPATH\test.ps1”

这是我的代码,但参数出现在目标路径后面而不是前面。

是否还可以将默认 Logo 替换为特定 Logo ?即 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)

enter image description here

关于windows - 创建快捷方式以使用 Powershell 运行 Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50057555/

相关文章:

c# - 在 Window Phone 8.1 中从服务器收到原始推送通知后执行某些功能

c++ - 如何获取卷 GUID

macos - osx : shortcut for 'Comment with Line Comment' 上的 phpstorm

c# - 从 C# 以编程方式创建快捷方式并设置 "Run as administrator"属性

powershell - Powershell:如何将 “ISO” char转换为UTF8?

java - Java Swing 中上下文相关的键盘绑定(bind)

windows - 尝试通过 Windows 上的 Alias 向 Apache on Dropbox 文件夹授予权限

c - 使 udp 套接字成为非阻塞

powershell - 在像PowerShell中的Foreach-Object这样的循环中退出函数

Powershell - 不允许使用空管道元素