|
| 1 | +Function GetSystemEnclosureInstancesUsingWMINamespace(ByRef arrSystemEnclosureInstances, ByVal objWMINamespace) |
| 2 | + 'region FunctionMetadata #################################################### |
| 3 | + ' Assuming that objWMINamespace represents a successful connection to the root\CIMv2 |
| 4 | + ' WMI namespace, this function retrieves the Win32_SystemEnclosure instances and stores |
| 5 | + ' them in arrSystemEnclosureInstances. |
| 6 | + ' |
| 7 | + ' The function takes two positional arguments: |
| 8 | + ' - The first argument (arrSystemEnclosureInstances) is populated upon success with the |
| 9 | + ' system enclosure instances returned from WMI of type Win32_SystemEnclosure |
| 10 | + ' - The second argument (objWMINamespace) is a WMI Namespace connection argument that must |
| 11 | + ' already be connected to the WMI namespace root\CIMv2 |
| 12 | + ' |
| 13 | + ' If Win32_SystemEnclosure instances were retrieved successfully, then the function returns |
| 14 | + ' a positive integer equal to the number of Win32_SystemEnclosure instances retrieved. |
| 15 | + ' Usually there is one Win32_SystemEnclosure, and this function therefore returns 1. |
| 16 | + ' However, in some circumstances (e.g., a docking station attached to a laptop/tablet), |
| 17 | + ' more than one Win32_SystemEnclosure instance is present. In these circumstances, the |
| 18 | + ' function would return 2, 3, etc. If an error occurs, the function returns a negative |
| 19 | + ' integer. If no error occurred but no instances could be retrieved, then the function |
| 20 | + ' returns zero. |
| 21 | + ' |
| 22 | + ' Example: |
| 23 | + ' intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null) |
| 24 | + ' If intReturnCode = 0 Then |
| 25 | + ' ' Successfully connected to the local computer's root\CIMv2 WMI Namespace |
| 26 | + ' intReturnCode = GetSystemEnclosureInstancesUsingWMINamespace(arrSystemEnclosureInstances, objSWbemServicesWMINamespace) |
| 27 | + ' If intReturnCode > 0 Then |
| 28 | + ' ' One or more Win32_SystemEnclosure instances were retrieved. The first |
| 29 | + ' ' instance is available at arrSystemEnclosureInstances.ItemIndex(0) and the |
| 30 | + ' ' number of instances is available at arrSystemEnclosureInstances.Count. In |
| 31 | + ' ' other words, the upper array boundary/index is |
| 32 | + ' ' (arrSystemEnclosureInstances.Count - 1). |
| 33 | + ' Else |
| 34 | + ' ' No Win32_SystemEnclosure instances were retrieved |
| 35 | + ' End If |
| 36 | + ' End If |
| 37 | + ' |
| 38 | + ' Version: 1.0.20210624.0 |
| 39 | + 'endregion FunctionMetadata #################################################### |
| 40 | + |
| 41 | + 'region License #################################################### |
| 42 | + ' Copyright 2021 Frank Lesniak |
| 43 | + ' |
| 44 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 45 | + ' software and associated documentation files (the "Software"), to deal in the Software |
| 46 | + ' without restriction, including without limitation the rights to use, copy, modify, merge, |
| 47 | + ' publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 48 | + ' persons to whom the Software is furnished to do so, subject to the following conditions: |
| 49 | + ' |
| 50 | + ' The above copyright notice and this permission notice shall be included in all copies or |
| 51 | + ' substantial portions of the Software. |
| 52 | + ' |
| 53 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 54 | + ' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 55 | + ' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 56 | + ' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 57 | + ' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 58 | + ' DEALINGS IN THE SOFTWARE. |
| 59 | + 'endregion License #################################################### |
| 60 | + |
| 61 | + 'region DownloadLocationNotice #################################################### |
| 62 | + ' The most up-to-date version of this script can be found on the author's GitHub repository |
| 63 | + ' at https://github.com/franklesniak/sysadmin-accelerator |
| 64 | + 'endregion DownloadLocationNotice #################################################### |
| 65 | + |
| 66 | + 'region Acknowledgements #################################################### |
| 67 | + ' None! |
| 68 | + 'endregion Acknowledgements #################################################### |
| 69 | + |
| 70 | + 'region DependsOn #################################################### |
| 71 | + ' TestObjectForData() |
| 72 | + ' TestObjectIsAnyTypeOfInteger() |
| 73 | + 'endregion DependsOn #################################################### |
| 74 | + |
| 75 | + Dim intFunctionReturn |
| 76 | + Dim intReturnMultiplier |
| 77 | + Dim arrWorkingSystemEnclosureInstances |
| 78 | + Dim intTemp |
| 79 | + |
| 80 | + Err.Clear |
| 81 | + |
| 82 | + intFunctionReturn = 0 |
| 83 | + intReturnMultiplier = 1 |
| 84 | + |
| 85 | + If TestObjectForData(objWMINamespace) <> True Then |
| 86 | + intFunctionReturn = intFunctionReturn + (-2 * intReturnMultiplier) |
| 87 | + Else |
| 88 | + On Error Resume Next |
| 89 | + Set arrWorkingSystemEnclosureInstances = objWMINamespace.InstancesOf("Win32_SystemEnclosure") |
| 90 | + If Err Then |
| 91 | + On Error Goto 0 |
| 92 | + Err.Clear |
| 93 | + intFunctionReturn = intFunctionReturn + (-3 * intReturnMultiplier) |
| 94 | + Else |
| 95 | + intTemp = arrWorkingSystemEnclosureInstances.Count |
| 96 | + If Err Then |
| 97 | + On Error Goto 0 |
| 98 | + Err.Clear |
| 99 | + intFunctionReturn = intFunctionReturn + (-4 * intReturnMultiplier) |
| 100 | + Else |
| 101 | + On Error Goto 0 |
| 102 | + If TestObjectIsAnyTypeOfInteger(intTemp) = False Then |
| 103 | + intFunctionReturn = intFunctionReturn + (-5 * intReturnMultiplier) |
| 104 | + Else |
| 105 | + If intTemp < 0 Then |
| 106 | + intFunctionReturn = intFunctionReturn + (-6 * intReturnMultiplier) |
| 107 | + Else |
| 108 | + ' intTemp >= 0 |
| 109 | + intFunctionReturn = intTemp |
| 110 | + ' 0 would be returned if there are no Win32_SystemEnclosure instances |
| 111 | + End If |
| 112 | + End If |
| 113 | + End If |
| 114 | + End If |
| 115 | + End If |
| 116 | + |
| 117 | + If intFunctionReturn > 0 Then |
| 118 | + On Error Resume Next |
| 119 | + Set arrSystemEnclosureInstances = objWMINamespace.InstancesOf("Win32_SystemEnclosure") |
| 120 | + If Err Then |
| 121 | + On Error Goto 0 |
| 122 | + Err.Clear |
| 123 | + intFunctionReturn = (-7 * intReturnMultiplier) |
| 124 | + Else |
| 125 | + On Error Goto 0 |
| 126 | + End If |
| 127 | + End If |
| 128 | + |
| 129 | + GetSystemEnclosureInstancesUsingWMINamespace = intFunctionReturn |
| 130 | +End Function |
0 commit comments