Thursday, February 12, 2009

Checking for the Uptime of a Remote Computer

I've created this script to query for the uptime of a remote computer. It takes, for an argument, the hostname or IP address of the remote machine. Here's the main body of the script:



The main body checks for 1 argument passed when the script is ran; if the number of arguments is not equal to 1, then the script exits. It then calls for the GetUptime procedure:



The GetUptime procedure queries WMI for the Win32_PerfRawData_PerfOS_System class, in particular the Timestamp_Object, Frequency_Object, and the SystemUpTime properties. To calculate for the system uptime in seconds (in my example, I used the iUptimeInSec variable), the following formula was used:

iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq

This value is then passed to the ConvertTime(seconds) function.



The iUptimeInSec variable was converted to an easier-t0-read format, eg. days, hours, minutes and seconds.

No comments: