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

2024-10-02 10:20:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个需要管理员权限的快捷方式。。。我在网上找到了这个代码,但它是在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代码:

^{pr2}$

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


Tags: the代码youhomebytesbitbytearray
2条回答

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

{e使用命令^模拟}的行为

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

相关问题 更多 >

    热门问题