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:
Function Uptime(sServer) 'This function will return the uptime of sServer in seconds. This 'function requires the "uptime.exe" utility. ' 'This script is provided under the Creative Commons license located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for commercial purposes with out the expressed written consent 'of NateRice.com Const OpenAsDefault = -2 Const FailIfNotExist = 0 Const ForReading = 1 Set oShell = CreateObject("WScript.Shell") Set oFSO = CreateObject("Scripting.FileSystemObject") sTempFile = oShell.ExpandEnvironmentStrings("%TEMP%") & "\runresult.tmp" oShell.Run "%comspec% /c uptime " & sServer & " > " & sTempFile, 0, True Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, _ OpenAsDefault) On Error Resume next sResults = fFile.ReadAll If Err.Number <> 0 Then GetUptime = -1 On Error Goto 0 Exit Function End If fFile.Close oFSO.DeleteFile (sTempFile) If InStr(sResults,"unable to connect") > 0 Then GetUptime = 0 Exit Function End If aSplitAtColon = Split(sResults, ":") For Each vSplitAtColon In aSplitAtColon If InStr(vSplitAtColon, "second(s)") Then aSplitAtComma = Split(vSplitAtColon, ",") Exit For End If Next vTotalTime = 0 For Each vTimeValue In aSplitAtComma If InStr(vTimeValue, "day(s)") Then vTimeValue = Trim(Replace(vTimeValue, "day(s)", "")) vTimeValue = 86400 * vTimeValue End If If InStr(vTimeValue, "hour(s)") Then vTimeValue = Trim(Replace(vTimeValue, "hour(s)", "")) vTimeValue = 3600 * vTimeValue End If If InStr(vTimeValue, "minute(s)") Then vTimeValue = Trim(Replace(vTimeValue, "minute(s)", "")) vTimeValue = 60 * vTimeValue End If If InStr(vTimeValue, "second(s)") Then vTimeValue = Trim(Replace(vTimeValue, "second(s)", "")) End If vTotalTime = vTotalTime + vTimeValue Next Uptime = vTotalTime Set oShell = Nothing Set oFSO = Nothing End Function
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”:
Function ReadableTime(vSeconds) 'This function creates readable time in Years:Days:Hours:Minutes:Seconds 'From the a value (vSeconds) passed in seconds. ' 'This script is provided under the Creative Commons liscense located 'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 'be used for comercial purposes with out the expressed written consent 'of NateRice.com If Not IsNumeric(vSeconds) Then ReadableTime = -1 Exit Function End If vScale = "seconds" If vSeconds >= 60 Then vSeconds1 = vSeconds Mod 60 aMinutes = Split((vSeconds / 60), ".") vMinutes = aMinutes(0) vSeconds = "." & vSeconds1 vScale = "mm.ss" End If If vMinutes >= 60 Then vMinutes1 = vMinutes Mod 60 aHours = Split((vMinutes / 60), ".") vHours = aHours(0) vMinutes = ":" & vMinutes1 vScale = "hh:mm.ss" End If If vHours >= 24 Then vHours1 = vHours Mod 24 aDays = Split((vHours / 24), ".") vDays = aDays(0) vHours = ":" & vHours1 vScale = "dd:hh:mm.ss" End If If vDays >= 365 Then vDays1 = vDays Mod 365 aYears = Split((vDays / 365), ".") vYears = aYears(0) vDays = ":" & vDays1 vScale = "yyyy:dd:hh:mm.ss" End If ReadableTime = vYears & vDays & vHours & vMinutes & vSeconds & " (" & vScale & ")" End Function