一键获取电脑的系统安装时间、硬盘序列号、MAC地址、补丁安装信息

查询Windows电脑的操作系统安装时间、硬盘序列号、MAC地址、补丁安装数量、和最新补丁安装时间,都有相应的命令行查询命令。但是一个一个查询有点麻烦,用Python写了一个一键查询的方法。

可将如下代码保存为计算机自查.py,使用Python运行:

import os
import re
import time
import winreg
from contextlib import suppress

popen = lambda cmd: os.popen(cmd).read()

with suppress(Exception):
    key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
    install_date = winreg.QueryValueEx(key, 'InstallDate')[0]
    print('操作系统安装日期为:', '%d-%d-%d' % time.localtime(install_date)[:3])

with suppress(Exception):
    raw = popen('wmic diskdrive get serialnumber')
    serial_hex = raw.split()[-1].strip()
    serial_num = bytes.fromhex(serial_hex).decode()
    serial_num = re.sub(r'(.)(.)', r'\2\1', serial_num).strip()
    print("硬盘序列号为:", serial_num)

with suppress(Exception):
    raw = popen('ipconfig /all')
    mac_address = re.findall(r'[0-9A-F-]{17}', raw)[0]
    print('MAC地址为:', mac_address)

with suppress(Exception):
    raw = popen('wmic qfe list full')
    patch_dates = re.findall('InstalledOn=(\d+)/(\d+)/(\d+)', raw)
    patch_dates = [(int(y), int(m), int(d)) for m, d, y in patch_dates]
    print('已安装的补丁数量为:', len(patch_dates))
    print('最新补丁安装时间为:', '%d-%d-%d' % max(patch_dates))

运行效果如下:

操作系统安装日期为: 2019-12-7
MAC地址为: 52-1A-C5-EA-EB-B8
硬盘序列号为: S1F0NYAF632347
已安装的补丁数量为: 13
最新补丁安装时间为: 2023-3-2

你可能感兴趣的:(Python,Windows,python,windows)