如何使用Powershell创建运行方式管理员快捷方式 [英] How to create a Run As Administrator shortcut using Powershell
问题描述
在我的PowerShell脚本中,我创建一个.exe的快捷方式(使用类似于来自 this question ):
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("$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?
推荐答案
这个答案是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("$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)
如果任何人想要更改 .LNK
文件中的其他内容,您可以参考官方Microsoft文档。
If anybody want to change something else in a .LNK
file you can refer to official Microsoft documentation.
这篇关于如何使用Powershell创建运行方式管理员快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!