PowerShell / WMI电源管理

时间:2016-02-10 21:04:05

标签: powershell wmi power-management wmi-query

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

使用PowerShell和WMI我想将“高性能”电源计划的“盖子关闭操作”,“电源按钮操作”和“睡眠按钮操作”设置为“不执行任何操作”选项,最后将活动电源计划设置为“高性能”电力计划。我有一个有效的解决方案,但我怀疑它是否是一个合适的解决方案。

我的问题的关键在于所有计划,子组,操作,可用值等都使用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

2 个答案:

答案 0 :(得分:1)

您可以使用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属性显示了有人在控制面板中进入Power Settings GUI时会看到的内容。 InstanceID是您可以找到GUID的地方,但您可能需要执行以下操作:

Get-CimInstance -Namespace "root\cimv2\power" -class Win32_PowerPlan|Select ElementName,@{l='GUID';e={$_.instanceid.substring(20)}}

其他内容应该在Win32_PowerSettingWin32_PowerSettingSubgroup中找到。您应该能够浏览该网站以查找其他相关课程,以获得您需要的信息。

答案 1 :(得分:0)

MSDN Win32_PowerPlan class page上有一些示例PowerShell可以执行您想要的操作,稍作修改(将"PowerSetting"添加到最后一行)。修改如下:

$powerplan=get-wmiobject -namespace "root\cimv2\power" -class Win32_powerplan | where {$_.IsActive}

$powerSettings = $powerplan.GetRelated("win32_powersettingdataindex") | foreach {
 $powersettingindex = $_;

 $powersettingindex.GetRelated("Win32_powersetting") | select @{Label="Power Setting";Expression={$_.instanceid}},
 @{Label="AC/DC";Expression={$powersettingindex.instanceid.split("\")[2]}},
 @{Label="Summary";Expression={$_.ElementName}},
 @{Label="Description";Expression={$_.description}},
 @{Label="Value";Expression={$powersettingindex.settingindexvalue}}
 }

$powerSettings | ft "Power Setting","AC/DC",Summary,Value -autosize

输出:

Power Setting                                                 AC/DC Summary                                                                                                                               Value
-------------                                                 ----- -------                                                                                                                               -----
Microsoft:PowerSetting\{f3c5027d-cd16-4930-aa6b-90db844a8f00} AC    Reserve battery level                                                                                                                     7
Microsoft:PowerSetting\{f3c5027d-cd16-4930-aa6b-90db844a8f00} DC    Reserve battery level                                                                                                                     7
Microsoft:PowerSetting\{6c2993b0-8f48-481f-bcc6-00dd2742aa06} AC    Processor idle threshold scaling                                                                                                          0
Microsoft:PowerSetting\{6c2993b0-8f48-481f-bcc6-00dd2742aa06} DC    Processor idle threshold scaling                                                                                                          0
Microsoft:PowerSetting\{d8742dcb-3e6a-4b3c-b3fe-374623cdcf06} AC    Low battery action                                                                                                                        0

...

您也可以直接调用Win32_powersettingdataindex class,但我发现将设置与其GUID相关联更加困难。