如何让Python脚本在Windows中以管理员身份运行?

问题描述 投票:0回答:1

我尝试将用于激活 Windows 的批处理脚本嵌入到 Python 脚本中,但该脚本需要在 Windows 中以管理员身份运行,所以我找到了不同的解决方案,但没有一个有效。这是最新的代码:

import subprocess
import sys

batch_script = """
@echo off
set "scriptver=v0.1.2"

:setup
>nul reg add hkcu\software\classes\.Admin\shell\runas\command /f /ve /d "cmd /x /d /r set \"f0=%%2\"&                   call \"%%2\" %%3"& set _= %*
>nul fltmc|| if "%f0%" neq "%~f0" (cd.>"%temp%\\runas.Admin" & start "%~n0" /high "%temp%\\runas.Admin" "%~f0" "%_:"=""%" & exit /b)

title Windows 11 Activate
color 17
:front

echo ------------------------------------------------------------------------------------
echo                                   Windows 11 Activate 
echo ------------------------------------------------------------------------------------
echo Choose an option:
echo A) Activate Windows 11 Pro                             H) Activate Windows 11 Education N
echo B) Activate Windows 11 Home                            I) Activate Windows 11 Home Single Language
echo C) Activate Windows 11 Pro N                           J) Activate Windows 11 Home Country Specific
echo D) Activate Windows 11 Home N                          K) Activate Windows 11 Pro for Workstations
echo E) Activate Windows 11 Enterprise                      L) Activate Windows 11 Pro for Workstations N
echo F) Activate Windows 11 Enterprise N                    M) Remove product key
echo G) Activate Windows 11 Education                       N) Exit without do any change
choice /c ABCDEFGHIJKLMN /m "Enter your choice: " 
if %errorlevel% equ 1 goto OptionA
if %errorlevel% equ 2 goto OptionB
if %errorlevel% equ 3 goto optionC
if %errorlevel% equ 4 goto optionD
if %errorlevel% equ 5 goto optionE
if %errorlevel% equ 6 goto optionF
if %errorlevel% equ 7 goto optionG
if %errorlevel% equ 8 goto optionH
if %errorlevel% equ 9 goto optionI
if %errorlevel% equ 10 goto optionJ
if %errorlevel% equ 11 goto OptionK
if %errorlevel% equ 12 goto OptionL
if %errorlevel% equ 13 goto OptionM
if %errorlevel% equ 14 goto OptionN
:optionA
echo Activating Windows 11 Pro...
cscript.exe c:\\windows\\system32\\slmgr.vbs /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX
cscript.exe c:\\windows\\system32\\slmgr.vbs /skms kms8.msguides.com
cscript.exe c:\\windows\\system32\\slmgr.vbs /ato
cscript.exe c:\\windows\\system32\\slmgr.vbs /xpr
echo Successfully activate Windows 11 Pro
timeout /t 10 & pause
goto front
"""

# Save the Batch script to a temporary file
with open('temp_batch_script.bat', 'w') as batch_file:
    batch_file.write(batch_script)

# Run the Batch script as a subprocess   
try:
    subprocess.run(['cmd', '/c', 'temp_batch_script.bat'], check=True)
except subprocess.CalledProcessError as e:
    print(f"Error running Batch script: {e}")
finally:
# Clean up temporary file
    subprocess.run(['del', 'temp_batch_script.bat'], shell=True, check=True)

我尝试在网上寻找解决方案并应用它们,但我发现 3 个解决方案都不起作用。

python windows batch-file administrator
1个回答
0
投票

创建一个名为

run.py
的新文件并为其提供以下内容:

imort os    
os.system("runas /user:Administrator \"path/to/your/first.file.py\"")

运行此文件而不是第一个文件,它将以管理员身份运行。

© www.soinside.com 2019 - 2024. All rights reserved.