Submitted By: Ismael Serrano

Lists computer uptime (in hours).

Visual Basic
Edit|Remove
Option Explicit
On Error Resume Next

Dim strComputer,objOU,objComputerItem

Set objOU = GetObject("LDAP://OU=Computers,OU=MyOU,DC=myDomain, DC=com")
objOU.Filter = Array("Computer")

For Each objComputerItem In objOU
	strComputer = objComputerItem.CN
	Call SubSendPing(strComputer)
Next


'-----------------------SUB & FUNCTIONS------------------------------------

Sub SubSendPing(strComputer)
	Dim objShell,strCommand,objExecObject,strText
	Set objShell = CreateObject("WScript.Shell")
    strCommand = "%comspec% /c ping -n 3 -w 1000 " & strComputer & ""
    Set objExecObject = objShell.Exec(strCommand)

    Do Until objExecObject.StdOut.AtEndOfStream
        strText = objExecObject.StdOut.ReadAll()
        If Instr(strText, "Reply") > 0 Then
'**********Write your code here to do if the computer replies to ping************        	
           	Call SubLastRebooted(strComputer)
'********************************************************************************
		Else
		    WScript.Echo strComputer & " Could not be reached."
		End If
    Loop
End Sub

Sub SubLastRebooted(strComputer)
    Dim objWMIService,colOperatingSystems,objItem,dtmBootUp,dtmLastBootupTime,dtmSystemUptime
    Set objWMIService = GetObject("WinMgmts:{impersonationLevel=Impersonate}!\\" & _
        strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")

	For Each objItem in colOperatingSystems
	   	dtmBootUp = objItem.LastBootUpTime
	   	dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
	   	dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
		WScript.Echo strComputer & " last restarted " & dtmSystemUptime & " hours"
	Next
End Sub

Function WMIDateStringToDate(dtmBootup)
    WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
         & " " & Mid (dtmBootup, 9, 2) & ":" & _
         Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
         13, 2))
End Function

'----------------------------END----------------------------------------