JamesTX10
asked on
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
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
ASKER
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
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
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
ASKER
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.
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.
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.
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
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. That works great.
Open in new window