怎么用脚本获取系统启动模式

wen 实用脚本 1

本文目录导读:

怎么用脚本获取系统启动模式

  1. Windows 系统
  2. Linux 系统
  3. macOS 系统
  4. 跨平台 Python 脚本
  5. 使用说明
  6. 注意事项

Windows 系统

PowerShell 脚本

# 方法1:使用 bcdedit
$mode = bcdedit /enum {current} | Select-String "winload"
if ($mode -match "winload.efi") {
    Write-Output "启动模式: UEFI"
} else {
    Write-Output "启动模式: Legacy BIOS"
}
# 方法2:检查磁盘分区表格式
$disk = Get-Disk 0
if ($disk.PartitionStyle -eq "GPT") {
    Write-Output "启动模式: UEFI (GPT分区)"
} else {
    Write-Output "启动模式: Legacy BIOS (MBR分区)"
}
# 方法3:检查固件变量
if (Test-Path "C:\Windows\Panther\setupact.log") {
    $content = Get-Content "C:\Windows\Panther\setupact.log"
    if ($content -match "Detected boot environment: UEFI") {
        Write-Output "启动模式: UEFI"
    } else {
        Write-Output "启动模式: Legacy BIOS"
    }
}

Batch 脚本 (CMD)

@echo off
REM 方法1:检查系统目录
if exist %SystemRoot%\System32\winload.efi (
    echo 启动模式: UEFI
) else (
    echo 启动模式: Legacy BIOS
)
REM 方法2:使用 bcdedit
bcdedit /enum {current} | find "winload.efi" > nul
if %errorlevel% equ 0 (
    echo 启动模式: UEFI
) else (
    echo 启动模式: Legacy BIOS
)

Linux 系统

Bash 脚本

#!/bin/bash
# 方法1:检查 efivarfs 是否存在
if [ -d /sys/firmware/efi ]; then
    echo "启动模式: UEFI"
else
    echo "启动模式: Legacy BIOS"
fi
# 方法2:使用 dmidecode
if command -v dmidecode &> /dev/null; then
    mode=$(dmidecode -t 0 | grep -i "uefi\|bios")
    if echo "$mode" | grep -qi "uefi"; then
        echo "启动模式: UEFI"
    else
        echo "启动模式: Legacy BIOS"
    fi
fi
# 方法3:检查 /sys/class/dmi/id/sys_vendor
if grep -q "UEFI" /sys/class/dmi/id/sys_vendor 2>/dev/null; then
    echo "启动模式: UEFI"
else
    echo "启动模式: Legacy BIOS"
fi

更简洁的检测方法

#!/bin/bash
[ -d /sys/firmware/efi ] && echo "UEFI" || echo "Legacy BIOS"

macOS 系统

Shell 脚本

#!/bin/bash
# 方法1:检查系统报告
mode=$(system_profiler SPHardwareDataType | grep "Boot ROM Version" | awk '{print $4}')
if [[ $mode == *"UEFI"* ]]; then
    echo "启动模式: UEFI"
else
    echo "启动模式: Legacy BIOS"
fi
# 方法2:使用 nvram
if nvram -p | grep -q "firmware"; then
    echo "启动模式: UEFI (较新的Mac)"
else
    echo "启动模式: Legacy BIOS (较旧的Mac)"
fi

跨平台 Python 脚本

import platform
import os
def get_boot_mode():
    system = platform.system()
    if system == "Windows":
        # Windows 检测
        if os.path.exists("C:\\Windows\\System32\\winload.efi"):
            return "UEFI"
        else:
            return "Legacy BIOS"
    elif system == "Linux":
        # Linux 检测
        if os.path.isdir("/sys/firmware/efi"):
            return "UEFI"
        else:
            return "Legacy BIOS"
    elif system == "Darwin":
        # macOS 检测
        import subprocess
        result = subprocess.run(["system_profiler", "SPHardwareDataType"], 
                              capture_output=True, text=True)
        if "UEFI" in result.stdout:
            return "UEFI"
        else:
            return "Legacy BIOS"
    else:
        return "Unknown"
if __name__ == "__main__":
    print(f"启动模式: {get_boot_mode()}")

使用说明

  1. Windows: 以管理员权限运行脚本
  2. Linux: 需要适当的权限(运行 chmod +x script.sh
  3. macOS: 可能需要允许执行脚本

注意事项

  • Windows 下 bcdedit 需要管理员权限
  • Linux 下检测 /sys/firmware/efi 是最可靠的方法
  • 某些虚拟机可能显示不准确的信息

根据你的具体需求选择合适的脚本,最通用的是检查 /sys/firmware/efi(Linux)或 winload.efi(Windows)。

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