|
| 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