以特定的域管理员身份运行Python代码

问题描述

我正在尝试制作一个Python文件,以将某些文件复制到System32和System并从非管理员用户运行。我只需要在代码中输入管理员用户名密码,以使UAC不会显示并且文件将被成功复制,因为UAC设置为始终隐藏在我们的计算机上。

from pathlib import Path
from tkinter.filedialog import askdirectory
from tkinter import messageBox
import os,shutil
import ctypes,sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    source = Path(askdirectory(title='select source'))
    destination1 = Path('C:/Windows/System/')
    destination2 = Path('C:/Windows/System32/')
    try:
        copyTree(source,destination1) # Custom copy Fucnction
        messageBox.showinfo("Completed","copy To System Succesful")
        copyTree(source,destination2)
        messageBox.showinfo("Completed","copy To System32 Succesful")
    except FileExistsError:
        messageBox.showerror("Error","Please Try Elevation")
else:
    # Re-run the program with admin rights====> I Need Here To Put The Domain Admin User And Password Instead Of Just Making UAC Show
    ctypes.windll.shell32.ShellExecuteW(None,"runas",sys.executable," ".join(sys.argv),None,0)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)