Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script for Determining System Uptime

321 views
Skip to first unread message

Alan Tang

unread,
Mar 27, 2003, 11:07:54 PM3/27/03
to
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & 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

Would anyone can tell me why "CDate" have error?

Thanks!


--
Best Regards,
Alan Tang


Torgeir Bakken (MVP)

unread,
Mar 28, 2003, 4:51:26 AM3/28/03
to
Alan Tang wrote:

> strComputer = "."
> Set objWMIService = GetObject("winmgmts:" _
> & "{impersonationLevel=impersonate}!\\" & strComputer &
> "\root\cimv2")
> Set colOperatingSystems = objWMIService.ExecQuery _
> ("Select * from Win32_OperatingSystem")
> For Each objOS in colOperatingSystems
> dtmBootup = objOS.LastBootUpTime
> dtmLastBootupTime = WMIDateStringToDate(dtmBootup)

> (snip)

Hi

Try this version (it will correctly adjust for Timezone data as well):


strComputer = "." ' "." for local computer

On Error Resume Next


Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2")

If Err.Number = 0 Then
On Error Goto 0


Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOS in colOperatingSystems

dtmLastBootUpTime = ConvWbemTime(objOS.LastBootUpTime)
dtmSystemUptimeInHours = DateDiff("h", dtmLastBootUpTime, Now)
dtmSystemUptimeInDays = DateDiff("d", dtmLastBootUpTime, Now)
Wscript.Echo "Computer: " & strComputer _
& " started at " & dtmLastBootUpTime
Wscript.Echo "Uptime in hours: " & dtmSystemUptimeInHours
Wscript.Echo "Uptime in days: " & dtmSystemUptimeInDays & vbCrlf
Next

Else
Wscript.Echo "Could not connect to computer with WMI: " _
& strComputer
End If


Function ConvWbemTime(DMTFformat)
Dim sYear, sMonth, sDay, sHour, sMinutes, sSeconds
sYear = mid(DMTFformat, 1, 4)
sMonth = mid(DMTFformat, 5, 2)
sDay = mid(DMTFformat, 7, 2)
sHour = mid(DMTFformat, 9, 2)
sMinutes = mid(DMTFformat, 11, 2)
sSeconds = mid(DMTFformat, 13, 2)

sTimezone = Right(DMTFformat, 4)
' yyyy-mm-dd hh:mm:ss
ConvWbemTime = sYear & "-" & sMonth & "-" & sDay & " " _
& sHour & ":" & sMinutes & ":" & sSeconds

' adjust for Timezone, DateAdd will also set date format to
' computer default
ConvWbemTime = DateAdd("n", sTimezone, ConvWbemTime)
End Function

--
torgeir
Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of the 1328 page
Scripting Guide: http://www.microsoft.com/technet/scriptcenter


Torgeir Bakken (MVP)

unread,
Mar 29, 2003, 8:50:44 PM3/29/03
to
Alan Tang wrote:

> Thanks for your script but it have error! WOuld you mind to help me
> again?
>
> ---------------------------
> Windows Script Host
> ---------------------------
> Script: D:\temp\t.vbs
> Line: 45
> Char: 3
> Error: Type mismatch: 'ConvWbemTime'
> Code: 800A000D
> Source: Microsoft VBScript runtime error
>
> ---------------------------
> OK
> ---------------------------

Hi

When testing this on a couple of computers, I found that on one computer Win2k
SP2), LastBootUpTime returned Null, and I got the same error as you. I upgraded
to SP3, but it did not help.


Please try this script and report back the output:

strComputer = "." ' "." for local computer

Set objWMIService = GetObject("winmgmts:" _


& "{impersonationLevel=impersonate}!\\" & strComputer _
& "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _


("Select * from Win32_OperatingSystem")

For Each objOS in colOperatingSystems

On Error Resume Next
Wscript.Echo "LastBootUpTime: " & objOS.LastBootUpTime & vbCrlf _
& "LastBootUpTime Typename: " & TypeName(objOS.LastBootUpTime)
Next


Some non-WMI command line utilities to get the uptime (you could parse the
output from a vbscript file if needed):

==================================================
Uptime.exe Tool Allows You to Estimate Server Availability with Windows NT 4.0
SP4 or Higher
http://support.microsoft.com/default.aspx?scid=KB;en-us;232243

Download:
http://www.microsoft.com/ntserver/nts/downloads/management/uptime/default.asp


==================================================
Psinfo.exe in the free PsTools suite from http://www.sysinternals.com , example
output:

PsInfo v1.2 - local and remote system information viewer
Copyright (C) 2001 Mark Russinovich
Sysinternals - www.sysinternals.com

System information for \\xxxxxxx:
Uptime: 2 days, 18 hours, 20 minutes, 36 seconds
Kernel version: Microsoft Windows 2000, Uniprocessor Free
Product type: Professional
Product version: 5.0
Service pack: 2
Kernel build number: 2195
(a lot more info removed)


==================================================
SRVINFO.EXE from the Windows 2000 Resource Kit

C:\>SrvInfo.exe -ns

Server Name: xxxxxxx
Security: Users
NT Type: NT Advanced Server -
Version: 5.0
Build: 2195, Service Pack 2
(snip)
System Up Time: 8 Days, 4 Hr, 9 Min, 16 Sec

0 new messages