Skip to content

Commit 9b31c94

Browse files
committed
Calculate uptime in seconds
1 parent 2ef6239 commit 9b31c94

File tree

2 files changed

+233
-0
lines changed

2 files changed

+233
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
Function GetComputerUptimeInSeconds(ByRef intSecondsSinceLastBoot)
2+
'region FunctionMetadata ####################################################
3+
' This function obtains the number of seconds since the computer was last booted. In other
4+
' words, it obtains the computer's uptime.
5+
'
6+
' The function takes one positional argument (intSecondsSinceLastBoot), which is populated
7+
' upon success with an integer indicating the number of seconds since the computer was last
8+
' booted
9+
'
10+
' The function returns a 0 if the number of seconds since the computer's last boot was
11+
' obtained successfully (as an integer). It returns a negative integer if an error occurred
12+
' retrieving it. Finally, it returns a positive integer if the number of seconds since the
13+
' last boot was obtained, but multiple operating system instances were present that
14+
' contained data for the last boot date string. When this happens, only the first
15+
' Win32_OperatingSystem instance containing data for the last boot date string is used to
16+
' determine the number of seconds of uptime.
17+
'
18+
' Example:
19+
' intReturnCode = GetComputerUptimeInSeconds(intSecondsSinceLastBoot)
20+
' If intReturnCode >= 0 Then
21+
' ' The number of seconds since last boot (system uptime) was retrieved successfully
22+
' ' and is stored in intSecondsSinceLastBoot
23+
' End If
24+
'
25+
' Version: 1.0.20210729.0
26+
'endregion FunctionMetadata ####################################################
27+
28+
'region License ####################################################
29+
' Copyright 2021 Frank Lesniak
30+
'
31+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
32+
' software and associated documentation files (the "Software"), to deal in the Software
33+
' without restriction, including without limitation the rights to use, copy, modify, merge,
34+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
35+
' persons to whom the Software is furnished to do so, subject to the following conditions:
36+
'
37+
' The above copyright notice and this permission notice shall be included in all copies or
38+
' substantial portions of the Software.
39+
'
40+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
41+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
42+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
43+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
44+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
45+
' DEALINGS IN THE SOFTWARE.
46+
'endregion License ####################################################
47+
48+
'region DownloadLocationNotice ####################################################
49+
' The most up-to-date version of this script can be found on the author's GitHub repository
50+
' at https://github.com/franklesniak/sysadmin-accelerator
51+
'endregion DownloadLocationNotice ####################################################
52+
53+
'region Acknowledgements ####################################################
54+
' None!
55+
'endregion Acknowledgements ####################################################
56+
57+
'region DependsOn ####################################################
58+
' GetComputerUptimeInSecondsUsingCurrentDateTimeComputerSystemInstancesOperatingSystemInstancesAndTimeZoneInstances()
59+
'endregion DependsOn ####################################################
60+
61+
Dim intFunctionReturn
62+
Dim intReturnMultiplier
63+
Dim intReturnCode
64+
65+
Dim objSWbemServicesWMINamespace
66+
Dim arrComputerSystemInstances
67+
Dim arrOperatingSystemInstances
68+
Dim arrTimeZoneInstances
69+
Dim intResultToReturn
70+
71+
intFunctionReturn = 0
72+
intReturnMultiplier = 4194304 * 4
73+
intResultToReturn = Null
74+
75+
intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
76+
If intReturnCode <> 0 Then
77+
intFunctionReturn = intReturnCode * intReturnMultiplier
78+
Else
79+
' Successfully connected to the local computer's root\CIMv2 WMI Namespace
80+
intReturnCode = GetComputerSystemInstancesUsingWMINamespace(arrComputerSystemInstances, objSWbemServicesWMINamespace)
81+
intReturnCode = GetOperatingSystemInstancesUsingWMINamespace(arrOperatingSystemInstances, objSWbemServicesWMINamespace)
82+
intReturnCode = GetTimeZoneInstancesUsingWMINamespace(arrTimeZoneInstances, objSWbemServicesWMINamespace)
83+
intFunctionReturn = GetComputerUptimeInSecondsUsingCurrentDateTimeComputerSystemInstancesOperatingSystemInstancesAndTimeZoneInstances(intResultToReturn, Null, arrComputerSystemInstances, arrOperatingSystemInstances, arrTimeZoneInstances)
84+
End If
85+
86+
If intFunctionReturn >= 0 Then
87+
intSecondsSinceLastBoot = intResultToReturn
88+
End If
89+
90+
GetComputerUptimeInSeconds = intFunctionReturn
91+
End Function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
Function GetComputerUptimeInSecondsUsingCurrentDateTimeComputerSystemInstancesOperatingSystemInstancesAndTimeZoneInstances(ByRef intSecondsSinceLastBoot, ByVal datetimeNow, ByVal arrComputerSystemInstances, ByVal arrOperatingSystemInstances, ByVal arrTimeZoneInstances)
2+
'region FunctionMetadata ####################################################
3+
' Assuming that arrComputerSystemInstances represents an array / collection of the
4+
' available computer system instances (of type Win32_ComputerSystem),
5+
' arrOperatingSystemInstances represents an array / collection of the available operating
6+
' system instances (of type Win32_OperatingSystem), and arrTimeZoneInstances represents an
7+
' array / collection of the available time zone instances (of type Win32_TimeZone), this
8+
' function obtains the number of seconds since the computer was last booted. In other
9+
' words, it obtains the computer's uptime.
10+
'
11+
' The function takes five positional arguments:
12+
' - The first argument (intSecondsSinceLastBoot) is populated upon success with an integer
13+
' indicating the number of seconds since the computer was last booted
14+
' - The second argument (datetimeNow) is a VBScript-native datetime object (VT_DATE) that
15+
' represents the current day and time. Normally it would be set to the equivalent of
16+
' Now(). If no data is supplied for this argument (e.g., Null or a variable set to
17+
' Nothing is passed, the function defaults to using the current datetime.
18+
' - The third argument (arrComputerSystemInstances) is an array/collection of objects of
19+
' class Win32_ComputerSystem
20+
' - The fourth argument (arrOperatingSystemInstances) is an array/collection of objects of
21+
' class Win32_OperatingSystem
22+
' - The fifth argument (arrTimeZoneInstances) is an array/collection of objects of class
23+
' Win32_TimeZone
24+
'
25+
' The function returns a 0 if the number of seconds since the computer's last boot was
26+
' obtained successfully (as an integer). It returns a negative integer if an error occurred
27+
' retrieving it. Finally, it returns a positive integer if the number of seconds since the
28+
' last boot was obtained, but multiple operating system instances were present that
29+
' contained data for the last boot date string. When this happens, only the first
30+
' Win32_OperatingSystem instance containing data for the last boot date string is used to
31+
' determine the number of seconds of uptime.
32+
'
33+
' Example:
34+
' intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
35+
' If intReturnCode = 0 Then
36+
' ' Successfully connected to the local computer's root\CIMv2 WMI Namespace
37+
' intReturnCode = GetComputerSystemInstancesUsingWMINamespace(arrComputerSystemInstances, objSWbemServicesWMINamespace)
38+
' If intReturnCode >= 0 Then
39+
' ' At least one Win32_ComputerSystem instance was retrieved successfully
40+
' intReturnCode = GetOperatingSystemInstancesUsingWMINamespace(arrOperatingSystemInstances, objSWbemServicesWMINamespace)
41+
' If intReturnCode >= 0 Then
42+
' ' At least one Win32_OperatingSystem instance was retrieved successfully
43+
' intReturnCode = GetTimeZoneInstancesUsingWMINamespace(arrTimeZoneInstances, objSWbemServicesWMINamespace)
44+
' If intReturnCode >= 0 Then
45+
' ' At least one Win32_TimeZone instance was retrieved successfully
46+
' datetimeNow = Now()
47+
' intReturnCode = GetComputerUptimeInSecondsUsingCurrentDateTimeComputerSystemInstancesOperatingSystemInstancesAndTimeZoneInstances(intSecondsSinceLastBoot, datetimeNow, arrComputerSystemInstances, arrOperatingSystemInstances, arrTimeZoneInstances)
48+
' If intReturnCode >= 0 Then
49+
' ' The number of seconds since last boot (system uptime) was
50+
' ' retrieved successfully and is stored in intSecondsSinceLastBoot
51+
' End If
52+
' End If
53+
' End If
54+
' End If
55+
' End If
56+
'
57+
' Version: 1.0.20210728.0
58+
'endregion FunctionMetadata ####################################################
59+
60+
'region License ####################################################
61+
' Copyright 2021 Frank Lesniak
62+
'
63+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
64+
' software and associated documentation files (the "Software"), to deal in the Software
65+
' without restriction, including without limitation the rights to use, copy, modify, merge,
66+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
67+
' persons to whom the Software is furnished to do so, subject to the following conditions:
68+
'
69+
' The above copyright notice and this permission notice shall be included in all copies or
70+
' substantial portions of the Software.
71+
'
72+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
73+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
74+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
75+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
76+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
77+
' DEALINGS IN THE SOFTWARE.
78+
'endregion License ####################################################
79+
80+
'region DownloadLocationNotice ####################################################
81+
' The most up-to-date version of this script can be found on the author's GitHub repository
82+
' at https://github.com/franklesniak/sysadmin-accelerator
83+
'endregion DownloadLocationNotice ####################################################
84+
85+
'region Acknowledgements ####################################################
86+
' None!
87+
'endregion Acknowledgements ####################################################
88+
89+
'region DependsOn ####################################################
90+
' TestObjectIsDateTimeContainingData()
91+
' GetComputerLastBootAsNativeDatetimeObjectUsingComputerSystemOperatingSystemAndTimeZoneInstances()
92+
'endregion DependsOn ####################################################
93+
94+
Dim intFunctionReturn
95+
Dim intReturnMultiplier
96+
Dim intReturnCode
97+
98+
Dim intResultToReturn
99+
Dim datetimeLastBootDate
100+
Dim dateTimeWorkingNow
101+
102+
Err.Clear
103+
104+
intFunctionReturn = 0
105+
intReturnMultiplier = 4194304
106+
intResultToReturn = Null
107+
108+
intFunctionReturn = GetComputerLastBootAsNativeDatetimeObjectUsingComputerSystemOperatingSystemAndTimeZoneInstances(datetimeLastBootDate, arrComputerSystemInstances, arrOperatingSystemInstances, arrTimeZoneInstances)
109+
If intFunctionReturn >= 0 Then
110+
' One or more Win32_OperatingSystem instances had a valid last boot date
111+
If TestObjectIsDateTimeContainingData(datetimeNow) <> True Then
112+
On Error Resume Next
113+
dateTimeWorkingNow = Now()
114+
If Err Then
115+
On Error Goto 0
116+
Err.Clear
117+
intFunctionReturn = -1 * intReturnMultiplier
118+
Else
119+
On Error Goto 0
120+
End If
121+
Else
122+
dateTimeWorkingNow = datetimeNow
123+
End If
124+
If intFunctionReturn >= 0 Then
125+
On Error Resume Next
126+
intResultToReturn = DateDiff("s", datetimeLastBootDate, dateTimeWorkingNow)
127+
If Err Then
128+
On Error Goto 0
129+
Err.Clear
130+
intFunctionReturn = -2 * intReturnMultiplier
131+
Else
132+
On Error Goto 0
133+
End if
134+
End If
135+
End If
136+
137+
If intFunctionReturn >= 0 Then
138+
intSecondsSinceLastBoot = intResultToReturn
139+
End If
140+
141+
GetComputerUptimeInSecondsUsingCurrentDateTimeComputerSystemInstancesOperatingSystemInstancesAndTimeZoneInstances = intFunctionReturn
142+
End Function

0 commit comments

Comments
 (0)