使用 运行 作为管理员创建快捷方式

create shortcut with Run As Administrator

我正在尝试创建一个需要管理员权限的快捷方式...我在互联网上找到了这段代码,但它是在 powershell 中编码的...我测试了它的工作原理!但我在 python 中需要它 我如何用 python

做同样的事情

powershell 代码:

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.

$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)

这是我的 python 代码:

filename = r'C:\Users\root\Desktop\qassam.lnk'

with open(filename, "rb") as f2:

    while True:
      current_byte = f2.read(1)
      if (not current_byte):
        break
      val = ord(current_byte)
      q = hex(val)
      print q 

我不知道下一步是什么,你能帮我吗?

您可以使用库来编辑 .lnk 文件(我未检查):

或使用 bytearray

模拟命令的行为
filename = r'C:\Users\root\Desktop\qassam.lnk'

# $bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\ColorPix.lnk")
with open(filename, "rb") as f2:
  ba = bytearray(f2.read())

# $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
ba[0x15] = ba[0x15] | 0x20

# [System.IO.File]::WriteAllBytes("$Home\Desktop\ColorPix.lnk", $bytes)
with open(filename, "wb") as f3:
  f3.write(ba)

此代码生成了快捷方式。但是我不明白你到底想要什么。

代码:

import os

def short(Shortcut_Path, _Path):
    Shortcut = """
    $WshShell = New-Object -comObject WScript.Shell
    $Shortcut = $WshShell.CreateShortcut("{}")
    $Shortcut.TargetPath = "{}"
    $Shortcut.Save()""".format(Shortcut_Path, _Path)

    open("Shortcut.ps1", "w").write(Shortcut)
    os.system("Shortcut.ps1")
    os.remove("Shortcut.ps1")

short(os.environ["USERPROFILE"] + r"\Desktop\AIMP.lnk", r"C:\Program Files 
(x86)\AIMP\AIMP.exe")