Skip to main content


ServerCare home > Posts > UPSWatcher  



November 18

A few weeks ago I woke up with the alarming feeling that something was amiss, which of course is basically my default mode of waking up, as I tend to miss the alarm every now and then. (I get up at 5:30 alright!)

I looked over to the clock, which typically illuminates how many minutes I overslept in comfortably red glowing numbers. No lights. .. No comfortable red glow…

Over the last decade or so the power service to my house had been very dependable: it simply always worked. However, for some reason we have experience four power outages in the last six months, killing that informal service level agreement I had come to trust upon.

Unfortunately my servers had gone down hard, which resulted in RAID configurations needing to rebuild. No harm done, but still annoying. I decided to get a UPS so that in the future my servers would be allowed the time to gracefully shut down, so that I could prevent any issues that might result from the next power failure. My main concern was my Hyper-V cluster with two nodes and the Storage server that takes care of the iSCSI drives for the Hyper-V cluster. In addition I want my Web & Exchange server to shutdown nicely before the UPS kicks the bucket.

I had read Ben's blog where he goes into how he's bought a APC UPS and how he's using this with his Hyper-V setup. Following his lead I acquired the APC 1500 LCD. I like the fact that this thing actually gives me some feedback when things are going on, hence the LCD. (some more comfortable red glowing numbers… I sense a trend!)

Let's face it, when you're experiencing a power outage, the last thing you want to be bothered with is remembering what a series of beeps in a particular order means. Sure, you have read the manual many moons ago, but who touches the UPS once it's up & running? Not me! So typically by the time you need to know what's going on; you want the 'business critical' info presented to you in the most convenient manner. Although the LCD is a lot better than some beeps, I feel there's still room for improvement, it is all a bit cryptic.

Then it's time to install the APC software, after reviewing that feature- filled piece of software (not) for a full five minutes and going to the website to find the real software (don't bother) I found APC was not going to give me the possibility to control a number of servers. It simply supports one system, even though the back of the UPS will gladly allow me to connect six devices. If you actually want to shut them all down (why would you want to, right?) you're in bad luck.

To shutdown before the UPS gives up and simply fails your servers somewhat later than the original power failure would have done, you need to buy the more expensive business edition of the software… I felt cheated!

To the rescue!

One thing I took away from Ben's blog was that the UPS is presented as a laptop battery to the OS in Windows 2008. I figured that would mean I can check it using WMI…. Not sure how many UPS brands are supported this way, but I knew APC was pretty well supported by Microsoft. I was right: the Win32_Battery class gives me all I need!

I quickly removed the APC software from my server and decided to go my own way.


The result is a simple VBScript that basically does the following:

1. Tells you information about the UPS (or laptop battery if you like ;-)

2. Watches the remaining power left –and- power status of the UPS. You can configure a 'trigger' that indicates at what power level (say; 50% for example) you want to script to take action. A small configuration file allows you to name some servers and how many seconds you want to wait before shutting them down.


note: as mentioned in the comments below: please use the script by running it from the command prompt preceded with 'cscript'. So the complete line could be something like:

c:>cscript upswatcher /m:myserver /w

Actually, the script works much like any standard Windows command, using switches to tell it what you want it to do. For example:

/L     will simply list UPS information

/M:    Allows you to specify what machine to target (this machine needs to have a battery/UPS attached/installed. If not provided; will assume local machine. (better use "." For local machine)

/W    Puts the scrip in "Watch mode" where it check the UPS power level and mode against a (in the script) predetermined value. Make sure you allow enough time for the UPS to shut the servers down! 50% may seem ample time, but if the load on your UPS is very high, 50% might only allow for 1 minute! (the LCD version of the ACP UPS will tell you this 'Time To Live') Note that the script will also evaluate the battery status; it will only reboot systems when the battery/UPS is being drained, not when it's charging (makes sense… right?)

/V    Verbose; will make the script pretty talkative about what it is doing and why it's doing it. I call it the female mode. (Don't tell my wife…!)

/E    Will send out an E-Mail for each server that is being shut down. You might want to consider scheduling your mail server as one of the last systems to go down. Mail server and E-Mail addresses are configured in the .vbs source.

To determine the machines to be shut down a small configuration file is used, it looks like this:

# Config file for UPSWatcher.vbs

# defines a machines name and the amount of seconds the device is allowed to survive on UPS power before shutdown.

# Paul Weterings, Nov 2009



# Put the servername you want to shutdown, and the time you want to wait before issuing the shutdown command

# make sure not to shutdown the local system ('.') before other systems have received a shutdown command (duh ;-)

# If you decide to send out mail, you migt also want to consider shutting down that system later...

# Finally, this scripts depends on being able to send WMI shutdown messages through the network: is you switch

# also on the UPS?


# Clusternodes first please

node2, 12

node1, 10

# The mail server, lets give it some time

voo-server2, 50

# the storage (I could have used the name, but since it is the system running the script I can use '.')

., 60


Please note that it's completely independent where this script is running, as long as it can connect to the Windows 2008 machine connected to the UPS using WMI.

