[英]Create Windows Explorer shortcut with Run As Administrator
[英]How to create a Run As Administrator shortcut using Powershell
在我的 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()
現在,當我創建快捷方式時,如何添加到腳本中以使其默認以管理員身份運行?
這個答案是對這個問題如何使用 JScript 創建一個使用“以管理員身份運行”的快捷方式的優秀答案的 PowerShell 翻譯。
簡而言之,您需要以字節數組的形式讀取 .lnk 文件。 定位字節 21 (0x15) 並將位 6 (0x20) 更改為 1。這是 RunAsAdministrator 標志。 然后將字節數組寫回 .lnk 文件。
在您的代碼中,這將如下所示:
$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 官方文檔。
您可以為此使用 -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.