Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3699
  • Last Modified:

VBS last system boot time, uptime and current time

I am needing a script that will give me Last system boot time, System uptime and the current time in this format: mm/dd/yyyy hh:mm AM/PM

My script provides most of this but it depends on the setup of the system time. Here is the export from two different servers:
USServer1      Last Boot Up Time: 4/26/2009 9:36:04 AM System Up Time: 48:46:02 Current Local Time: 4\-28\-2009 10:22:06 AM

EUServer1      Last Boot Up Time: 25.04.2009 13:06:29 System Up Time: 76:15:38 Current Local Time: 4\-28\-2009 17:22:07

I want the results for all servers (no matter local time setup) to look like this:
Last Boot Up Time: 04/25/2009 01:06 PM ~ System Up Time: 3 Days 4 Hours ~ Current Local Time: 04/28/2009 05:22 PM



strComputer = "." ' Local computer
 
set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
for each objOS in colOS
	objWMIDateTime.Value = objOS.LastBootUpTime
	Wscript.Echo "Last Boot Up Time: " & objWMIDateTime.GetVarDate & " " & _
		"System Up Time: " &  TimeSpan(objWMIDateTime.GetVarDate,Now) & _
		" Current Local Time: " & Month(date) & "-" & Day(date) & "-" & Year(date)  & _
		 " " & FormatDateTime(now(),3)
next
 
Function TimeSpan(dt1, dt2) 
	' Function to display the difference between
	' 2 dates in hh:mm:ss format
	If (isDate(dt1) And IsDate(dt2)) = false Then 
		TimeSpan = "00:00:00" 
		Exit Function 
        End If 
 
        seconds = Abs(DateDiff("S", dt1, dt2)) 
        minutes = seconds \ 60 
        hours = minutes \ 60 
        minutes = minutes mod 60 
        seconds = seconds mod 60 
 
        if len(hours) = 1 then hours = "0" & hours 
 
        TimeSpan = hours & ":" & _ 
            RIGHT("00" & minutes, 2) & ":" & _ 
            RIGHT("00" & seconds, 2) 
End Function

Open in new window

0
JamesTX10
Asked:
JamesTX10
  • 4
  • 4
1 Solution
 
HubasanCommented:
I believe this is what you were looking for:
On Error Resume Next
 
Set oWS = CreateObject("wscript.shell")
Set oNet = CreateObject("wscript.network")
Set oFS = CreateObject("scripting.FileSystemObject")
cTitle = "Get Time Details"
 
sComputer = WScript.Arguments(0)
sDomain = oNet.UserDomain
 
 
sScriptName = WScript.ScriptName
sScriptPath = WScript.ScriptFullName
 
sLog = Replace(sScriptName, ".vbs", "-" & sComputer & ".log")
sLogFile = Replace(sScriptPath, sScriptName, sLog)
 
Set oLogFile = oFS.CreateTextFile(sLogFile,True)
 
If sComputer = "" Then
	Do
	  sComputer = InputBox("Please enter Computer Name of the computer you wish to connect to?", cTitle,sLocCompName)
	  	If Len(sComputer) = 0 Then
	    	sRes = oWS.Popup ("Sorry, you must enter Computer Name to continue.  Do you wish to try again?", , cTitle, vbExclamation+vbYesNo)
	      	If sRes = vbNo Then
	        	WScript.Quit
	        End If
	    End If
	Loop Until Len(sComputer) <> 0
End If
 
If sComputer = "." Then
	sComputer = oNet.ComputerName
End If
 
oLogFile.WriteLine sComputer & vbTab & "Last Boot Up Time: " & LastBootTime(sComputer) & " ~ " &_
"System Uptime: " & UpTime(sComputer) & " ~ " &_
"Current Local Time: " & LocalDateTime(sComputer)
 
oWS.Run "notepad " & sLogFile, ,False
 
Function UpTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLastBoot = oOS.LastBootUpTime
		sSec = DateDiff("s",UTCtoStandard(sLastBoot), Now)		
	Next
	sMin = sSec\60
	sSec = sSec Mod 60
	sHour = sMin\60
	sMin = sMin Mod 60
	sDay = sHour\24	
  UpTime= "(" & sDay & ")" & " Days, " & sHour Mod 24 &_
  " Hours, " & sMin &" Minutes, " & sSec & " Seconds"
