Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

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
Avatar of sr75
sr75
Flag of United States of America image

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

Avatar of bsharath

ASKER

I just get this box..

---------------------------
Windows Script Host
---------------------------
189
---------------------------
OK  
---------------------------
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.
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...
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
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Rob this works fine but i get just 19 machines when run
Can we even mention the machine names in colum A?
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.
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))
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.
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.

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.
Thanks Rob....