Поделиться через


Monitoring Changes in Computer Power Status

Microsoft® Windows® 2000 Scripting Guide

Changes in power status often indicate that a problem has occurred with a computer or with another managed device. If a server suddenly switches from AC power to an uninterruptible power supply, this change can indicate that an electrical problem of some kind has occurred, either with the computer itself or with the electrical system in the room in which the computer is kept.

Administrators need to monitor these changes in power status and be notified of such changes immediately. This enables them to take action before the device loses power entirely. (Uninterruptible power supply systems, for example, might run for only 15 minutes or so before shutting down.)

The Win32_PowerManagementEvent class can be used to monitor changes in power status on a computer. These changes can include a switch from one power source to another as well as a change in computer power state (for example, entering or exiting Suspend mode).

The Win32_PowerManagementEvent class has only two properties: EventType, used to indicate the type of power change event that occurred, and OEMEventType, which is used by some original equipment manufacturers to define additional power change events. The values for the EventType property are shown in Table 8.22.

Table 8.22 Win32_PowerManagementEvent Event Types

EventType Value

Description

4

Entering Suspend. Indicates that the computer is about to enter the suspended state.

While suspended, the computer appears to be off; however, it can be "awakened" in response to various events, including user input (such as moving the mouse or pressing a key on the keyboard). While the computer is suspended, power consumption is reduced to one of several levels depending on how the system is to be used. The lower the level of power consumption, the more time it takes the system to return to the working state.

When the computer enters the suspend state, the desktop is locked, and you must press CTRL+ALT+DELETE and provide a valid user name and password to resume operations.

7

Resume from Suspend. Indicates that a ResumeSuspend message has been sent, enabling the computer to return to its regular power state.

10

Power Status Change. Indicates a change in the power status of the computer, such as a switch from battery power to AC, or from AC to an uninterruptible power supply. The system also broadcasts this event when remaining battery power slips below the threshold specified by the user or if the battery power changes by a specified percentage.

11

OEM Event. Indicates that an Advanced Power Management (APM) BIOS has sent an OEM event.

The value of the event will be captured in the OEMEventCode property. Because some APM BIOS implementations do not provide OEM event notifications, this event might never be broadcast on some computers.

APM is the legacy power management scheme that was first widely supported in Windows 95. Although still supported in Windows 2000 and Windows XP, APM has been largely superseded by ACPI (Advanced Configuration and Power Interface).

18

Resume Automatic. Indicates that the computer has awakened in response to an event. If the system detects user activity (such as a mouse click), the ResumeSuspend message will be broadcast, letting applications know that they can resume full interactivity with the user.

Scripting Steps

Listing 8.26 contains a script that monitors changes in power status on a computer. To carry out this task, the script must perform the following steps:

  1. Use a GetObject call and an ExecNotification query to monitor for changes in power status.

    This query looks for new instances of the Win32_PowerManagementEvent class.

  2. When a power change event occurs, echo the event type.

Listing 8.26 Monitoring Changes in Power Status

  
1
2
3
4
5
6
Set colMonitoredEvents = GetObject("winmgmts:")._
 ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
 Set strLatestEvent = colMonitoredEvents.NextEvent
 Wscript.Echo strLatestEvent.EventType
Loop