The following code will contact a PC whose name you specify and attempt to determine how long (in hours) the PC has been up since its last reboot. It uses WMI to do this, so you’ll need to make sure WMI is enabled on the machine you’re contacting and that you are running this script with administrator permissions against the remote machine. To determine uptime for the machine the script is running on, supply "." as the PC name.
'
'
' Calculate a PC's uptime in hours, using WMI.
'
'
function getUptime(pcname)
strComputer = pcname
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
dtmBootup = objOS.LastBootUpTime
dtmLastBootupTime = CDate(Mid(dtmBootup, 5, 2) & "/" & _
Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
& " " & Mid (dtmBootup, 9, 2) & ":" & _
Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
13, 2))
dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
getUptime = dtmSystemUptime
Next
end function