简体   繁体   English

如何使用 Powershell 创建以管理员身份运行的快捷方式

[英]How to create a Run As Administrator shortcut using Powershell

In my PowerShell script, I create a shortcut to a .exe (using something similar to the answer from this question ):在我的 PowerShell 脚本中,我创建了一个 .exe 的快捷方式(使用类似于这个问题的答案):

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()

Now, when I create the shortcut, how do I add to the script to make it default to running as Administrator?现在,当我创建快捷方式时,如何添加到脚本中以使其默认以管理员身份运行?

This answer is a PowerShell translation of an excellent answer to this question How can I use JScript to create a shortcut that uses "Run as Administrator" .这个答案是对这个问题如何使用 JScript 创建一个使用“以管理员身份运行”的快捷方式的优秀答案的 PowerShell 翻译。

In short, you need to read the .lnk file in as an array of bytes.简而言之,您需要以字节数组的形式读取 .lnk 文件。 Locate byte 21 (0x15) and change bit 6 (0x20) to 1. This is the RunAsAdministrator flag.定位字节 21 (0x15) 并将位 6 (0x20) 更改为 1。这是 RunAsAdministrator 标志。 Then you write you byte array back into the .lnk file.然后将字节数组写回 .lnk 文件。

In your code this would look like this:在您的代码中,这将如下所示:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\ColorPix.lnk")
$Shortcut.TargetPath = "C:\Program Files (x86)\ColorPix\ColorPix.exe"
$Shortcut.Save()

$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\ColorPix.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$Home\Desktop\ColorPix.lnk", $bytes)

If anybody want to change something else in a .LNK file you can refer to official Microsoft documentation .如果有人想更改.LNK文件中的其他内容,您可以参考Microsoft 官方文档

You can use the -Elevate true switch for this:您可以为此使用 -Elevate true 开关:

    CreateShortcut -name "myApp" -Target 
    "${env:ProgramFiles}\mApp\myApp.exe" -OutputDirectory 
    "C:\Users\Public\Desktop" -Elevated True

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Run As Administrator创建Windows资源管理器快捷方式 - Create Windows Explorer shortcut with Run As Administrator 如何使用Powershell在桌面上创建磁盘快捷方式? - How to create Disk shortcut on desktop using Powershell? 自动将“以管理员身份运行”设置为快捷方式 - Set “Run as Administrator” for shortcut automaticlly 如何以管理员身份在Powershell中运行命令以安装Weblogic 12c并克服其作为管理员运行的影响 - How to run command in Powershell as a Administrator to installl Weblogic 12c and to overcome its implications as to run as a Administrator 如何使用 python 以管理员权限运行 cmd - How to run cmd with administrator privilege using python 如何在Windows中创建上下文菜单条目以使用管理员权限打开PowerShell? - How to create a context menu entry, in windows, to open PowerShell with administrator privileges? 如何从启用了“以管理员身份运行”的基于 WiX 的安装程序安装桌面快捷方式(到批处理文件)? - How to install a desktop shortcut (to a batch file) from a WiX-based installer that has “Run as Administrator” enabled? 如何在Windows中创建运行PHP代码的快捷方式? - How to create a shortcut to run PHP code in Windows? 如何将 Powershell 中的 Windows 快捷方式创建到文件名中包含特殊字符的文件? - How to create a Windows Shortcut in Powershell to a file with special characters in filename? 快捷方式路径名和“以管理员身份运行”中的特殊字符是否存在已知限制? - Any known limitations to special characters in shortcut pathname and “Run as Administrator”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM