簡體   English   中英

如何使用 powershell 將 Powercfg /availablesleepstates 輸出到對象中

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

命令行 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 腳本:

$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}

這是一個 PowerShell function 直接公開 SYSTEM_POWER_CAPABILITIES,正如@Persistent13 建議的那樣。 該結構已更新為包含新變量,例如 AoAc,以顯示是否支持現代待機 (S0)。 SYSTEM_POWER_CAPABILITIES 的更新定義比我想象的更難找到,因為Microsoft KB錯誤地顯示 SYSTEM_BATTERY_STATE。 我設法找到了一些替代資源,這些資源在 TypeDef 中有鏈接。

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()
}

用法:

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

我不確定 BatteryScale 是否准確,因為它為我返回全 0。 所有 PowerSystemUnspecified 值也有點令人擔憂。 但是 S0-5 睡眠狀態似乎都報告正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM