Solved

WMI Uptime Script Reporting Incorrect Number on W2k Server

Posted on 2014-02-13
16
745 Views
Last Modified: 2014-02-25
Trying to use VBS from our monitoring server to generate an alarm for a W2k server that has uptime 25 hours or more.  Its the best way I can think of to get an alarm if its daily reboot function hangs for whatever reason.  After much testing I finally got the script below working but I realized it is reporting the incorrect amount of hours.  I used uptime.exe it shows it as 1 day 11 hours (35 hrs total) and this script is reporting 2433 minutes (40.55 hours).  I really don't understand (yet) the whole process of converting UTC to a real number and I'm not sure I want to is there anyone familiar with this script that could provide a fix or at least reason the reporting is off?

---script---
strComputer = "servername.domain.com"

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("n", dtmLastBootUpTime, Now) 
    Wscript.Echo "Message: System Uptime Alarm"
    Wscript.Echo "Data:"
    Wscript.Echo "Uptime"&vbTab& dtmSystemUptime
    WScript.Quit(0)
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
Comment
Question by:mcburn13
  • 10
  • 6
16 Comments
 
LVL 8

Accepted Solution

by:
TheNautican earned 500 total points
ID: 39857371
Although your code looked correct to me, i did find this after a quick google search. Read out is nicer.

Regards,
-Naut

Option Explicit
     Dim objOS, dateLastBoot, nSystemUptime
     Dim nDays, nHours, nMinutes
     '
     ' Get the Windows Home Server OS object
     '
     For Each objOS in GetObject( _
         "winmgmts:").InstancesOf ("Win32_OperatingSystem")
         '
         ' Return the last boot up time and
         ' convert it to a Date object
         '
         dateLastBoot = ConvertToDate(objOS.LastBootUpTime)
         '
         ' Calculate the number of minutes between then and now
         '
         nSystemUptime = DateDiff("n", dateLastBoot, Now)
         '
         ' Convert the total minutes into hours, days, and minutes
         '
         nDays = Int(nSystemUptime / 1440)
         nHours = Int (((nSystemUptime / 1440) - nDays) * 24)
         nMinutes = nSystemUptime Mod 60
         '
         ' Display the result
         '
         Wscript.Echo "Last Boot: " & dateLastBoot & vbCrLf & _
                      "System Uptime: " & _
                      nDays & " days and " & _
                      nHours & " hours and " & _
                      nMinutes & " minutes"
     Next
     '
     ' This function takes a datetime string and converts
     ' it to a real date and time object
     '
     Function ConvertToDate(strDate)
         Dim strYear, strMonth, strDay
         Dim strHour, strMinute, strSecond
         strYear = Left(strDate, 4)
         strMonth = Mid(strDate, 5, 2)
         strDay = Mid(strDate, 7, 2)
         strHour = Mid(strDate, 9, 2)
         strMinute = Mid(strDate, 11, 2)
         strSecond = Mid(strDate, 13, 2)
         ConvertToDate = DateSerial(strYear, strMonth, strDay) & " " & _
                    TimeSerial(strHour, strMinute, strSecond)
End Function

Open in new window

0
 
LVL 1

Author Comment

by:mcburn13
ID: 39857681
I found that script too but I need the output to be only the total number of hours in a whole #. This script outputs days THEN hours.  If I can figure out how to change this script to add up only the total hours I think I'd be golden..
0
 
LVL 8

Expert Comment

by:TheNautican
ID: 39857855
how would you like an uptime of say, 2 days, 5 hours, and 35 minutes to be displayed?
53hrs or 54hrs?

-Naut
0
 
LVL 1

Author Comment

by:mcburn13
ID: 39857949
53 hours would be fine...
0
 
LVL 8

Assisted Solution

by:TheNautican
TheNautican earned 500 total points
ID: 39858103
I left all the code there, just commented out what was not needed.

Option Explicit
     Dim objOS, dateLastBoot, nSystemUptime
     Dim nDays, nHours, nMinutes
     '
     ' Get the Windows Home Server OS object
     '
     For Each objOS in GetObject( _
         "winmgmts:").InstancesOf ("Win32_OperatingSystem")
         '
         ' Return the last boot up time and
         ' convert it to a Date object
         '
         dateLastBoot = ConvertToDate(objOS.LastBootUpTime)
         '
         ' Calculate the number of minutes between then and now
         '
         nSystemUptime = DateDiff("n", dateLastBoot, Now)
	Wscript.Echo Int( nSystemUptime / 60 ) & " Hrs"
         ' Convert the total minutes into hours, days, and minutes
         '
         'nDays = Int(nSystemUptime / 1440)
         'nHours = Int (((nSystemUptime / 1440) - nDays) * 24)
         'nMinutes = nSystemUptime Mod 60
         '
         ' Display the result
         '
         'Wscript.Echo "Last Boot: " & dateLastBoot & vbCrLf & _
         '             "System Uptime: " & _
         '             nDays & " days and " & _
         '             nHours & " hours and " & _
         '             nMinutes & " minutes"
     Next
     '
     ' This function takes a datetime string and converts
     ' it to a real date and time object
     '
     Function ConvertToDate(strDate)
         Dim strYear, strMonth, strDay
         Dim strHour, strMinute, strSecond
         strYear = Left(strDate, 4)
         strMonth = Mid(strDate, 5, 2)
         strDay = Mid(strDate, 7, 2)
         strHour = Mid(strDate, 9, 2)
         strMinute = Mid(strDate, 11, 2)
         strSecond = Mid(strDate, 13, 2)
         ConvertToDate = DateSerial(strYear, strMonth, strDay) & " " & _
                    TimeSerial(strHour, strMinute, strSecond)
