PowerShell/WMI 电源管理

标签 powershell wmi power-management wmi-query

我是 Windows 电源管理的新手,甚至是 WMI 的新手,但总是欢迎学习机会。

使用 PowerShell 和 WMI 我想将“高性能”电源计划的“盖子关闭操作”、“电源按钮操作”和“ sleep 按钮操作”设置为“什么都不做”选项,最后将事件电源计划设置为“高”性能”电源计划。我有一个可行的解决方案,但我怀疑它是否是一个合适的解决方案。

我的问题的关键是所有计划、子组、操作、可用值等都用 GUID 标识,我读到这些 GUID 可能因系统而异(特别是如果通过组策略应用) .) 在我的解决方案中,我设法避免硬编码 GUID 只是为了结束硬编码值,例如“电源和盖子按钮”、“盖子关闭操作”、“什么都不做”等。这些值可能会或可能不会有所不同非英语版本的 Windows。 (顺便说一下,我使用的是 Windows 8.1。)

我的问题

以编程方式发现每个计划、子组、操作、可用值等的 GUID 的正确方法是什么?

引用

在使用 powercfg.exe 工具的 BATCH 中,命令将是:

:: Set the lid close action, power button action and sleep button action to do nothing

powercfg /SETACVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg /SETDCVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 5ca83367-6e45-459f-a27b-476b1d01c936 0
powercfg /SETACVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 0
powercfg /SETDCVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 7648efa3-dd9c-4e3e-b566-50f929386280 0
powercfg /SETACVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 96996bc0-ad50-47ec-923b-6f41874dd9eb 0
powercfg /SETDCVALUEINDEX 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c 4f971e89-eebd-4455-a8de-9e59040e7347 96996bc0-ad50-47ec-923b-6f41874dd9eb 0
powercfg /SETACTIVE 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c

我的 PowerShell/WMI 解决方案是这样的:
$CommonArgs = @{"namespace"="root\cimv2\power"}
$CommonArgs += $PSBoundParameters

function Set-PowerSettingDataIndexValue
{
    <#
    .Synopsis
        Sets the value associated with a specified power setting for both AC and DC power.
    .Description
        This function is somewhat similar to running the two commands
        'POWERCFG /SETACVALUEINDEX <SCHEME_GUID> <SUB_GUID> <SETTING_GUID> <SETTING_INDEX>'
        and
        'POWERCFG /SETDCVALUEINDEX <SCHEME_GUID> <SUB_GUID> <SETTING_GUID> <SETTING_INDEX>'
        except that the <SUB_GUID> is implied by the $PowerSettingDefinitionGuid
    .Example
        Set-PowerSettingDataIndexValue "{8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c}" "{7648efa3-dd9c-4e3e-b566-50f929386280}" 3
    #>

    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true)]
        [string]$PowerPlanGuid,
        [parameter(Mandatory=$true)]
        [string]$PowerSettingDefinitionGuid,
        [parameter(Mandatory=$true)]
        [string]$SettingIndexValue
    )

    $PowerSettingDataIndexAC = Get-CimInstance -ClassName Win32_PowerSettingDataIndex -Filter "InstanceID = 'Microsoft:PowerSettingDataIndex\\$PowerPlanGuid\\AC\\$PowerSettingDefinitionGuid'" @CommonArgs
    Set-CimInstance -InputObject $PowerSettingDataIndexAC -Property @{ SettingIndexValue=$SettingIndexValue }

    $PowerSettingDataIndexDC = Get-CimInstance -ClassName Win32_PowerSettingDataIndex -Filter "InstanceID = 'Microsoft:PowerSettingDataIndex\\$PowerPlanGuid\\DC\\$PowerSettingDefinitionGuid'" @CommonArgs
    Set-CimInstance -InputObject $PowerSettingDataIndexDC -Property @{ SettingIndexValue=$SettingIndexValue }
}

