WMI之电源选项

2019-07-13 21:53发布

使用WMI来监控电源选项,需要使用Power Policy Classes,这些类在namespace root/cimv2/power  Class Description Win32_PowerSetting Represents a power setting on the system. Win32_PowerSettingSubgroup Represents a power setting subgroup on the system. Win32_PowerPlan Represents a power plan on a system. Win32_PowerSettingDefinition Represents a power setting definition. Win32_PowerSettingDefinitionPossibleValue Represents possible values for a power setting. Win32_PowerSettingDefinitionRangeData Represents the possible range data values for a power setting. Win32_PowerSettingDataIndex Represents the setting data index value for a power setting on the system. Win32_PowerSettingElementSettingDataIndex Represents the association between the power setting and the corresponding setting data. Win32_PowerSettingCapabilities Represents the association between the power setting and the power setting definition. Win32_PowerSettingDefineCapabilities Associates the power setting's definition data with the power setting definition. Win32_PowerSettingInSubgroup Represents the association between the power setting and a subgroup.  Win32_PowerSettingDataIndexInPlan Represents the association between the power plan and the power setting data index. 这些类之间的关系如图: 我在其中用到的类有3个:Win32_PowerPlan, Win32_PowerSetting, Win32_PowerSettingDataIndex Win32_PowerPlan代表一个特定的Power Scheme Win32_PowerSetting代表某一个选项,某项PowerSettingID和显示名称存储在这里 Win32_PowerSettingDataIndex代表一个选项的值,连接PowerPlanPowerSettingPowerSetting的值存储在这里。 $powerplan=get-wmiobject –namespace “root/cimv2/power” –class Win32_PowerPlan 这样就取得了所有Power SchemeArray,可以通过下标来访问。下面就是对象的成员 __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 Settings InstanceID        : 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 与上面的PowerPlanInstanceID相同,表示它与此PowerPlan相关联。 然后是Win32_PowerSetting,自然它与PowerSettingDataIndexInstanceID中的一段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     } }