End Function
                                            

Open in new window


Regards,
-Naut
0
 
LVL 1

Author Comment

by:mcburn13
ID: 39859032
Thanks for trying it works fine on my Win7 machine but on this Windows 2000 server it is off by five hours. See the screenshot comparing this script to the uptime.exe utility output.   Really scratching my head on this one. I guess W2k reads these UTC #s differently?
Uptime Output...
0
 
LVL 1

Author Comment

by:mcburn13
ID: 39859043
sounds totally hack but maybe if we can add a "subtract 5" operator in there it would fix this.  But then I'd have to monitor the monitor to make sure the difference is always 5.  Thanks so much for giving this your attention so far.
0
 
LVL 8

Expert Comment

by:TheNautican
ID: 39859623
Ok, we dont have uptime.exe on our 2008 servers but i ran two other things and got the same time the VBS reports. First, I went into Task manager and went to the performance tab. Under system you'll see "Up time" A quick conversion of the days plus the hours matched the vbs script. 2nd I ran C:\systeminfo | find "System Boot Time" and then manually added that time up as well and it matched the VBScript.

I found this blub on microsofts website about uptime.exe. This might
explain why you are getting different times.


It is important to note that the uptime and availability measurements reflect the local view of the operating system only. The "Total Uptime" measurement refers to the sum of all the time during the measured period where the operating system reported itself to be in normal operation. It does not reflect times when the server might be unavailable for other reasons, such at network outages.

-Naut
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:mcburn13
ID: 39859809
The thing is I'm using a monitoring utility that needs a single whole number to report on. I was able to get it in that format with your updated script its just that it is reporting 5 hours off.  I can't use uptime.exe as it only outputs in a certain format.

This has to be something with W2k as this script reports correctly on later OS's. I am trying to figure out how to fudge that time converter to subtract 5 so maybe that would work for me. If anyone knows how let me know...
0
 
LVL 1

Assisted Solution

by:mcburn13
mcburn13 earned 0 total points
ID: 39859821
I able to get what i THINK I need by adding it here:
Wscript.Echo "Uptime" &vbTab & Int( nSystemUptime / 60 - 5)

Open in new window


will test for a bit and share my results...
0
 
LVL 1

Author Comment

by:mcburn13
ID: 39859868
I think my last obstacle is being able to specify the servername to run against.  In my previous script I used strComputer but not sure how to insert that with the current.
0
 
LVL 1

Assisted Solution

by:mcburn13
mcburn13 earned 0 total points
ID: 39859886
sorry for all the chatter I think I figured out a hack/slash way by adding it here

before
For Each objOS in GetObject( _
         "winmgmts:").InstancesOf ("Win32_OperatingSystem")

Open in new window


after
For Each objOS in GetObject( _
         "winmgmts:\\SERVERNAME").InstancesOf ("Win32_OperatingSystem")

Open in new window

0
 
LVL 8

Expert Comment

by:TheNautican
ID: 39861854
Might I ask what you are using as the base to compare the script to? Also, how does that base compare to the other methods such as What Task Manager shows for the uptime?

-Naut
0
 
LVL 1

Author Comment

by:mcburn13
ID: 39861881
I just know when the machine was rebooted- the uptime.exe utility will match the CPU idle time and coincide with the time it was restarted. For some reason this script works perfectly (as you wrote it) on Operating Systems later than W2k but for some reason W2k reports it with an additional 5 hours added to it.   With my - 5 operator in there, it is now reporting it spot on but couldn't have done it without your help thanks!
0
 
LVL 8

Expert Comment

by:TheNautican
ID: 39862201
After further research there does appear to be something wrong with w2k servers as pointed out by the Scripting Guy.
http://blogs.technet.com/b/heyscriptingguy/archive/2005/08/02/how-can-i-determine-the-uptime-for-a-server.aspx

Try this code to see if you get the same 5 hour error.

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("n", dtmLastBootUpTime, Now)
    dtmSystemUptime = Int(dtmSystemUptime / 60)
    Wscript.Echo dtmSystemUptime & " Hrs"
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
 
LVL 1

Author Closing Comment

by:mcburn13
ID: 39885108
i came up with the "Subtract 5" operator to add to the script
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
firstChar challenge 13 85
wordmultiple challenge 12 90
Hide vba in gp 7 43
Basic Java Case or If-Else statement... 3 5
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now