' Enable custom error handling On Error Resume Next ' Declare variables Dim numUptime, numUptMins, numUptHour, numUptDays Dim strYear, strMonth, strDay, strDate, strHour, strMins, strTime Dim strComputer, strMsg strComputer = Context.GetProperty("Address") Context.LogMessage "Attempting connection to " & strComputer Context.LogMessage "Connecting as User: " & Context.GetProperty("CredWindows:DomainAndUserid") & " Password: " & Context.GetProperty("CredWindows:Password") ' Connect to the specified computer Set objLocator = CreateObject( "WbemScripting.SWbemLocator" ) Set objWMIService = objLocator.ConnectServer ( Context.GetProperty("Address"), "root/cimv2", Context.GetProperty("CredWindows:DomainAndUserid") , Context.GetProperty("CredWindows:Password") ) 'Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" ) ' Get the "real" computer name Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem", , 48 ) For Each objItem in colItems strComputer = objItem.Name Next 'Confirm device identity Context.LogMessage "Device identifies itself as: " & strComputer ' Get the uptime by querying the boot time and the current time Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 ) For Each objItem in colItems numUptime = DateDiff( "n", _ ParseDat( objItem.LastBootUpTime ), _ ParseDat( objItem.LocalDateTime ) ) numUptMins = numUptime Mod 60 numUptHour = ( numUptime \ 60 ) Mod 24 numUptDays = ( numUptime \ 60 ) \ 24 ' Build message to be displayed ' Day(s) strMsg = vbCrLf & strComputer & " has been up for " & numUptDays & " day" ' Add "s" for multiple days If numUptDays <> 1 Then strMsg = strMsg & "s" ' Hour(s) strMsg = strMsg & ", " & numUptHour & " hour" ' Add "s" for multiple hours If numUptHour <> 1 Then strMsg = strMsg & "s" ' And minute(s) strMsg = strMsg & " and " & numUptMins & " minute" ' Add "s" for multiple minutes If numUptMins <> 1 Then strMsg = strMsg & "s" Next Context.LogMessage "Uptime returned: " & numUptime & " (minutes)" Context.LogMessage strMsg ' Confirm the results - look for uptime beyond 15 days (numUptime is in minutes) If numUptime > 21600 Then Context.SetResult 1," Up time excessive - restart required" Else Context.SetResult 0," Up time within tolerance" End If numUptime = "" strComputer = "" strMsg = "" Set colItems = Nothing Set objWMIService = Nothing Function ParseDat( ByVal strDate ) strYear = Left( strDate, 4 ) strMonth = Mid( strDate, 5, 2 ) strDay = Mid( strDate, 7, 2 ) strDat = strDay & "-" & strMonth & "-" & strYear strHour = Mid( strDate, 9, 2 ) - strTimeShift strMins = Mid( strDate, 11, 2 ) strTime = strHour & ":" & strMins ParseDat = strDat & " " & strTime End Function