Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings:

I am new to VB script. I have learned a lot from this site as well as others. However I am having a problem with a concept for a script I would like to write and I need a push in the right direction.

First a little background. I have an old laptop with WinXP. I want to plug this laptop into an electrical outlet. When the power goes out I want my script to write to a file the time and date that the laptop ran off of the battery. When power is restored and the laptop is running off of ac current it will write in the same log file the time and date AC power was restored. I basically want to use this laptop to log power outages at my house while I'm away.

I can get the PowerLinestatus using VBScript but how do I get the program to monitor (like a TSR) the status and write the changes without having to throw the program in to a loop? This is where I am stuck at.. How to keep the program running to monitor the power and write only when there is a change is power source (battery or AC)? Any advice would be greatly appreciated.

Regards,
Keith
Posted

I would not look for WMI solution for this. There is quite a straightforward way of handling such things: use the .NET FCL class Microsoft.Win32.SystemEvents:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.aspx[^].

You will need to handle the static event Microsoft.Win32.SystemEvents.PowerModeChanged:
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.powermodechanged.aspx[^].

To poll the current power status at any time, you can use the class System.Windows.Forms.SystemInformation:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.aspx[^].

The code samples on the help pages on PowerStatus (property and class) demonstrate how to get this information:
http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.powerstatus.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.powerstatus.aspx[^].

I hope it will get you all you need.

—SA
 
Share this answer
 
Comments
Espen Harlinn 25-Apr-13 19:15pm    
5'ed!
Sergey Alexandrovich Kryukov 25-Apr-13 19:41pm    
Thank you, Espen.
—SA
If you really like to know more about the capabilities exposed by WMI on your computer, you could have a look at: CIMTool for Windows Management Instrumentation - Part 3[^] - it also helps with code generation.

Best regards
Espen Harlinn
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900