Skip to content

Commit c480638

Browse files
committed
Detect AWS EC2 Instance
1 parent dc6cfea commit c480638

File tree

4 files changed

+583
-1
lines changed

4 files changed

+583
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
' Based on the Sysadmin Accelerator version 1.6.20230422.0
1+
' Based on the Sysadmin Accelerator version 1.6.20230423.0
22
' Sysadmin Accelerator subject to embedded license terms
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
Function TestComputerIsAmazonWebServicesEC2Instance(ByRef boolIsAmazonWebServicesEC2Instance)
2+
'region FunctionMetadata #######################################################
3+
' This function determines if the computer is an Amazon Web Services (AWS) EC2
4+
' instance
5+
'
6+
' The function takes one positional argument (boolIsAmazonWebServicesEC2Instance),
7+
' which is populated upon success with a boolean value: True when the computer was
8+
' determined to be an AWS EC2 instance, False when the computer was determined to
9+
' not be an AWS EC2 instance
10+
'
11+
' The function returns a 0 when the function successfully evaluated whether the
12+
' computer is an AWS EC2 instance. The function returns a negative integer if an
13+
' error occurred.
14+
'
15+
' Note: for Citrix Xen virtual machines, this function will return a false positive
16+
' on one out of every 4096 virtual machines. This is because AWS EC2 instances are
17+
' detectable when the first three digits of their UUID are EC2, but Citrix Xen
18+
' virtual machines will have their UUID start with EC2 one out of every 4096 times.
19+
'
20+
' Example:
21+
' intReturnCode = TestComputerIsAmazonWebServicesEC2Instance(boolIsAmazonWebServicesEC2Instance)
22+
' If intReturnCode = 0 Then
23+
' ' Successfully tested whether this system is an AWS EC2 instance
24+
' If boolIsAmazonWebServicesEC2Instance = True Then
25+
' ' Computer is an AWS EC2 instance
26+
' Else
27+
' ' Computer is not an AWS EC2 instance
28+
' End If
29+
' End If
30+
'
31+
' Version: 1.0.20230423.0
32+
'endregion FunctionMetadata #######################################################
33+
34+
'region License ################################################################
35+
' Copyright 2023 Frank Lesniak
36+
'
37+
' Permission is hereby granted, free of charge, to any person obtaining a copy of
38+
' this software and associated documentation files (the "Software"), to deal in the
39+
' Software without restriction, including without limitation the rights to use,
40+
' copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
41+
' Software, and to permit persons to whom the Software is furnished to do so,
42+
' subject to the following conditions:
43+
'
44+
' The above copyright notice and this permission notice shall be included in all
45+
' copies or substantial portions of the Software.
46+
'
47+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
48+
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
49+
' FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
50+
' COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
51+
' AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
52+
' WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53+
'endregion License ################################################################
54+
55+
'region DownloadLocationNotice #################################################
56+
' The most up-to-date version of this script can be found on the author's GitHub
57+
' repository at https://github.com/franklesniak/sysadmin-accelerator
58+
'endregion DownloadLocationNotice #################################################
59+
60+
'region DependsOn ##############################################################
61+
' ConnectLocalWMINamespace()
62+
' GetComputerSystemInstancesUsingWMINamespace()
63+
' GetComputerSystemProductInstancesUsingWMINamespace()
64+
' GetBIOSInstancesUsingWMINamespace()
65+
'endregion DependsOn ##############################################################
66+
67+
Dim intFunctionReturn
68+
Dim intReturnMultiplier
69+
Dim intReturnCode
70+
Dim objSWbemServicesWMINamespace
71+
Dim arrComputerSystemInstances
72+
Dim arrComputerSystemProductInstances
73+
Dim arrBIOSInstances
74+
Dim boolInterimResult
75+
76+
intFunctionReturn = 0
77+
intReturnMultiplier = 1
78+
79+
intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
80+
If intReturnCode = 0 Then
81+
' Successfully connected to the local computer's root\CIMv2 WMI Namespace
82+
Else
83+
' An error occurred
84+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
85+
End If
86+
87+
If intFunctionReturn = 0 Then
88+
' No error occurred
89+
intReturnMultiplier = intReturnMultiplier * 16
90+
intReturnCode = GetComputerSystemInstancesUsingWMINamespace(arrComputerSystemInstances, objSWbemServicesWMINamespace)
91+
If intReturnCode >= 0 Then
92+
' At least one Win32_ComputerSystem instance was retrieved successfully
93+
Else
94+
' An error occurred
95+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
96+
End If
97+
End If
98+
99+
If intFunctionReturn = 0 Then
100+
' No error occurred
101+
intReturnMultiplier = intReturnMultiplier * 8
102+
intReturnCode = GetComputerSystemProductInstancesUsingWMINamespace(arrComputerSystemProductInstances, objSWbemServicesWMINamespace)
103+
If intReturnCode >= 0 Then
104+
' At least one Win32_ComputerSystemProduct instance was retrieved
105+
' successfully
106+
Else
107+
' An error occurred
108+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
109+
End If
110+
End If
111+
112+
If intFunctionReturn = 0 Then
113+
' No error occurred
114+
intReturnMultiplier = intReturnMultiplier * 8
115+
intReturnCode = GetBIOSInstancesUsingWMINamespace(arrBIOSInstances, objSWbemServicesWMINamespace)
116+
If intReturnCode >= 0 Then
117+
' At least one Win32_BIOS instance was retrieved successfully
118+
Else
119+
' An error occurred
120+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
121+
End If
122+
End If
123+
124+
If intFunctionReturn = 0 Then
125+
' No error occurred
126+
intReturnMultiplier = intReturnMultiplier * 8
127+
intReturnCode = TestComputerIsAmazonWebServicesEC2InstanceUsingComputerSystemComputerSystemProductAndBIOSInstances(boolInterimResult, arrComputerSystemInstances, arrComputerSystemProductInstances, arrBIOSInstances)
128+
If intReturnCode = 0 Then
129+
' Successfully tested whether this system is an AWS EC2 instance
130+
Else
131+
' An error occurred
132+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
133+
End If
134+
End If
135+
136+
If intFunctionReturn = 0 Then
137+
' No error occurred
138+
boolIsAmazonWebServicesEC2Instance = boolInterimResult
139+
End If
140+
141+
TestComputerIsAmazonWebServicesEC2Instance = intFunctionReturn
142+
End Function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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

Comments
 (0)