简体   繁体   English

如何使用 powershell 将 Powercfg /availablesleepstates 输出到对象中

[英]How to ouput Powercfg /availablesleepstates into objects using powershell

Command line output:命令行 output:

C:\powercfg -availablesleepstates
The following sleep states are available on this system:
    Standby (S3)
    Hibernate
    Hybrid Sleep
    Fast Startup

The following sleep states are not available on this system:
    Standby (S1)
        The system firmware does not support this standby state.

    Standby (S2)
        The system firmware does not support this standby state.

    Standby (S0 Low Power Idle)
        The system firmware does not support this standby state.

powershell script: powershell 脚本:

$info = (powercfg /a | Select-String -Pattern "sleep states are available" -context 4) | select -Last 4
$items = $info -split ","
$ourObject = New-Object -TypeName psobject

$ourObject | Add-Member -MemberType NoteProperty -Name PCFG0bj -Value $items -Force
$ourObject | fl

Output:
PCFG0bj : {> The following sleep states are available on this system:
                Standby (S3)
                Hibernate
                Hybrid Sleep
                Fast Startup}

Here is a PowerShell function that exposes SYSTEM_POWER_CAPABILITIES directly, as @Persistent13 suggested.这是一个 PowerShell function 直接公开 SYSTEM_POWER_CAPABILITIES,正如@Persistent13 建议的那样。 The struct has been updated to include new variables, such as AoAc to show if Modern Standby (S0) is supported.该结构已更新为包含新变量,例如 AoAc,以显示是否支持现代待机 (S0)。 An updated definition for SYSTEM_POWER_CAPABILITIES was more difficult to find than I thought, as the Microsoft KB incorrectly displays SYSTEM_BATTERY_STATE. SYSTEM_POWER_CAPABILITIES 的更新定义比我想象的更难找到,因为Microsoft KB错误地显示 SYSTEM_BATTERY_STATE。 I managed to find some alternative resources, which are linked in the TypeDef.我设法找到了一些替代资源,这些资源在 TypeDef 中有链接。

Function: Function:

