怎么用脚本获取主板序列号

wen 实用脚本 3

本文目录导读:

怎么用脚本获取主板序列号

  1. Windows 系统
  2. Linux 系统
  3. macOS 系统
  4. Python 脚本(跨平台)
  5. 综合脚本(自动检测系统)
  6. 注意事项

Windows 系统

PowerShell 方法(推荐)

# 方法1:使用 WMI
Get-WmiObject Win32_BaseBoard | Select-Object SerialNumber
# 方法2:使用 CIM(更现代)
Get-CimInstance Win32_BaseBoard | Select-Object SerialNumber
# 方法3:获取完整主板信息
Get-CimInstance Win32_BaseBoard | Format-List Manufacturer,Product,SerialNumber,Version

CMD 批处理脚本

@echo off
wmic baseboard get serialnumber
pause

VBScript 方法

strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BaseBoard")
For Each objItem in colItems
    WScript.Echo "主板序列号: " & objItem.SerialNumber
Next

Linux 系统

使用 dmidecode(需要 root 权限)

#!/bin/bash
# 获取主板序列号
sudo dmidecode -s baseboard-serial-number
# 或者获取完整主板信息
sudo dmidecode -t baseboard

使用 sysfs(无需 root)

#!/bin/bash
# 直接从 /sys 目录读取
cat /sys/class/dmi/id/board_serial

macOS 系统

使用 system_profiler

#!/bin/bash
# 获取硬件信息
system_profiler SPHardwareDataType | grep "Serial Number"
# 更精确地获取主板信息
system_profiler SPHardwareDataType | grep "Board ID"

Python 脚本(跨平台)

Windows 版本

import subprocess
import re
def get_windows_mb_serial():
    try:
        output = subprocess.check_output(
            "wmic baseboard get serialnumber",
            shell=True,
            text=True
        )
        # 解析输出
        lines = output.strip().split('\n')
        for line in lines:
            if line.strip() and 'SerialNumber' not in line:
                return line.strip()
    except Exception as e:
        return f"错误: {e}"
    return "未找到"
print(f"主板序列号: {get_windows_mb_serial()}")

Linux/macOS 版本

import subprocess
import os
def get_linux_mb_serial():
    try:
        if os.path.exists('/sys/class/dmi/id/board_serial'):
            with open('/sys/class/dmi/id/board_serial', 'r') as f:
                return f.read().strip()
        else:
            output = subprocess.check_output(
                ['sudo', 'dmidecode', '-s', 'baseboard-serial-number'],
                text=True
            )
            return output.strip()
    except Exception as e:
        return f"错误: {e}"
def get_mac_mb_serial():
    try:
        output = subprocess.check_output(
            ['system_profiler', 'SPHardwareDataType'],
            text=True
        )
        for line in output.split('\n'):
            if 'Serial Number' in line or 'Board ID' in line:
                return line.split(':')[1].strip()
    except Exception as e:
        return f"错误: {e}"
    return "未找到"
# 根据系统调用相应函数
import platform
system = platform.system()
if system == 'Windows':
    print(f"主板序列号: {get_windows_mb_serial()}")
elif system == 'Darwin':
    print(f"主板序列号: {get_mac_mb_serial()}")
else:
    print(f"主板序列号: {get_linux_mb_serial()}")

综合脚本(自动检测系统)

#!/bin/bash
# 跨平台主板序列号获取脚本
detect_os() {
    case "$OSTYPE" in
        msys*|cygwin*|win32*) echo "windows" ;;
        darwin*) echo "mac" ;;
        linux*) echo "linux" ;;
        *) echo "unknown" ;;
    esac
}
OS_TYPE=$(detect_os)
case $OS_TYPE in
    windows)
        wmic baseboard get serialnumber | grep -v "SerialNumber" | grep -v "^$"
        ;;
    mac)
        system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $NF}'
        ;;
    linux)
        if [ -r /sys/class/dmi/id/board_serial ]; then
            cat /sys/class/dmi/id/board_serial
        else
            # 需要root权限
            if [ "$(id -u)" -eq 0 ]; then
                dmidecode -s baseboard-serial-number
            else
                echo "需要root权限,请使用: sudo $0"
                exit 1
            fi
        fi
        ;;
    *)
        echo "不支持的操作系统"
        exit 1
        ;;
esac

注意事项

  1. 权限问题:Linux 下有时需要 sudo 权限
  2. 虚拟环境:在虚拟机中可能返回 "To be filled by O.E.M." 等默认值
  3. 安全考虑:某些主板可能隐藏或加密序列号
  4. 编码问题:某些中文系统可能显示乱码,可尝试设置编码
  5. 网络环境:某些企业管理的计算机可能禁用 WMI 查询

选择最适合你需求的脚本即可,如果遇到权限问题,请以管理员/root 身份运行。

抱歉,评论功能暂时关闭!