|
| 1 | +Function GetComputerSystemProductIdentifyingNumberUsingComputerSystemProductInstances(ByRef strComputerSystemProductIdentifyingNumber, ByVal arrComputerSystemProductInstances) |
| 2 | + 'region FunctionMetadata ####################################################### |
| 3 | + ' Assuming that arrComputerSystemProductInstances represents an array / collection |
| 4 | + ' of the available computer system product instances (of type |
| 5 | + ' Win32_ComputerSystemProduct), this function obtains the computer system product |
| 6 | + ' identifying number. |
| 7 | + ' |
| 8 | + ' The function takes two positional arguments: |
| 9 | + ' - The first argument (strComputerSystemProductIdentifyingNumber) is populated |
| 10 | + ' upon success with a string containing the computer system product identifying |
| 11 | + ' number as reported by WMI. |
| 12 | + ' - The second argument (arrComputerSystemProductInstances) is an array/collection |
| 13 | + ' of objects of class Win32_ComputerSystemProduct |
| 14 | + ' |
| 15 | + ' The function returns a 0 if the computer system product identifying number was |
| 16 | + ' obtained successfully. It returns a negative integer if an error occurred |
| 17 | + ' retrieving the computer system product identifying number. Finally, it returns a |
| 18 | + ' positive integer if the computer system product identifying number was obtained, |
| 19 | + ' but multiple computer system product instances were present that contained data |
| 20 | + ' for the computer system product identifying number. When this happens, only the |
| 21 | + ' first Win32_ComputerSystemProduct instance containing data for the computer |
| 22 | + ' system product identifying number is used. |
| 23 | + ' |
| 24 | + ' Example: |
| 25 | + ' intReturnCode = GetComputerSystemProductInstances(arrComputerSystemProductInstances) |
| 26 | + ' If intReturnCode >= 0 Then |
| 27 | + ' ' At least one Win32_ComputerSystemProduct instance was retrieved |
| 28 | + ' ' successfully |
| 29 | + ' intReturnCode = GetComputerSystemProductIdentifyingNumberUsingComputerSystemProductInstances(strComputerSystemProductIdentifyingNumber, arrComputerSystemProductInstances) |
| 30 | + ' If intReturnCode >= 0 Then |
| 31 | + ' ' The computer system product identifying number was retrieved successfully and is |
| 32 | + ' ' stored in strComputerSystemProductIdentifyingNumber |
| 33 | + ' End If |
| 34 | + ' End If |
| 35 | + ' |
| 36 | + ' Version: 1.0.20230424.0 |
| 37 | + 'endregion FunctionMetadata ####################################################### |
| 38 | + |
| 39 | + 'region License ################################################################ |
| 40 | + ' Copyright 2023 Frank Lesniak |
| 41 | + ' |
| 42 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 43 | + ' this software and associated documentation files (the "Software"), to deal in the |
| 44 | + ' Software without restriction, including without limitation the rights to use, |
| 45 | + ' copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 46 | + ' Software, and to permit persons to whom the Software is furnished to do so, |
| 47 | + ' subject to the following conditions: |
| 48 | + ' |
| 49 | + ' The above copyright notice and this permission notice shall be included in all |
| 50 | + ' copies or substantial portions of the Software. |
| 51 | + ' |
| 52 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 53 | + ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 54 | + ' FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 55 | + ' COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 56 | + ' AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 57 | + ' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 58 | + 'endregion License ################################################################ |
| 59 | + |
| 60 | + 'region DownloadLocationNotice ################################################# |
| 61 | + ' The most up-to-date version of this script can be found on the author's GitHub |
| 62 | + ' repository at https://github.com/franklesniak/sysadmin-accelerator |
| 63 | + 'endregion DownloadLocationNotice ################################################# |
| 64 | + |
| 65 | + 'region Acknowledgements ####################################################### |
| 66 | + ' None! |
| 67 | + 'endregion Acknowledgements ####################################################### |
| 68 | + |
| 69 | + 'region DependsOn ############################################################## |
| 70 | + ' TestObjectForData() |
| 71 | + ' TestObjectIsAnyTypeOfInteger() |
| 72 | + 'endregion DependsOn ############################################################## |
| 73 | + |
| 74 | + Dim intFunctionReturn |
| 75 | + Dim intReturnMultiplier |
| 76 | + Dim intTemp |
| 77 | + Dim intCounterA |
| 78 | + Dim strInterimResult |
| 79 | + Dim strOldInterimResult |
| 80 | + Dim strResultToReturn |
| 81 | + Dim intCountOfComputerSystemProductIdentifyingNumbers |
| 82 | + |
| 83 | + Err.Clear |
| 84 | + |
| 85 | + intFunctionReturn = 0 |
| 86 | + intReturnMultiplier = 128 |
| 87 | + strInterimResult = "" |
| 88 | + strResultToReturn = "" |
| 89 | + intCountOfComputerSystemProductIdentifyingNumbers = 0 |
| 90 | + |
| 91 | + If TestObjectForData(arrComputerSystemProductInstances) <> True Then |
| 92 | + intFunctionReturn = intFunctionReturn + (-1 * intReturnMultiplier) |
| 93 | + Else |
| 94 | + On Error Resume Next |
| 95 | + intTemp = arrComputerSystemProductInstances.Count |
| 96 | + If Err Then |
| 97 | + On Error Goto 0 |
| 98 | + Err.Clear |
| 99 | + intFunctionReturn = intFunctionReturn + (-2 * intReturnMultiplier) |
| 100 | + Else |
| 101 | + On Error Goto 0 |
| 102 | + If TestObjectIsAnyTypeOfInteger(intTemp) = False Then |
| 103 | + intFunctionReturn = intFunctionReturn + (-3 * intReturnMultiplier) |
| 104 | + Else |
| 105 | + If intTemp < 0 Then |
| 106 | + intFunctionReturn = intFunctionReturn + (-4 * intReturnMultiplier) |
| 107 | + ElseIf intTemp = 0 Then |
| 108 | + intFunctionReturn = intFunctionReturn + (-5 * intReturnMultiplier) |
| 109 | + Else |
| 110 | + For intCounterA = 0 To (intTemp - 1) |
| 111 | + strOldInterimResult = strInterimResult |
| 112 | + On Error Resume Next |
| 113 | + strInterimResult = arrComputerSystemProductInstances.ItemIndex(intCounterA).IdentifyingNumber |
| 114 | + If Err Then |
| 115 | + On Error Goto 0 |
| 116 | + Err.Clear |
| 117 | + strInterimResult = strOldInterimResult |
| 118 | + Else |
| 119 | + On Error Goto 0 |
| 120 | + If TestObjectForData(strInterimResult) <> True Then |
| 121 | + strInterimResult = strOldInterimResult |
| 122 | + Else |
| 123 | + ' Found a result with real data |
| 124 | + If TestObjectForData(strResultToReturn) = False Then |
| 125 | + strResultToReturn = strInterimResult |
| 126 | + End If |
| 127 | + intCountOfComputerSystemProductIdentifyingNumbers = intCountOfComputerSystemProductIdentifyingNumbers + 1 |
| 128 | + End If |
| 129 | + End If |
| 130 | + Next |
| 131 | + End If |
| 132 | + End If |
| 133 | + End If |
| 134 | + End If |
| 135 | + |
| 136 | + If intFunctionReturn >= 0 Then |
| 137 | + ' No error has occurred yet |
| 138 | + If intCountOfComputerSystemProductIdentifyingNumbers = 0 Then |
| 139 | + ' No result found |
| 140 | + intFunctionReturn = intFunctionReturn + (-5 * intReturnMultiplier) |
| 141 | + Else |
| 142 | + intFunctionReturn = intCountOfComputerSystemProductIdentifyingNumbers - 1 |
| 143 | + End If |
| 144 | + End If |
| 145 | + |
| 146 | + If intFunctionReturn >= 0 Then |
| 147 | + strComputerSystemProductIdentifyingNumber = strResultToReturn |
| 148 | + End If |
| 149 | + |
| 150 | + GetComputerSystemProductIdentifyingNumberUsingComputerSystemProductInstances = intFunctionReturn |
| 151 | +End Function |
0 commit comments