怎么用脚本获取当前电池模式

wen 实用脚本 3

本文目录导读:

怎么用脚本获取当前电池模式

  1. Windows 系统(PowerShell 脚本)
  2. macOS 系统(Shell 脚本)
  3. 跨平台方案:Python
  4. 注意事项:

获取当前电池模式(如省电模式、高性能模式等)的方法取决于你的操作系统,以下是针对 WindowsmacOS 的常用脚本方案:

Windows 系统(PowerShell 脚本)

Windows 可以通过查询电源计划(Power Scheme)的 GUID 来判断当前模式。

# 获取当前活动的电源计划
$currentPlan = powercfg /getactivescheme
# 提取 GUID(通常格式为:{GUID}  (计划名称))
if ($currentPlan -match '{([^}]+)}') {
    $guid = $matches[1]
    # 根据常见 GUID 判断模式
    switch ($guid.ToUpper()) {
        '381B4222-F694-41F0-9685-FF5BB260DF2E' { Write-Output "当前模式: 平衡 (Balanced)" }
        '8C5E7FDA-E8BF-4A96-9A85-A6E23A8C635C' { Write-Output "当前模式: 高性能 (High Performance)" }
        'A1841308-3541-4FAB-BC81-F71556F20B4A' { Write-Output "当前模式: 节能 (Power Saver)" }
        default { Write-Output "当前模式: 自定义或未知 (GUID: $guid)" }
    }
}

如果想封装成函数,方便其他脚本调用:

function Get-BatteryMode {
    $currentPlan = powercfg /getactivescheme
    if ($currentPlan -match '{([^}]+)}') {
        $guid = $matches[1].ToUpper()
        $mode = switch ($guid) {
            '381B4222-F694-41F0-9685-FF5BB260DF2E' { 'Balanced' }
            '8C5E7FDA-E8BF-4A96-9A85-A6E23A8C635C' { 'HighPerformance' }
            'A1841308-3541-4FAB-BC81-F71556F20B4A' { 'PowerSaver' }
            default { 'Custom' }
        }
        return $mode
    }
    return $null
}
# 使用示例
$mode = Get-BatteryMode
Write-Output "当前电池模式: $mode"

macOS 系统(Shell 脚本)

macOS 可以使用 pmset 命令获取电源管理设置。

#!/bin/bash
# 获取当前电源管理状态(包含电量信息)
pmset -g batt | head -n 1
# 更详细地获取正在使用的电源模式
# 'AC Power' 表示插电,'Battery Power' 表示用电池
current_mode=$(pmset -g | grep -E '^[-]power|^[Cc]urrent|^[Bb]attery|^[Aa]C')
# 检查是否处于低功耗模式(Low Power Mode)
# 在较新的 macOS 版本(如 macOS 11+)中
if pmset -g | grep -q "lowpowermode"; then
    low_power=$(pmset -g | grep "lowpowermode" | awk '{print $2}')
    if [ "$low_power" = "1" ]; then
        echo "当前模式: 节能模式 (Low Power Mode)"
    else
        echo "当前模式: 正常模式 (Normal)"
    fi
else
    # 旧版 macOS 或没有低功耗模式
    echo "当前电源状态: 请查看 pmset 输出"
    pmset -g | head -5
fi

跨平台方案:Python

如果你的环境可以运行 Python,可以使用 subprocess 模块:

import subprocess
import platform
def get_windows_power_mode():
    try:
        result = subprocess.run(['powercfg', '/getactivescheme'], 
                               capture_output=True, text=True)
        if result.returncode == 0 and result.stdout:
            # 提取 GUID
            import re
            match = re.search(r'{([^}]+)}', result.stdout)
            if match:
                guid = match.group(1).upper()
                modes = {
                    '381B4222-F694-41F0-9685-FF5BB260DF2E': '平衡',
                    '8C5E7FDA-E8BF-4A96-9A85-A6E23A8C635C': '高性能',
                    'A1841308-3541-4FAB-BC81-F71556F20B4A': '节能'
                }
                return modes.get(guid, f'自定义({guid})')
    except:
        pass
    return '未知'
def get_macos_power_mode():
    try:
        result = subprocess.run(['pmset', '-g', 'batt'], 
                               capture_output=True, text=True)
        if 'Battery Power' in result.stdout:
            # 检查低功耗模式
            low_result = subprocess.run(['pmset', '-g'], 
                                       capture_output=True, text=True)
            if 'lowpowermode 1' in low_result.stdout:
                return '节能模式 (低功耗)'
            return '电池模式 (正常)'
        elif 'AC Power' in result.stdout:
            return '交流电模式 (已插电)'
    except:
        pass
    return '未知'
# 主逻辑
system = platform.system()
if system == 'Windows':
    print(f"当前电池模式: {get_windows_power_mode()}")
elif system == 'Darwin':  # macOS
    print(f"当前电源模式: {get_macos_power_mode()}")
else:
    print(f"不支持的平台: {system}")

注意事项:

  1. Windows 注意事项

    • 方法只检测标准电源计划,如果用户安装了笔记本厂商的自定义电源管理软件,实际模式可能不同
    • 需要以普通用户权限运行(无需管理员)
  2. macOS 注意事项

    • 低功耗模式在较新版本的 macOS(如 Big Sur 及以上)才存在
    • pmset -g 输出详细,可以根据需要提取具体字段
  3. 自动化脚本建议

    • 可以添加循环定时检查(如每 30 秒),用于监控模式变化
    • 配合事件通知(如 Windows 下的 Register-ObjectEvent)实时响应模式切换

根据你的具体需求(是 Windows 还是 macOS,是否需要实时监控),可以选择上述任一方案,如果需要适配特定场景(如笔记本带独显的切换模式),可能需要追加查询硬件状态。

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