Link to home
Start Free TrialLog in
Avatar of SerenitySystems
SerenitySystems

asked on

Need VBS script to fetch uptime, in days, of a remote Windows host

I need VBS script to fetch uptime, in days, of a remote Windows host, and output the number to standard output.  I need to be able to specify the remote hostname at the command line.  For example:

c:\> uptime.vbs server5
32

c:\>

I will be calling this .vbs from a DOS batchfile.  If the remote host isn't reachable for any reason, the app should return nothing and exit quietly.

There is a Windows reskit utility called uptime.exe, but it returns the days, hours, and minutes of uptime and if the remote  host isn't reachable, it blows up the output of up my batchfile and kills the formatting of the file I'm writing to.
Avatar of sirbounty
sirbounty
Flag of United States of America image

Not sure what format you're looking for, but this should do it...
If Wscript.Arguments.Count=0 Then 'perform locally only
  strServer="."
Else
  strServer=Wscript.Arguments(0)
End If
 
Dim objWMI:Set objWMI=GetObject("winmgmts:\\" & strServer & "\root\CIMV2")
Dim colItems:Set colItems=objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each objItem in colItems
  lbt=objItem.LastBootUpTime
  uptime=datediff("h",lbt,now)
  wscript.echo uptime & " hours."
Next
set objWMI=Nothing
wscript.quit
 
Function WMIDateStringToDate(dtmStart)
  WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & "/" & Mid(dtmStart, 7, 2) & "/" & Left(dtmStart, 4) _
    & " " & Mid (dtmStart, 9, 2) & ":" & Mid(dtmStart, 11, 2) & ":" & Mid(dtmStart, 13, 2))
End Function

Open in new window

yank off the & " hours." if you just want the number...
Hi, this script (an adaptation of the above) will get just the hours of uptime, after checking if the server is available first.  It will then return the hours to the calling batch file, under the %ERRORLEVEL% variable.  So, to call the VBS with batch, use:

@echo off
cscript /nologo "test1.vbs"
echo %errorlevel%
pause

Regards,

Rob.
If Wscript.Arguments.Count=0 Then 'perform locally only
  strServer="localhost"
Else
  strServer=Wscript.Arguments(0)
End If
 
Dim objWMI 
Dim colItems
 
uptime = 0
 
If Ping(strServer) = True Then
	On Error Resume Next
	If strServer = "localhost" Then strServer = "."
	Set objWMI=GetObject("winmgmts:\\" & strServer & "\root\CIMV2")
	Set colItems=objWMI.ExecQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem")
	For Each objItem in colItems
	  strLastBootTime=Left(objItem.LastBootUpTime, InStr(objItem.LastBootUpTime, "+") -1)
	  strLastBootTime = WMIDateStringToDate(strLastBootTime)
	  uptime=datediff("h",strLastBootTime,Now)
	  'wscript.echo uptime
	Next
	set objWMI=Nothing
	If Err.Number <> 0 Then
		uptime = 0
	End If
End If
wscript.quit(uptime)
 
Function WMIDateStringToDate(dtmStart)
  WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & "/" & Mid(dtmStart, 7, 2) & "/" & Left(dtmStart, 4) _
    & " " & Mid (dtmStart, 9, 2) & ":" & Mid(dtmStart, 11, 2) & ":" & Mid(dtmStart, 13, 2))
End Function
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 4 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

Avatar of SerenitySystems
SerenitySystems

ASKER

From SirBounty's script, I get this error:

C:\getuptime.vbs(11, 3) Microsoft VBScript runtime error: Type mismatch: 'lbt'

If I run it without 'script', I get the same error, but the error is displated in a Windows error message window.
In RobSampson's code, it's not clear where to specifiy the hostname of the remote host. I tried including it after the vbs file name and after the batchfile name. No luck.  The batchfile returns a 0 (zero) for the errorlevel.  I tried setting strServer to the name of a known host but didn't get anything. These are pingable hosts, so the code should be passing the ping subroutine OK.
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both solutions are great. This totally fixes my problem. Thanks for your help!
Oh, I forgot the closing bracket on
strServer=Wscript.Arguments.Item(0

but you may have fixed it.

Thanks for the grade.

Rob.
ack!  I was premature. sirbounty's script is returning the uptine in hours. I need days.
Ha, oh you're right, we missed that bit!  In either one of our scripts, in the DateDiff function on this line:
datediff("h",strLastBootTime,Now)

change the "h" to "d"....that's it.

Regards,

Rob.