Ever had multiple servers connected to the same UPS (uninterpretable power supply) and want to have them all shut down gracefully after a power outage? It seems like it would be an easy thing to do, but it’s harder than you think. Most UPSs have either a serial port or a USB port to allow them to connect to a single server or workstation type computer for the purpose of monitoring the battery health and to alert you in the event of a power outage. They can not only alert you of the outage, but also after a specified time running on battery or after the batteries reach specific levels of charge.
These alerts can also be configured to perform an action like shutting down the computer or putting it to sleep or in hibernation mode. But, since the UPS can only connect to one server, what do you do if you have two or more servers plugged in to it? That’s where the instructions you are about to read come in. With a little bit of batch file Kung Fu and a small, free utility, it can be a very easy task.
First you’re going to need to download PsTools from SysInternals (now owned by Microsoft), extract the PsShutdown.exe executable and save it in a folder where you’re going to put everything else. I just used C:\shutdown for this. In case you haven’t figured it out yet, all of the magic will run from the server connected directly to the UPS.
You will also need a text file with the names of the servers you want to shutdown (not including the server plugged in to the UPS). Name it ServList.txt and save it in the same folder you created in the last step. It should have the names or IP addresses of each server, one on each line like this:
Next you will need the batch file which actually does the dirty work. Here are the contents so you can copy and paste into a file called shut.bat and, again, save it in the same folder as everything else.
REM variable "ServList" is the path to the list of servers SET ServList=c:\shutdown\ServList.txt REM variable "LogFile" is the path to the list of servers SET LogFile=c:\shutdown\LogFile.txt psshutdown.exe -t 10 -c @%ServList% -m "System Shutting down" 2>&1 | findstr . >>"%LogFile%"
Her is a quick overview of what the batch script is doing. The first and second commands set variables that contain the paths to the list of servers and the logfile where we will output the results. Just edit those lines as needed depending on where you stored the files. The last line actually runs the psshutdown application with the correct switches (options) needed. These options are:
Sets the countdown delay to 10 seconds.
Allows any logged in users to abort the shutdown process.
Tells it to use the list of servers we created earlier.
-m "System Shutting down"
Sends a message to the console in case someone is logged in.
2>&1 | findstr . >> "%LogFile%"
Pipes any output messages from psshutdown to the findstr command which then pipes them to the logfile for troubleshooting. If you want to get fancy, you could add additional timestamps or other information, but I find the built in windows system logs are detailed enough.
After you have all the files compiled, your shutdown folder should look like this (the logfile.txt file will be created the first time you run the script):
At this point, you can go ahead and test out the script to see if it works. Just remember that you will need to be logged in as a user with administrator rights on the remote machines and also don’t run it on any production servers unless it’s after hours. Also remember that if you are running it on remote servers, you will need someone there or some other mechanism to turn them back on!
Now to configure the automated part. From the control panel open the power options and go to the UPS tab. You should already have the UPS set up and see something like this:
Click on the Configure button to get to the UPS configuration screen. Here, you can set the amount of time the server will run on battery before triggering the critical alarm. This time you allow will depend on the type and number of servers plugged in and the size of the UPS. In my case I can run for almost an hour before the battery dies, but there is no reason to run the batteries dry and I figure if the power is out for more than 15 minutes, it’s probably going to be out for a while, so I may as well start shutting everything down.
The next field is where you tell it to run the shutdown batch file which is going to tell the other servers to shut down. After it’s done with that, the last thing it does is shut its self down.
When you configure the critical alarm to run a program it actually creates a scheduled task. This is finally where you point the task to the batch file and configure the credentials to run it as. In my case, the servers are all members of an active directory domain so I ran it as the domain administrator, which has administrator access on each system. You could also create a special user account for this task, or if the computers aren’t members of a domain, just create an account on each machine with the same user name and password and local administrator permissions.
And that’s all there is to it. Just save all the windows you have open and you’re ready to go.
Let me know if you found this tutorial helpful or if you have any other suggestions or methods that worked for you
Thanks for reading.