如何使用 Powershell 创建以管理员身份运行的快捷方式 [英] How to create a Run As Administrator shortcut using Powershell

查看:50
本文介绍了如何使用 Powershell 创建以管理员身份运行的快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 PowerShell 脚本中,我创建了一个 .exe 的快捷方式(使用类似于 这个问题):

In my PowerShell script, I create a shortcut to a .exe (using something similar to the answer from this question):

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

现在,当我创建快捷方式时,如何添加到脚本中以使其默认以管理员身份运行?

Now, when I create the shortcut, how do I add to the script to make it default to running as Administrator?

推荐答案

这个答案是这个问题的一个优秀答案的 PowerShell 翻译如何使用JScript 创建使用以管理员身份运行"的快捷方式.

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".

简而言之,您需要以字节数组的形式读取 .lnk 文件.定位字节 21 (0x15) 并将位 6 (0x20) 更改为 1.这是 RunAsAdministrator 标志.然后将字节数组写回 .lnk 文件.

In short, you need to read the .lnk file in as an array of bytes. Locate byte 21 (0x15) and change bit 6 (0x20) to 1. This is the RunAsAdministrator flag. Then you write you byte array back into the .lnk file.

在您的代码中,这看起来像这样:

In your code this would look like this:

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

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

如果有人想更改 .LNK 文件中的其他内容,您可以参考 微软官方文档.

If anybody want to change something else in a .LNK file you can refer to official Microsoft documentation.

这篇关于如何使用 Powershell 创建以管理员身份运行的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