|
| 1 | +Function TestComputerIsAmazonWebServicesEC2InstanceUsingComputerSystemComputerSystemProductAndBIOSInstances(ByRef boolIsAmazonWebServicesEC2Instance, ByVal arrComputerSystemInstances, ByVal arrComputerSystemProductInstances, ByVal arrBIOSInstances) |
| 2 | + 'region FunctionMetadata ####################################################### |
| 3 | + ' Assuming that arrComputerSystemInstances represents an array / collection of the |
| 4 | + ' available computer system instances (of type Win32_ComputerSystem), |
| 5 | + ' arrComputerSystemProductInstances represents an array / collection of the |
| 6 | + ' available computer system instances (of type Win32_ComputerSystemProduct), and |
| 7 | + ' arrBIOSInstances represents an array / collection of the available BIOS instances |
| 8 | + ' (of type Win32_BIOS), this function determines if the computer is an Amazon Web |
| 9 | + ' Services (AWS) EC2 instance |
| 10 | + ' |
| 11 | + ' The function takes four positional arguments: |
| 12 | + ' - The first argument (boolIsAmazonWebServicesEC2Instance) is populated upon |
| 13 | + ' success with a boolean value: True when the computer was determined to be an |
| 14 | + ' AWS EC2 instance, False when the computer was determined to not be an AWS EC2 |
| 15 | + ' instance |
| 16 | + ' - The second argument (arrComputerSystemInstances) is a WMI collection/array |
| 17 | + ' that must be pre-populated with a collection of Win32_ComputerSystem objects |
| 18 | + ' - The third argument (arrComputerSystemProductInstances) is a WMI collection/ |
| 19 | + ' array that must be pre-populated with a collection of |
| 20 | + ' Win32_ComputerSystemProduct objects |
| 21 | + ' - The fourth argument (arrBIOSInstances) is a WMI collection/array that must be |
| 22 | + ' pre-populated with a collection of Win32_BIOS objects |
| 23 | + ' |
| 24 | + ' The function returns a 0 when the function successfully evaluated whether the |
| 25 | + ' computer is an AWS EC2 instance. The function returns a negative integer if an |
| 26 | + ' error occurred. |
| 27 | + ' |
| 28 | + ' Note: for Citrix Xen virtual machines, this function will return a false positive |
| 29 | + ' on one out of every 4096 virtual machines. This is because AWS EC2 instances are |
| 30 | + ' detectable when the first three digits of their UUID are EC2, but Citrix Xen |
| 31 | + ' virtual machines will have their UUID start with EC2 one out of every 4096 times. |
| 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 = GetComputerSystemProductInstancesUsingWMINamespace(arrComputerSystemProductInstances, objSWbemServicesWMINamespace) |
| 41 | + ' If intReturnCode >= 0 Then |
| 42 | + ' ' At least one Win32_ComputerSystemProduct instance was retrieved |
| 43 | + ' ' successfully |
| 44 | + ' intReturnCode = GetBIOSInstancesUsingWMINamespace(arrBIOSInstances, objSWbemServicesWMINamespace) |
| 45 | + ' If intReturnCode >= 0 Then |
| 46 | + ' ' At least one Win32_BIOS instance was retrieved successfully |
| 47 | + ' intReturnCode = TestComputerIsAmazonWebServicesEC2InstanceUsingComputerSystemComputerSystemProductAndBIOSInstances(boolIsAmazonWebServicesEC2Instance, arrComputerSystemInstances, arrComputerSystemProductInstances, arrBIOSInstances) |
| 48 | + ' If intReturnCode = 0 Then |
| 49 | + ' ' Successfully tested whether this system is an AWS EC2 |
| 50 | + ' ' instance |
| 51 | + ' If boolIsAmazonWebServicesEC2Instance = True Then |
| 52 | + ' ' Computer is an AWS EC2 instance |
| 53 | + ' Else |
| 54 | + ' ' Computer is not an AWS EC2 instance |
| 55 | + ' End If |
| 56 | + ' End If |
| 57 | + ' End If |
| 58 | + ' End If |
| 59 | + ' End If |
| 60 | + ' End If |
| 61 | + ' |
| 62 | + ' Version: 1.0.20230423.0 |
| 63 | + 'endregion FunctionMetadata ####################################################### |
| 64 | + |
| 65 | + 'region License ################################################################ |
| 66 | + ' Copyright 2023 Frank Lesniak |
| 67 | + ' |
| 68 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 69 | + ' this software and associated documentation files (the "Software"), to deal in the |
| 70 | + ' Software without restriction, including without limitation the rights to use, |
| 71 | + ' copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 72 | + ' Software, and to permit persons to whom the Software is furnished to do so, |
| 73 | + ' subject to the following conditions: |
| 74 | + ' |
| 75 | + ' The above copyright notice and this permission notice shall be included in all |
| 76 | + ' copies or substantial portions of the Software. |
| 77 | + ' |
| 78 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 79 | + ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 80 | + ' FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 81 | + ' COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 82 | + ' AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 83 | + ' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 84 | + 'endregion License ################################################################ |
| 85 | + |
| 86 | + 'region DownloadLocationNotice ################################################# |
| 87 | + ' The most up-to-date version of this script can be found on the author's GitHub |
| 88 | + ' repository at https://github.com/franklesniak/sysadmin-accelerator |
| 89 | + 'endregion DownloadLocationNotice ################################################# |
| 90 | + |
| 91 | + 'region DependsOn ############################################################## |
| 92 | + ' TestObjectForData() |
| 93 | + ' GetComputerManufacturerUsingComputerSystemInstances() |
| 94 | + ' GetComputerUUIDUsingComputerSystemProductInstances() |
| 95 | + ' GetSMBIOSVersionStringUsingBIOSInstances() |
| 96 | + ' TestComputerIsAmazonWebServicesEC2InstanceUsingManufacturerSMBIOSVersionAndUUID() |
| 97 | + 'endregion DependsOn ############################################################## |
| 98 | + |
| 99 | + Dim intFunctionReturn |
| 100 | + Dim intReturnMultiplier |
| 101 | + Dim strComputerManufacturer |
| 102 | + Dim strComputerUUID |
| 103 | + Dim strSMBIOSVersion |
| 104 | + Dim boolInterimResult |
| 105 | + |
| 106 | + intFunctionReturn = 0 |
| 107 | + intReturnMultiplier = 1 |
| 108 | + |
| 109 | + If TestObjectForData(arrComputerSystemInstances) <> True Then |
| 110 | + intFunctionReturn = intFunctionReturn + (-1 * intReturnMultiplier) |
| 111 | + Else |
| 112 | + If TestObjectForData(arrComputerSystemProductInstances) <> True Then |
| 113 | + intFunctionReturn = intFunctionReturn + (-2 * intReturnMultiplier) |
| 114 | + Else |
| 115 | + If TestObjectForData(arrBIOSInstances) <> True Then |
| 116 | + intFunctionReturn = intFunctionReturn + (-3 * intReturnMultiplier) |
| 117 | + End If |
| 118 | + End If |
| 119 | + End If |
| 120 | + |
| 121 | + If intFunctionReturn = 0 Then |
| 122 | + ' No error occurred |
| 123 | + intReturnMultiplier = intReturnMultiplier * 4 |
| 124 | + intReturnCode = GetComputerManufacturerUsingComputerSystemInstances(strComputerManufacturer, arrComputerSystemInstances) |
| 125 | + If intReturnCode >= 0 Then |
| 126 | + ' The computer manufacturer was retrieved successfully and is stored in |
| 127 | + ' strComputerManufacturer |
| 128 | + Else |
| 129 | + ' An error occurred |
| 130 | + intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) |
| 131 | + End If |
| 132 | + End If |
| 133 | + |
| 134 | + If intFunctionReturn = 0 Then |
| 135 | + ' No error occurred |
| 136 | + intReturnMultiplier = intReturnMultiplier * 1024 |
| 137 | + intReturnCode = GetComputerUUIDUsingComputerSystemProductInstances(strComputerUUID, arrComputerSystemProductInstances) |
| 138 | + If intReturnCode >= 0 Then |
| 139 | + ' The computer's UUID was retrieved successfully and is stored in |
| 140 | + ' strComputerUUID |
| 141 | + Else |
| 142 | + ' An error occurred |
| 143 | + intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) |
| 144 | + End If |
| 145 | + End If |
| 146 | + |
| 147 | + If intFunctionReturn = 0 Then |
| 148 | + ' No error occurred |
| 149 | + intReturnMultiplier = intReturnMultiplier * 1024 |
| 150 | + intReturnCode = GetSMBIOSVersionStringUsingBIOSInstances(strSMBIOSVersion, arrBIOSInstances) |
| 151 | + If intReturnCode >= 0 Then |
| 152 | + ' The systems management BIOS version string was retrieved |
| 153 | + ' successfully and is stored in strSMBIOSVersion |
| 154 | + Else |
| 155 | + ' An error occurred |
| 156 | + intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) |
| 157 | + End If |
| 158 | + End If |
| 159 | + |
| 160 | + If intFunctionReturn = 0 Then |
| 161 | + ' No error occurred |
| 162 | + intReturnMultiplier = intReturnMultiplier * 1024 |
| 163 | + intReturnCode = TestComputerIsAmazonWebServicesEC2InstanceUsingManufacturerSMBIOSVersionAndUUID(boolInterimResult, strComputerManufacturer, strSMBIOSVersion, strComputerUUID) |
| 164 | + If intReturnCode = 0 Then |
| 165 | + ' Successfully tested whether this system is an AWS EC2 instance |
| 166 | + Else |
| 167 | + ' An error occurred |
| 168 | + intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) |
| 169 | + End If |
| 170 | + End If |
| 171 | + |
| 172 | + If intFunctionReturn = 0 Then |
| 173 | + ' No error occurred |
| 174 | + boolIsAmazonWebServicesEC2Instance = boolInterimResult |
| 175 | + End If |
| 176 | + |
| 177 | + TestComputerIsAmazonWebServicesEC2InstanceUsingComputerSystemComputerSystemProductAndBIOSInstances = intFunctionReturn |
| 178 | +End Function |
0 commit comments