function Set-PowerButtonAndLidActions
{
    <#
    .Synopsis
        Sets the power button, sleep button and lid actions to do nothing effectively disabling these buttons.
    .Description
        This function modifies every existing power plan to effectively disable the power button, sleep button and
        lid action. Additonally this function will set the "high performance" power plan as the active power plan.
    .Example
        Set-PowerButtonAndLidActions
    #>

    # Get the power setting subgroup where the element name is 'Power buttons and lid'
    $PowerSettingSubgroup = Get-CimInstance -ClassName Win32_PowerSettingSubgroup -Filter "ElementName = 'Power buttons and lid'" @CommonArgs

    # Get the power setting definitions for 'Lid close action', 'Power button action' and 'Sleep button action'
    $PowerSettingDefinitionLidCloseAction = Get-CimAssociatedInstance -InputObject $PowerSettingSubgroup | where { $_.ElementName -eq "Lid close action" }
    $PowerSettingDefinitionPowerButtonAction = Get-CimAssociatedInstance -InputObject $PowerSettingSubgroup | where { $_.ElementName -eq "Power button action" }
    $PowerSettingDefinitionSleepButtonAction = Get-CimAssociatedInstance -InputObject $PowerSettingSubgroup | where { $_.ElementName -eq "Sleep button action" }

    # Extract the GUID from each action's instance ID
    $PowerSettingDefinitionLidCloseActionGuid = $PowerSettingDefinitionLidCloseAction.InstanceID -replace '.*({[^}]+})', '$1'
    $PowerSettingDefinitionPowerButtonActionGuid = $PowerSettingDefinitionPowerButtonAction.InstanceID -replace '.*({[^}]+})', '$1'
    $PowerSettingDefinitionSleepButtonActionGuid = $PowerSettingDefinitionSleepButtonAction.InstanceID -replace '.*({[^}]+})', '$1'

    # Get the value of the 'Do Nothing' option for each power setting definition
    $LidCloseActionDoNothing = Get-CimInstance -Query "SELECT * FROM Win32_PowerSettingDefinitionPossibleValue WHERE ElementName = 'Do nothing' AND InstanceID LIKE '%\\$PowerSettingDefinitionLidCloseActionGuid\\%'" @CommonArgs
    $PowerButtonActionDoNothing = Get-CimInstance -Query "SELECT * FROM Win32_PowerSettingDefinitionPossibleValue WHERE ElementName = 'Do nothing' AND InstanceID LIKE '%\\$PowerSettingDefinitionPowerButtonActionGuid\\%'" @CommonArgs
    $SleepButtonActionDoNothing = Get-CimInstance -Query "SELECT * FROM Win32_PowerSettingDefinitionPossibleValue WHERE ElementName = 'Do nothing' AND InstanceID LIKE '%\\$PowerSettingDefinitionSleepButtonActionGuid\\%'" @CommonArgs

    $p = Get-CimInstance -ClassName Win32_PowerPlan -Filter "ElementName = 'High performance'" @CommonArgs

    # Extract the GUID from the power plan's InstanceID property
    $PlanGuid = $p.InstanceID -replace '.*({[^}]+})', '$1'

    Set-PowerSettingDataIndexValue -PowerPlanGuid $PlanGuid -PowerSettingDefinitionGuid $PowerSettingDefinitionLidCloseActionGuid $LidCloseActionDoNothing.SettingIndex
    Set-PowerSettingDataIndexValue -PowerPlanGuid $PlanGuid -PowerSettingDefinitionGuid $PowerSettingDefinitionPowerButtonActionGuid $PowerButtonActionDoNothing.SettingIndex
    Set-PowerSettingDataIndexValue -PowerPlanGuid $PlanGuid -PowerSettingDefinitionGuid $PowerSettingDefinitionSleepButtonActionGuid $SleepButtonActionDoNothing.SettingIndex

    Invoke-CimMethod -InputObject $p -MethodName Activate | Out-Null
}

Set-PowerButtonAndLidActions

最佳答案

您可以使用 Get-CIMInstance列出几乎所有这些。电源计划都在Win32_PowerPlan类,所以:

Get-CimInstance -classname Win32_PowerPlan -Namespace "root\cimv2\power"

这将列出计算机上的所有计划。您可以在 https://msdn.microsoft.com/en-us/library/dd904531(v=vs.85).aspx 上找到所有这些信息。 .甚至还有 PowerShell 示例。

从那里你只需要解析出你需要的信息。 ElementName属性显示有人在进入控制面板中的电源设置 GUI 时会看到什么。 InstanceID是您可以找到 GUID 的地方,但您可能需要执行以下操作:
Get-CimInstance -Namespace "root\cimv2\power" -class Win32_PowerPlan|Select ElementName,@{l='GUID';e={$_.instanceid.substring(20)}}

其余的应该在 Win32_PowerSetting 中找到和 Win32_PowerSettingSubgroup .您应该能够浏览该站点以查找其他相关类(class),以获取您需要的信息。

关于PowerShell/WMI 电源管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35325862/

相关文章:

string - 在大文件中高效地搜索字符串

wmi - 通过 WMI 启用 UWF

windows - Win32_PhysicalMemory.Capactiy——它在 32 位应用程序中的表现如何?

linux-kernel - 关于 Linux 每个 cpu 核心 PID0 空闲任务

azure - Powershell - 从 -out 表中仅选择一个值

json - 通过 CLI 进行 Azure ARM 模板部署失败,并出现 .ps1 脚本扩展错误

powershell - pwsh-命令正在删除引号

powershell - 使用 WMI 获取 IIS6 网站的所有虚拟目录

android - 如果使用网络位置提供程序(相对于 GPS),电池功耗是否有优势?

android - 延迟暂停、早期恢复和唤醒锁。请解释