我是Windows电源管理的新手,甚至是WMI的新手,但始终欢迎您学习。

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

我问题的症结在于,所有计划,子组,操作,可用值等均由GUID标识,并且我读到这些GUID可能因系统而异(尤其是通过组策略应用时) 。)在我的解决方案中,我设法避免仅对GUID进行硬编码,而只能以硬编码值结尾,例如“电源和盖子按钮”,“合上盖 Action ”,“什么都不做”等,这些值可能会有所不同Windows的非英语版本。 (顺便说一下,我正在使用Windows 8.1。)

我的问题

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

引用

在使用powercfg.exe工具的批处理中,命令为:

:: 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_PowerSettingWin32_PowerSettingSubgroup中找到。您应该能够浏览该站点以找到其他相关的类(class),以获取所需的信息。

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

10-10 16:47