End Function
 
Function LastBootTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLastBoot = oOS.LastBootUpTime		
	Next
 
  LastBootTime = UTCtoStandard(sLastBoot)
End Function
 
Function LocalDateTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLocalDT = oOS.LocalDateTime
	Next  
	LocalDateTime = UTCtoStandard(sLocalDT)
End Function
 
Function UTCtoStandard(CreationDate)
 UTCtoStandard = CDate(Mid(CreationDate, 5, 2) & "/" & _
 Mid(CreationDate, 7, 2) & "/" & Left(CreationDate, 4) _
 & " " & Mid (CreationDate, 9, 2) & ":" & _
 Mid(CreationDate, 11, 2) & ":" & Mid(CreationDate, _
 13, 2))
End Function

Open in new window

0
 
JamesTX10Author Commented:
The output looks correct on one computer that I tested it on. But I need the control part of the script to match mine. strComputer = "." ' Local computer

and I need it to display just like my script does. Not stored in a txt file.

This script will be run locally by Opsware. So the computer will always be local and Opsware will capture the results rather then the txt file.

Thanks
0
 
HubasanCommented:
Sure, here is the shorter version of the script that does what you described.
sComputer = "." 'Local Computer
 
WScript.Echo "Last Boot Up Time: " & LastBootTime(sComputer) & " ~ " &_
"System Uptime: " & UpTime(sComputer) & " ~ " &_
"Current Local Time: " & LocalDateTime(sComputer)
 
Function UpTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLastBoot = oOS.LastBootUpTime
		sSec = DateDiff("s",UTCtoStandard(sLastBoot), Now)		
	Next
	sMin = sSec\60
	sSec = sSec Mod 60
	sHour = sMin\60
	sMin = sMin Mod 60
	sDay = sHour\24	
  UpTime= "(" & sDay & ")" & " Days, " & sHour Mod 24 &_
  " Hours, " & sMin &" Minutes, " & sSec & " Seconds"
End Function
 
Function LastBootTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLastBoot = oOS.LastBootUpTime		
	Next
 
  LastBootTime = UTCtoStandard(sLastBoot)
End Function
 
Function LocalDateTime(sComputer)
	Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
	Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
	For Each oOS in colOS
		sLocalDT = oOS.LocalDateTime
	Next  
	LocalDateTime = UTCtoStandard(sLocalDT)
End Function
 
Function UTCtoStandard(CreationDate)
 UTCtoStandard = CDate(Mid(CreationDate, 5, 2) & "/" & _
 Mid(CreationDate, 7, 2) & "/" & Left(CreationDate, 4) _
 & " " & Mid (CreationDate, 9, 2) & ":" & _
 Mid(CreationDate, 11, 2) & ":" & Mid(CreationDate, _
 13, 2))
End Function

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JamesTX10Author Commented:
The script output format is working except for on our EU servers.

USSERVER1      Last Boot Up Time: 4/24/2009 3:10:48 PM \~ System Uptime: \(3\) Days, 22 Hours, 23 Minutes, 46 Seconds \~ Current Local Time: 4/28/2009 1:34:34 PM
EUSERVER1      Last Boot Up Time: 25.04.2009 10:09:36 \~ System Uptime: \(3\) Days, 10 Hours, 25 Minutes, 4 Seconds \~ Current Local Time: 28.04.2009 20:34:40

Also is there a way to remove the "\" ? It does not show up if I run the script from my desktop but it does show up in the output from running the script with Opsware. If not that is fine as I will remove it with a macro on the report side.
0
 
HubasanCommented:
EU servers record dates using decimal dots instead of slashes like we do in US. I have corrected this in the script and now the local time should display the same for both:

As for the backslash that is appearing in the output, you should look into that with Opsware since the script is not interacting with it. Opsware is just running the script and interpreting on it's own what is going on there.
sComputer = "." 'Local Computer
 
WScript.Echo "Last Boot Up Time: " & Replace(LastBootTime(sComputer), ".","/") & " ~ " &_
"System Uptime: " & UpTime(sComputer) & " ~ " &_
"Current Local Time: " & Replace(LocalDateTime(sComputer), ".","/")
 
