Remote Server Uptime with VBScript

There are quite a few times when I’ll reboot a server pragmatically and want to know if the reboot was successful, or if the machine has finished rebooting before I continue to run commands against it.

I wasn’t sure of a good way to do that other than looking at the machine’s uptime and making sure that the time it has been up was a shorter amount of time then last time I checked. Seemed like a reasonable way to me, but there was really no built in way to check the server’s uptime in VBscript.

There is a command that you can run, called “uptime” that will reveal the servers uptime in a format that looks something like “x day(s) y hour(s) z minute(s) a second(s)”. This is a pretty handy tool and seems to work well when you are reading the results. However returning a value with all those extra words tacked on didn’t make it very friendly for parsing in a script.

I would have to include a part that would convert that string into a value in seconds. (I bet you didn’t know that there were exactly 86,400 seconds in a single day) I would just have to parse the string so that for every day, hour and minute it would convert those to the proper second value, and then add them all up so that you knew how long the box in question had been up.

So, in order to pull the value via the uptime utility, I have to run the command, save the results, read them and parse the string that was returned so that I can get the values that are stored there. Then once I have the values I can add them all up and return the time, in seconds, that the server has been up.

So, here it is, the function to return, in seconds the amount of time that a particular server has been up:

And, if for some reason you want to convert that time in seconds back to a more human readable string, I whipped up another function that would return the time as a string with the scale that it’s being displayed in. This function is called “ReadableTime”:


Leave a Comment