How can i qury the machines uptime in a ie window.

Hi,

I want a script to get me the uptime from all machines in the domain in a ie,hta or excel window.Is there a way to do this..

Regards
Sharath
LVL 11
bsharathAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
Sharath, in Excel, add a reference to Microsoft Scripting Runtime, and run this macro.

You can have an empty workbook, it will get all computers on your network:

Sub Get_Up_Time()
    Set objNetwork = CreateObject("WScript.Network")
    Set objComputers = GetObject("WinNT://" & objNetwork.UserDomain)
    objComputers.Filter = Array("Computer")
    Cells(1, "A").Value = "Computer"
    Cells(1, "B").Value = "Up Time"
    intRow = 2
    For Each strComputer In objComputers
        Cells(intRow, "A").Value = strComputer.Name
        If Ping(strComputer) = True Then
            On Error Resume Next
            Set objWMIService = GetObject _
                ("winmgmts:\\" & strComputer.Name & "\root\cimv2")
            If Err.Number = 0 Then
                On Error GoTo 0
                Set colOperatingSystems = objWMIService.ExecQuery _
                    ("Select * from Win32_OperatingSystem")
                For Each objOS In colOperatingSystems
                    dtmBootup = objOS.LastBootUpTime
                    dtmLastBootUpTime = WMIDateStringToDate(dtmBootup)
                    dtmSystemUptime = DateDiff("d", dtmLastBootUpTime, Now)
                    Cells(intRow, "B").Value = dtmSystemUptime
                Next
            Else
                Err.Clear
                On Error GoTo 0
                Cells(intRow, "B").Value = "OFF"
            End If
        Else
            Cells(intRow, "B").Value = "OFF"
        End If
        intRow = intRow + 1
        If intRow = 20 Then Exit For
    Next
    MsgBox "Done"
End Sub

Function Ping(strComputer)
    Dim objShell, boolCode
    Set objShell = CreateObject("WScript.Shell")
    boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer.Name, 0, True)
    If boolCode = 0 Then
        Ping = True
    Else
        Ping = False
    End If
End Function

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




Regards,

Rob.
0
 
sr75Commented:
You can use the LastBootUpTime property of Win32_OperatingSystem using WMI.  Querying that will get you the uptime information.  I have attached a working sample of the vbscripting code.  I am also sure that you can do an ldap query of the Computers OU to query each machine.  Or you can use some code that loops through your IP network.  

For i=0 to 256
strComputer = 192.168.1.i
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOS in colOperatingSystems
    dtmBootup = objOS.LastBootUpTime
    dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
    dtmSystemUptime = DateDiff("h", dtmLastBootUpTime, Now)
    Wscript.Echo dtmSystemUptime
Next
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

Open in new window

0
 
bsharathAuthor Commented:
I just get this box..

---------------------------
Windows Script Host
---------------------------
189
---------------------------
OK  
---------------------------
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
sr75Commented:
It was a working sample to show you some code that you could use.  It wasn't the actual script to use.  It pops up the number of hours the system has been up.  You could change that to minutes, seconds, days, weeks, months, and even years.
0
 
bsharathAuthor Commented:
Ok How can i change this to get the details of all machines to a file and to change it to no of days wise...
Sorry i have very limited knowledge of scripting...
0
 
sr75Commented:
The easiest is to do a For loop that counts up or down 0-255.  I don't know your network so I cannot really help you with that.  As for changing to days you just need to change line 9.  It currently has an "h" and to switch to days you need to change it to "d".  

Here is a list of the values it takes in that position:

yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
0
 
bsharathAuthor Commented:
Rob this works fine but i get just 19 machines when run
Can we even mention the machine names in colum A?
0
 
RobSampsonCommented:
Oh whoops! Yeah, I left in an Exit For for testing....remove this line
If intRow = 20 Then Exit For

and it will get all of them.....

Rob.
0
 
bsharathAuthor Commented:
Ok...
After 38 machine i get a run time error when debug

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))
0
 
RobSampsonCommented:
Hmmm, not sure why....that may be a problem with the date on a few machines....we'll check that later....for now, change this:

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

to this:

Function WMIDateStringToDate(dtmBootup)
   If Len(dtmBootUp) > 13 Then
      strTheDate = Mid(dtmBootup, 5, 2) & "/" & _
            Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
            & " " & Mid(dtmBootup, 9, 2) & ":" & _
            Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
            13, 2)
   Else
      strTheDate = "01/01/1901 00:00:00"
   End If
   If IsDate(strTheDate) = False Then strTheDate = "01/01/1901 00:00:00"
    WMIDateStringToDate = CDate(strTheDate)
End Function


And see how you go.  For those computers that would have had an error, you'll get an uptime of somewhere near 36500 days.....incorrect obviously, but we'll check that later once we know which machines are like that.

Regards.

Rob.
0
 
bsharathAuthor Commented:
Rob sorry for the delay in responding.My laptop has an motherboard issue and i have given it for service.I am at my relatives place to Browse now.

For most of the machines i get as "OFF" does this mean that the macro queries each machine directly to get the info.?.If yes then the macro works fine.
Except this message and some data as 36500


"Microsoft office excel is waiting for another application to complete an OLE action.

0
 
RobSampsonCommented:
Yes, if it reports OFF that means it could not ping the machine, and that means it will not be able to get the LastBootUpTime.  The machine must be on so it can read the last boot up time from the OS.

Regards,

Rob.
0
 
bsharathAuthor Commented:
Thanks Rob....
0
All Courses

From novice to tech pro — start learning today.