In addition, you do not need to shutdown the local machine, since that is quite optional. Notice in the example config file the local server (in this case the server connected to the UPS) is going down last… think about it…It would be rather dumb to shutdown the server sending the shutdown commands first. In addition; I know this is silly to mention, but make sure your switch is also on the UPS!

It would be quite entertaining to redevelop this into a powershell script, or even a small C# utility, if time permits. Feel free to do an attempt; it is open source as far as I'm concerned. I would appreciate an update if you do though.

UPSWatcher 1.7 can be found here

Update jan 2011: A slightly improved PowerShell version of this script can be found here.


Re: UPSWatcher


Can you please help me with some details? I have the following setup:

1 pc with windows XP
1 APC Back-UPS ES700
1 pc with Hyper-V server 2008 (core)
1 switch for both machines

The UPS is connected to the XP machine via USB cable and is managed using PowerChute Personal Edition.
The second machine (hyper-v) is powered by the same UPS but with no means to gracefully shut it down. The switch is also powered by the UPS.

What scenario would you use for this setup? Thanks!
 on 11/20/2009 10:36


What you could try is:

Connect the UPS to the core 2008 server. I'm not 100% sure (so it would be great if you could confirm this) but the lack of a GUI should not be a problem for Windows 2008 to still automagically 'understand' the UPS, install the driver; and have it noticed in WMI.

Core understands VBScript, so you should be able to run the script like this:

cscript UPSWatcher /M:. /L

This doesn do much more that simply give you theinformation back about the UPS: -if- it sees it through WMI.

If this works.. you're in business. Simple remove the APC software from your XP system, and run the script on your Windows 2008 Core server. (make sure to change the config file to contain the XP machines name)

I'd really love to know if this works for you, so make sure to post back?

 on 11/20/2009 13:07

Re: UPSWatcher

I tried to run the script on the xp machine in order to remotly shutdown the hyper-v server when my "battery" level is lower then 90% (for tests) and I edited the "device" file to describe which machine should be turned off. The procces is there (when running with "w" parameter) but the script didn't work for some reason (maybe I should have unninstalled the powerchute application first).

I don't have phisical acces to the UPS room so I can't try your suggestion.

I'll be back with details!

Thanks alot!
 on 11/20/2009 16:42

dude, it won't work on xp

Why do you think he sais...
"One thing I took away from Ben's blog was that the UPS is presented as a laptop battery to the OS in Windows 2008. I figured that would mean I can check it using WMI…. Not sure how many UPS brands are supported this way, but I knew APC was pretty well supported by Microsoft. I was right: the Win32_Battery class gives me all I need! "
you need to connect it to win2008...
 on 11/22/2009 22:36

RE: dude, it won't work on xp

The UPS is seen as "laptop battery" in windows XP too. So, it should work.

Anyway, I found another way to solve my problem and I post my solution here, maybe this could be helpful for others:

- uninstalled "powerchute"
- on XP machine I installed "apcupsd" and configured it (using conf file) as NIS Server
- on hyper-v R2 I installed "apcupsd" and configured it to listen to the NIS Server (YES, is compatible). You can fully configure the client with different settings then the NIS server (if needed).

Tested by unplugging the UPS. Both machines shutdown gracefully at selected "run on battery" time. Note: the client machine (in my case the hyper-v server) should be configured to shutdown before NIS server. If not, it will shutdown anyway when NIS server is shutting down.

Hope that helps someone!
 on 11/24/2009 11:26


I would assume that you would run this script on using the Scheduler service every so often?
 on 2/5/2010 23:12

Questions about the script

I have been looking over the code and had some questions...  First, I noticed in the code at the line where you would call the shutdown function it is commented out. 

'dicMachines.Item(objMachine).blRebootSentOK = ShutDown(...

Without removing the comment it won't really shutdown the server, correct?  Not sure if you meant to leave that comment in (assuming you had it in place for testing)...

Secondly, the shutdown command specifies a force shutdown:

ShutDown = ObjOperatingSystem.Win32Shutdown(1+4,0)

My question is does that force shutdown actually allow time for the Hyper-V services to actually handle any running VM's?  I suspect the force causes the server to shutdown before the VM's have been placed into a saved state (which is kinda bad)...

Please let me know what you think...  chris (at) dscoduc (dot) com
 on 2/6/2010 20:00

re: Chris

Hi Chris, sorry for the late reply.

No; the script would be run at startup of the computer.

 on 2/16/2010 8:16


Yes, you need to 'decomment' it, I intentionally left it in as I didn't want to be the cause of someone accidently rebooting a server. I guess I could change it to echo something like: please remove the quote to actually shutdown server strServer...

On the Hyper-V services: good question I'd have to try that, I think you might be right on that one!

I was actually thinking of translating this VBScript to .net C# program, and make some more changes such as instead of simply waiting some configurabl seconds to reboot to a % of the load on the UPS... makes more sense. That might be the perfect opportunity to also include Hyper-V awareness...
 on 2/16/2010 8:27

in hindsight

Chris... you're right. commenting out the most functional line in the script is confusing. I've replaced the zipfile with a newer version which also fixes a few other small issues. (nothing special, just some style-errors).

 on 2/16/2010 8:57
1 - 10Next

Add Comment

Spam Filter *

Please enter 4982 in this field. This will help prevent SPAM.


Body *