使用WMI来监控电源选项,需要使用Power Policy Classes,这些类在namespace root/cimv2/power 中ClassDescriptionWin32_PowerSettingRepresents a power setting on the system.Win32_PowerSettingSubgroupRepresents a power setting subgroup on the system.Win32_PowerPlanRepresents a power plan on a system.Win32_PowerSettingDefinitionRepresents a power setting definition.Win32_PowerSettingDefinitionPossibleValueRepresents possible values for a power setting.Win32_PowerSettingDefinitionRangeDataRepresents the possible range data values for a power setting.Win32_PowerSettingDataIndexRepresents the setting data index value for a power setting on the system.Win32_PowerSettingElementSettingDataIndexRepresents the association between the power setting and the corresponding setting data.Win32_PowerSettingCapabilitiesRepresents the association between the power setting and the power setting definition.Win32_PowerSettingDefineCapabilitiesAssociates the power setting's definition data with the power setting definition.Win32_PowerSettingInSubgroupRepresents the association between the power setting and a subgroup. Win32_PowerSettingDataIndexInPlanRepresents the association between the power plan and the power setting data index.这些类之间的关系如图:我在其中用到的类有3个:Win32_PowerPlan, Win32_PowerSetting, Win32_PowerSettingDataIndexWin32_PowerPlan代表一个特定的Power Scheme。Win32_PowerSetting代表某一个选项,某项PowerSetting的ID和显示名称存储在这里Win32_PowerSettingDataIndex代表一个选项的值,连接PowerPlan和PowerSetting,PowerSetting的值存储在这里。$powerplan=get-wmiobject –namespace “root/cimv2/power” –class Win32_PowerPlan这样就取得了所有Power Scheme的Array,可以通过下标来访问。下面就是对象的成员__GENUS : 2__CLASS : Win32_PowerPlan__SUPERCLASS : CIM_SettingData__DYNASTY : CIM_ManagedElement__RELPATH : Win32_PowerPlan.InstanceID="Microsoft:PowerPlan//{381b4222-f694-41f0-9685-ff5bb260df2e}"__PROPERTY_COUNT : 7__DERIVATION : {CIM_SettingData, CIM_ManagedElement}__SERVER : RICHARD-PC__NAMESPACE : root/cimv2/power__PATH : //RICHARD-PC/root/cimv2/power:Win32_PowerPlan.InstanceID="Microsoft:PowerPlan//{381b4222-f694-41f0-9685-ff5bb260df2e}"Caption : ChangeableType : ConfigurationName : Description : Automatically balances performance with energy consumption on capable hardware.ElementName : Recommended SettingsInstanceID : Microsoft:PowerPlan/{381b4222-f694-41f0-9685-ff5bb260df2e}IsActive : True$active=$powerplan|where {$_.isactive –eq $True}通过Where-Object cmdlet获得当前激活的Power Scheme。通过$active.ElementName可以获得它的显示名称,$active.InstanceID可以获得它的唯一ID。$powerplan[2].activate方法可以激活此Power Scheme。然后再来看看Win32_PowerSettingDataIndex__GENUS : 2__CLASS : Win32_PowerSettingDataIndex__SUPERCLASS : CIM_SettingData__DYNASTY : CIM_ManagedElement__RELPATH : Win32_PowerSettingDataIndex.InstanceID="Microsoft:PowerSettingDataIndex//{381b4222-f694-41f0-9685-ff5bb260df2e}//AC//{29f6c1db-86da-48c5-9fdb-f2b67b1f4 4da}"__PROPERTY_COUNT : 7__DERIVATION : {CIM_SettingData, CIM_ManagedElement}__SERVER : RICHARD-PC__NAMESPACE : root/cimv2/power__PATH : //RICHARD-PC/root/cimv2/power:Win32_PowerSettingDataIndex.InstanceID="Microsoft:PowerSettingDataIndex//{381b4222-f694-41f0-9685-ff5bb260df2e}//AC//{29f 6c1db-86da-48c5-9fdb-f2b67b1f44da}"Caption : ChangeableType : ConfigurationName : Description : ElementName : InstanceID : Microsoft:PowerSettingDataIndex/{381b4222-f694-41f0-9685-ff5bb260df2e}/AC/{29f6c1db-86da-48c5-9fdb-f2b67b1f44da}SettingIndexValue : 1200它的InstanceID中的一段381b4222-f694-41f0-9685-ff5bb260df2e 与上面的PowerPlan的InstanceID相同,表示它与此PowerPlan相关联。然后是Win32_PowerSetting,自然它与PowerSettingDataIndex的InstanceID中的一段29f6c1db-86da-48c5-9fdb-f2b67b1f44da相关联。以下代码就能列出此PowerPlan中所有的设置:[array]$Powerplan=get-wmiobject -namespace "root/cimv2/power" -class Win32_powerplan|Where-Object{$_.instanceid -eq 'Microsoft:PowerPlan/{381b4222-f694-41f0-9685-ff5bb260df2e}'}$powersettingindexes=get-wmiobject -namespace "root/cimv2/power" -class Win32_powersettingdataindex|where-object {$_.instanceid.contains($Powerplan[0].instanceid.split("/")[1])}foreach ($powersettingindex in $powersettingindexes){ $powersetting=get-wmiobject -namespace "root/cimv2/power" -class Win32_powersetting|where-object {$_.instanceid.contains($powersettingindex.instanceid.split("/")[3])} foreach ($powersetting in $powersettings) { "Power Setting: " + $powersetting.instanceid "AC/DC: " + $powersettingindex.instanceid.split("/")[2] "Description: " + $powersetting.description "Summary: " + $powersetting.ElementName "Value: " + $powersettingindex.settingindexvalue }}