Function UpTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLastBoot = oOS.LastBootUpTime
                sSec = DateDiff("s",UTCtoStandard(sLastBoot), Now)              
        Next
        sMin = sSec\60
        sSec = sSec Mod 60
        sHour = sMin\60
        sMin = sMin Mod 60
        sDay = sHour\24 
  UpTime= "(" & sDay & ")" & " Days, " & sHour Mod 24 &_
  " Hours, " & sMin &" Minutes, " & sSec & " Seconds"
End Function
 
Function LastBootTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLastBoot = oOS.LastBootUpTime          
        Next
 
  LastBootTime = UTCtoStandard(sLastBoot)
End Function
 
Function LocalDateTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLocalDT = oOS.LocalDateTime
        Next  
        LocalDateTime = UTCtoStandard(sLocalDT)
End Function
 
Function UTCtoStandard(CreationDate)
 UTCtoStandard = CDate(Mid(CreationDate, 5, 2) & "/" & _
 Mid(CreationDate, 7, 2) & "/" & Left(CreationDate, 4) _
 & " " & Mid (CreationDate, 9, 2) & ":" & _
 Mid(CreationDate, 11, 2) & ":" & Mid(CreationDate, _
 13, 2))
End Function

Open in new window

0
 
JamesTX10Author Commented:
The \ is not a problem.

The EU servers are now showing the / rather then . but the day month order is wrong. Right now they come out like this: Last Boot Up Time: 24/04/2009 DD/MM/YYYY I need it to be MM/DD/YYYY. The US servers are reporting this correctly Last Boot Up Time: 4/26/2009
0
 
HubasanCommented:
Yeah, you are right. I forgot that EU time is DD/MM/YYYY...
Here, I have updated the script with an additional function that will re-format EU date to US Date. Time however will still show as 24 hour time from EU servers rather than 12 hour format. There is no easy way to reformat the time EU time to US time. Hope this can work for you.
sComputer = "." 'Local Computer
 
sLocal = LocalDateTime(sComputer)
sLastBoot = LastBootTime(sComputer)
 
WScript.Echo "Last Boot Up Time: " & GetUSTime(sLastBoot) & " ~ " &_
"System Uptime: " & UpTime(sComputer) & " ~ " &_
"Current Local Time: " & GetUSTime(sLocal)
 
Function GetUSTime(sDT)
	sDay = Day(sDT)
	sMonth = Month(sDT)
	sYear = Year(sDT)
	sTime = TimeValue(sDT)
	GetUSTime = sMonth & "/" & sDay & "/" & sYear & " " & sTime
End Function
 
Function UpTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLastBoot = oOS.LastBootUpTime
                sSec = DateDiff("s",UTCtoStandard(sLastBoot), Now)              
        Next
        sMin = sSec\60
        sSec = sSec Mod 60
        sHour = sMin\60
        sMin = sMin Mod 60
        sDay = sHour\24 
  UpTime= "(" & sDay & ")" & " Days, " & sHour Mod 24 &_
  " Hours, " & sMin &" Minutes, " & sSec & " Seconds"
End Function
 
Function LastBootTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLastBoot = oOS.LastBootUpTime          
        Next
 
  LastBootTime = UTCtoStandard(sLastBoot)
End Function
 
Function LocalDateTime(sComputer)
        Set oWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
        Set colOS = oWMI.ExecQuery("Select * from Win32_OperatingSystem")
        For Each oOS in colOS
                sLocalDT = oOS.LocalDateTime
        Next  
        LocalDateTime = UTCtoStandard(sLocalDT)
End Function
 
Function UTCtoStandard(CreationDate)
 UTCtoStandard = CDate(Mid(CreationDate, 5, 2) & "/" & _
 Mid(CreationDate, 7, 2) & "/" & Left(CreationDate, 4) _
 & " " & Mid (CreationDate, 9, 2) & ":" & _
 Mid(CreationDate, 11, 2) & ":" & Mid(CreationDate, _
 13, 2))
End Function

Open in new window

0
 
JamesTX10Author Commented:
Thanks. That works great.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now