Windows系统实现普通用户临时安装软件授权

import time

import pyautogui
import win32api
import requests
import win32con

import os
import subprocess


# 获取本机管理员名称:每台电脑的管理员可能不同,需要根据具体情况修改代码,以下是本公司常用的两个管理员名称
def get_windows_users():
    users = []
    cmd = 'net user'
    output = subprocess.check_output(cmd, shell=True).decode('utf-8', errors="ignore")
    lines = output.split(' ')
    for line in lines:
        if 'User accounts' in line or '---------' in line:
            continue
        if line.strip() != '':
            users.append(line.split()[0])
    if 'sysadmin' in users:
        admin_user = 'Windows管理员名称1'
    else:
        admin_user = 'Windows管理员名称2'
    return admin_user


# 检查url是否可用 官网地址不可用,调用本地服务器上传的安装包
def check_url(url):
    try:
        response = requests.get(url).status_code
        if response == 200:
            # 需要安装软件的官网地址
            url = 'https://work.weixin.qq.com/wework_admin/commdownload?platform=win&from=wwindex'
            return url
        else:
            url = 'http://内网服务器IP/WeCom_4.1.22.6009.exe'
            return url
    except Exception as e:
        url = 'http://内网服务器IP/WeCom_4.1.22.6009.exe'
        return url


# 安装软件
def install_software():
    cl = check_url(url='https://work.weixin.qq.com/')
    print(cl)
    try:
        win32api.MessageBox(0, '开始下载软件', '下载软件', win32con.MB_OK)
        response = requests.get(cl)
        software_path = r'文件路径\WXWork_latest.exe' #文件下载后保存的路径和软件名
        with open(software_path, 'wb') as f:
            f.write(response.content)
        print('软件下载完成')
        win32api.MessageBox(0, '软件下载完成,是否安装', '安装软件', win32con.MB_OK)
        windows_admin_user = get_windows_users()
        # 执行管理员安装命令
        os.system("start cmd /k runas /user:" + windows_admin_user + " \"{}\"".format(software_path))
        time.sleep(0.5)
        # 模拟输入密码
        password = "123456789"  # 替换为你的管理员密码
        for char in password:
            pyautogui.typewrite(char)
            time.sleep(0.05)  # 添加适当的延迟以模拟人类输入速度

        # 模拟按下回车键以提交密码
        pyautogui.press('enter')
    except Exception as e:
        print(e)


install_software()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值