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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在 Windows 系统中使用 Python 代码安装软件并输入密码,你可以使用 Python 的 `subprocess` 模块来调用命令行工具安装软件。 下面是一个示例代码: ```python import subprocess # 定义安装命令 command = "choco install <package-name> -y" # 使用 subprocess.run 执行命令 result = subprocess.run(command, shell=True, capture_output=True) # 输出命令的输出 print(result.stdout) # 如果命令执行失败,输出错误信息 if result.returncode != 0: print(result.stderr) ``` 在上面的代码中,我们使用了 `subprocess.run` 方法来执行命令。我们将 `shell` 参数设置为 `True`,表示我们希望在 shell 中执行命令。我们还将 `capture_output` 参数设置为 `True`,表示我们希望捕获命令的输出。 在上面的示例代码中,我们使用了 `choco` 命令行工具来安装软件。`choco` 是一个用于在 Windows安装软件的命令行工具,你可以使用它来安装软件包,卸载软件包,更新软件包等。 要使用 `choco` 命令行工具,你需要先安装它。你可以使用以下命令安装 `choco`: ``` @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\ ### 回答2: 在Windows系统中,安装软件通常需要管理员权限才能执行。如果希望用Python代码实现对该过程进行密码控制,可以借助subprocess模块和getpass模块实现。以下是一个示例代码: ```python import subprocess from getpass import getpass # 获取用户输入的密码 password = getpass("请输入管理员密码:") command = "msiexec /i C:\\path\\to\\installer.msi" # 替换为需要安装软件路径和文件名 try: # 使用subprocess模块执行命令,并附带管理员密码 process = subprocess.Popen(["powershell", "-Command", f"Start-Process -FilePath '{command}' -Verb RunAs"], stdin=subprocess.PIPE) process.communicate(password.encode()) # 将密码传递给子进程的stdin if process.returncode == 0: print("软件安装成功!") else: print("软件安装失败!") except Exception as e: print(f"安装过程出现错误:{e}") ``` 以上代码中,首先使用getpass模块获取用户输入的管理员密码,并保存在变量`password`中。然后使用subprocess.Popen方法调用powershell命令,附带`-Command`参数执行安装命令。在`communicate`方法中将密码传递给子进程的`stdin`。 执行后,程序会弹出UAC提示框,需要用户输入管理员密码。如果密码正确,安装命令将以管理员身份执行,如果失败则会提示相应的错误信息。 ### 回答3: 在Windows系统中,Python代码实现软件安装需要输入密码的控制可以通过调用系统命令来实现。 首先需要使用Python的`subprocess`模块来执行系统命令。我们可以使用`subprocess.run()`函数来执行命令,并通过参数`shell=True`来启用shell。 具体的步骤如下: 1. 导入subprocess模块: ```python import subprocess ``` 2. 编写代码来执行安装软件的命令。可以使用`subprocess.run()`函数执行命令,并捕获输出: ```python # 安装软件命令 command = 'msiexec /i path_to_installer.msi' # 执行命令,启用shell output = subprocess.run(command, shell=True, capture_output=True, text=True) ``` 需要注意的是,`'path_to_installer.msi'`应替换为待安装软件的路径。 3. 如果命令执行出错,即需要密码验证,可以通过判断输出结果来实现密码输入的控制: ```python if output.returncode != 0: # 需要密码验证 password = input('请输入密码:') # 拼接命令和密码 command_with_password = f'echo {password} | {command}' # 执行命令 output_with_password = subprocess.run(command_with_password, shell=True, capture_output=True, text=True) ``` 在代码中,我们首先判断命令执行的返回值`output.returncode`是否为0。如果不为0,表示命令执行出错,需要密码验证。 然后使用`input()`函数来读取输入的密码,并将密码和命令拼接起来,再次使用`subprocess.run()`函数来执行命令。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值