如何获得管理员权限,以便用Python在csv文件中编写Windows用户名?

时间:2022-07-27 20:20:37

I want to write Windows usernames in a csv file. I wrote this script in Linux, connected to Win10 PC via krdc and RDP protocol. I shared my Linux drive over that remote desktop app with Win10 and then I ran this scipt from that shared disk in Win10.

我想在csv文件中写入Windows用户名。我用Linux编写了这个脚本,通过krdc和RDP协议连接到Win10 PC。我在Win10的远程桌面应用程序上共享我的Linux驱动器,然后从Win10的共享磁盘上运行这个scipt。

If I do this via command prompt as administrator it runs as expected. However, if I run it as a normal user and UAC is confirmed as Yes, the output file is not created.

如果我作为管理员通过命令提示符执行此操作,它将按预期运行。但是,如果我作为一个普通用户运行它,并且UAC被确认为Yes,那么输出文件就不会被创建。

How to run this script as a normal user, answering yes to UAC and having that file writen in my mounted drive over rdp protocol in krdc remote app where Win10 sees that drive as \\TSCLIENT?

如何以普通用户的身份运行这个脚本,向UAC应答yes,并在我的已安装驱动器中通过krdc远程应用程序的rdp协议写入该文件,Win10将该驱动器视为\TSCLIENT?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import ctypes
import sys
import subprocess


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

if is_admin():
    subprocess.call(['wmic', '/output:usracc.txt', 'useraccount', 'list', 'full', '/format:csv'])
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

1 个解决方案

#1


2  

If you want to do this with out having administrator privileges you can:

如果你想在拥有管理员权限的情况下这样做,你可以:

import os
import csv

userList = os.popen("net user").read().split()[5:-4]

outFile = open("userList.csv", 'w')
with outFile:
    fileWriter = csv.writer(outFile)

for user in userList:
    print(user)
    fileWriter.writerow([user]) 

That code will give you a file "userList.csv" with all the users without being an administrator

该代码将提供一个文件“userList”。csv"包含所有用户而不是管理员

#1


2  

If you want to do this with out having administrator privileges you can:

如果你想在拥有管理员权限的情况下这样做,你可以:

import os
import csv

userList = os.popen("net user").read().split()[5:-4]

outFile = open("userList.csv", 'w')
with outFile:
    fileWriter = csv.writer(outFile)

for user in userList:
    print(user)
    fileWriter.writerow([user]) 

That code will give you a file "userList.csv" with all the users without being an administrator

该代码将提供一个文件“userList”。csv"包含所有用户而不是管理员