Function Get-PowerCapabilities
{
    Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

public static class PowerCfg
{
    [DllImport("PowrProf.dll")]
    public static extern bool GetPwrCapabilities(out SYSTEM_POWER_CAPABILITIES lpSystemPowerCapabilities);

    public static SYSTEM_POWER_CAPABILITIES GetCapabilities()
    {
        SYSTEM_POWER_CAPABILITIES spc;
        bool well = GetPwrCapabilities(out spc);
        return spc;
    }

    // https://github.com/MicrosoftDocs/sdk-api/blob/docs/sdk-api-src/content/winnt/ns-winnt-system_power_capabilities.md
    // https://www.pinvoke.net/default.aspx/Structures/SYSTEM_POWER_CAPABILITIES.html
    [Serializable]
    public struct SYSTEM_POWER_CAPABILITIES
    {
        [MarshalAs(UnmanagedType.I1)]
        public bool PowerButtonPresent;   //If this member is TRUE, there is a system power button.
        [MarshalAs(UnmanagedType.I1)]
        public bool SleepButtonPresent;   //If this member is TRUE, there is a system sleep button.
        [MarshalAs(UnmanagedType.I1)]
        public bool LidPresent;           //If this member is TRUE, there is a lid switch.
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemS1;             //If this member is TRUE, the operating system supports sleep state S1.
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemS2;             //If this member is TRUE, the operating system supports sleep state S2.
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemS3;             //If this member is TRUE, the operating system supports sleep state S3.
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemS4;             //If this member is TRUE, the operating system supports sleep state S4 (hibernation).
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemS5;             //If this member is TRUE, the operating system supports power off state S5 (soft off).
        [MarshalAs(UnmanagedType.I1)]
        public bool HiberFilePresent;     //If this member is TRUE, the system hibernation file is present.
        [MarshalAs(UnmanagedType.I1)]
        public bool FullWake;             //If this member is TRUE, the system supports wake capabilities.
        [MarshalAs(UnmanagedType.I1)]
        public bool VideoDimPresent;      //If this member is TRUE, the system supports video display dimming capabilities.
        [MarshalAs(UnmanagedType.I1)]
        public bool ApmPresent;           //If this member is TRUE, the system supports APM BIOS power management features.
        [MarshalAs(UnmanagedType.I1)]
        public bool UpsPresent;           //If this member is TRUE, there is an uninterruptible power supply (UPS).
        [MarshalAs(UnmanagedType.I1)]
        public bool ThermalControl;       //If this member is TRUE, the system supports thermal zones.
        [MarshalAs(UnmanagedType.I1)]
        public bool ProcessorThrottle;    //If this member is TRUE, the system supports processor throttling.
        public byte ProcessorMinThrottle; //The minimum level of system processor throttling supported, expressed as a percentage.
        public byte ProcessorMaxThrottle; //The maximum level of system processor throttling supported, expressed as a percentage. Also known as ProcessorThrottleScale before Windows XP
        [MarshalAs(UnmanagedType.I1)]
        public bool FastSystemS4;         //If this member is TRUE, the system supports the hybrid sleep state.
        [MarshalAs(UnmanagedType.I1)]
        public bool Hiberboot;            //If this member is set to TRUE, the system is currently capable of performing a fast startup transition. This setting is based on whether the machine is capable of hibernate, whether the machine currently has hibernate enabled (hiberfile exists), and the local and group policy settings for using hibernate (including the Hibernate option in the Power control panel).
        [MarshalAs(UnmanagedType.I1)]
        public bool WakeAlarmPresent;     //If this member is TRUE, the platform has support for ACPI wake alarm devices.
        [MarshalAs(UnmanagedType.I1)]
        public bool AoAc;                 //If this member is TRUE, the system supports the S0 low power idle model.
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] spare2;             //Unknown
        [MarshalAs(UnmanagedType.I1)]
        public bool DiskSpinDown;         //If this member is TRUE, the system supports allowing the removal of power to fixed disk devices.
        public byte HiberFileType;        //Unknown
        [MarshalAs(UnmanagedType.I1)]
        public bool AoAcConnectivitySupported;           //Unknown
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] spare3;                            //Reserved
        [MarshalAs(UnmanagedType.I1)]
        public bool SystemBatteriesPresent;              //If this member is TRUE, there are one or more batteries in the system.
        [MarshalAs(UnmanagedType.I1)]
        public bool BatteriesAreShortTerm;               //If this member is TRUE, the system batteries are short-term. Short-term batteries are used in uninterruptible power supplies (UPS).
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public BATTERY_REPORTING_SCALE[] BatteryScale;   //A BATTERY_REPORTING_SCALE structure that contains information about how system battery metrics are reported.
        public SYSTEM_POWER_STATE AcOnLineWake;          //The lowest system sleep state (Sx) that will generate a wake event when the system is on AC power.
        public SYSTEM_POWER_STATE SoftLidWake;           //The lowest system sleep state (Sx) that will generate a wake event via the lid switch.
        public SYSTEM_POWER_STATE RtcWake;               //The lowest system sleep state (Sx) supported by hardware that will generate a wake event via the Real Time Clock (RTC).
        public SYSTEM_POWER_STATE MinDeviceWakeState;    //The minimum allowable system power state supporting wake events. Note that this state may change as different device drivers are installed on the system.
        public SYSTEM_POWER_STATE DefaultLowLatencyWake; //The default system power state used if an application calls RequestWakeupLatency with LT_LOWEST_LATENCY.
    }

    // https://github.com/MicrosoftDocs/sdk-api/blob/docs/sdk-api-src/content/winnt/ns-winnt-battery_reporting_scale.md
    public struct BATTERY_REPORTING_SCALE
    {
        public UInt32 Granularity;
        public UInt32 Capacity;
    }

    // https://learn.microsoft.com/en-us/windows/win32/api/winnt/ne-winnt-system_power_state
    public enum SYSTEM_POWER_STATE
    {
        PowerSystemUnspecified = 0, //Unspecified system power state.
        PowerSystemWorking = 1,     //Specifies system power state S0.
        PowerSystemSleeping1 = 2,   //Specifies system power state S1.
        PowerSystemSleeping2 = 3,   //Specifies system power state S2.
        PowerSystemSleeping3 = 4,   //Specifies system power state S3.
        PowerSystemHibernate = 5,   //Specifies system power state S4 (HIBERNATE).
        PowerSystemShutdown = 6,    //Specifies system power state S5 (OFF).
        PowerSystemMaximum = 7      //Specifies the maximum enumeration value.
    }
}
"@
    [PowerCfg]::GetCapabilities()
}

Usage:用法:

PS C:\WINDOWS\system32> Get-PowerCapabilities

PowerButtonPresent        : True
SleepButtonPresent        : True
LidPresent                : True
SystemS1                  : False
SystemS2                  : False
SystemS3                  : True
SystemS4                  : True
SystemS5                  : True
HiberFilePresent          : True
FullWake                  : True
VideoDimPresent           : True
ApmPresent                : False
UpsPresent                : False
ThermalControl            : True
ProcessorThrottle         : False
ProcessorMinThrottle      : 0
ProcessorMaxThrottle      : 0
FastSystemS4              : True
Hiberboot                 : True
WakeAlarmPresent          : False
AoAc                      : False
spare2                    : {0, 2, 0}
DiskSpinDown              : False
HiberFileType             : 0
AoAcConnectivitySupported : False
spare3                    : {0, 0, 0, 1...}
SystemBatteriesPresent    : False
BatteriesAreShortTerm     : False
BatteryScale              : {PowerCfg7+BATTERY_REPORTING_SCALE, PowerCfg7+BATTERY_REPORTING_SCALE,
                            PowerCfg7+BATTERY_REPORTING_SCALE}
AcOnLineWake              : PowerSystemHibernate
SoftLidWake               : PowerSystemUnspecified
RtcWake                   : PowerSystemUnspecified
MinDeviceWakeState        : PowerSystemUnspecified
DefaultLowLatencyWake     : PowerSystemUnspecified

I'm not sure BatteryScale is accurate, as it returns all 0s for me.我不确定 BatteryScale 是否准确,因为它为我返回全 0。 All the PowerSystemUnspecified values are a bit concerning too.所有 PowerSystemUnspecified 值也有点令人担忧。 But the S0-5 sleep states all seem to report correctly.但是 S0-5 睡眠状态似乎都报告正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM