Skip to content

Commit 8230cf0

Browse files
committed
Initial functions for VM and chassis detection
1 parent 1be922f commit 8230cf0

10 files changed

+1104
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
Function GetSystemEnclosureInstances(ByRef arrSystemEnclosureInstances)
2+
'region FunctionMetadata ####################################################
3+
' This function retrieves the Win32_SystemEnclosure instances and stores them in
4+
' arrSystemEnclosureInstances.
5+
'
6+
' The function takes one positional argument (arrSystemEnclosureInstances), which is
7+
' populated upon success with the system enclosure instances returned from WMI of type
8+
' Win32_SystemEnclosure
9+
'
10+
' If Win32_SystemEnclosure instances were retrieved successfully, then the function returns
11+
' a positive integer equal to the number of Win32_SystemEnclosure instances retrieved.
12+
' Usually there is one Win32_SystemEnclosure, and this function therefore returns 1.
13+
' However, in some circumstances (e.g., a docking station attached to a laptop/tablet),
14+
' more than one Win32_SystemEnclosure instance is present. In these circumstances, the
15+
' function would return 2, 3, etc. If an error occurs, the function returns a negative
16+
' integer. If no error occurred but no instances could be retrieved, then the function
17+
' returns zero.
18+
'
19+
' Example:
20+
' intReturnCode = GetSystemEnclosureInstances(arrSystemEnclosureInstances)
21+
' If intReturnCode > 0 Then
22+
' ' One or more Win32_SystemEnclosure instances were retrieved. The first
23+
' ' instance is available at arrSystemEnclosureInstances.ItemIndex(0) and the
24+
' ' number of instances is available at arrSystemEnclosureInstances.Count. In
25+
' ' other words, the upper array boundary/index is
26+
' ' (arrSystemEnclosureInstances.Count - 1).
27+
' Else
28+
' ' No Win32_SystemEnclosure instances were retrieved
29+
' End If
30+
'
31+
' Version: 1.0.20210624.0
32+
'endregion FunctionMetadata ####################################################
33+
34+
'region License ####################################################
35+
' Copyright 2021 Frank Lesniak
36+
'
37+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
38+
' software and associated documentation files (the "Software"), to deal in the Software
39+
' without restriction, including without limitation the rights to use, copy, modify, merge,
40+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
41+
' persons to whom the Software is furnished to do so, subject to the following conditions:
42+
'
43+
' The above copyright notice and this permission notice shall be included in all copies or
44+
' substantial portions of the Software.
45+
'
46+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
47+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
48+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
49+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
50+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51+
' DEALINGS IN THE SOFTWARE.
52+
'endregion License ####################################################
53+
54+
'region DownloadLocationNotice ####################################################
55+
' The most up-to-date version of this script can be found on the author's GitHub repository
56+
' at https://github.com/franklesniak/sysadmin-accelerator
57+
'endregion DownloadLocationNotice ####################################################
58+
59+
'region Acknowledgements ####################################################
60+
' None!
61+
'endregion Acknowledgements ####################################################
62+
63+
'region DependsOn ####################################################
64+
' ConnectLocalWMINamespace()
65+
' GetSystemEnclosureInstancesUsingWMINamespace()
66+
'endregion DependsOn ####################################################
67+
68+
Dim intFunctionReturn
69+
Dim intReturnMultiplier
70+
Dim intReturnCode
71+
Dim objSWbemServicesWMINamespace
72+
73+
intFunctionReturn = 0
74+
intReturnMultiplier = 8
75+
76+
intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
77+
If intReturnCode <> 0 Then
78+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
79+
Else
80+
intReturnCode = GetSystemEnclosureInstancesUsingWMINamespace(arrSystemEnclosureInstances, objSWbemServicesWMINamespace)
81+
intReturnMultiplier = 1
82+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
83+
End If
84+
85+
GetSystemEnclosureInstances = intFunctionReturn
86+
End Function
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Function TestComputerIsVirtualMachine(ByRef boolIsVM)
2+
'region FunctionMetadata ####################################################
3+
' This function determines if the computer is a virtual machine
4+
'
5+
' The function takes one positional arguments (boolIsVM), which is populated upon success
6+
' with a boolean value: True when the computer was determined to be a virtual machine,
7+
' False when the computer was determined to not be a virtual machine
8+
'
9+
' The function returns a 0 when the function successfully evaluated whether the computer is
10+
' a virtual machine. The function returns a negative integer if an error occurred.
11+
'
12+
' Example:
13+
' intReturnCode = TestComputerIsVirtualMachine(boolIsVM)
14+
' If intReturnCode = 0 Then
15+
' ' Successfully tested whether this system is a VM
16+
' If boolIsVM = True Then
17+
' ' Computer is a VM
18+
' Else
19+
' ' Computer is not a VM
20+
' End If
21+
' End If
22+
'
23+
' Version: 1.0.20210625.0
24+
'endregion FunctionMetadata ####################################################
25+
26+
'region License ####################################################
27+
' Copyright 2021 Frank Lesniak
28+
'
29+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
30+
' software and associated documentation files (the "Software"), to deal in the Software
31+
' without restriction, including without limitation the rights to use, copy, modify, merge,
32+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
33+
' persons to whom the Software is furnished to do so, subject to the following conditions:
34+
'
35+
' The above copyright notice and this permission notice shall be included in all copies or
36+
' substantial portions of the Software.
37+
'
38+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
39+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
40+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
41+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
42+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
43+
' DEALINGS IN THE SOFTWARE.
44+
'endregion License ####################################################
45+
46+
'region DownloadLocationNotice ####################################################
47+
' The most up-to-date version of this script can be found on the author's GitHub repository
48+
' at https://github.com/franklesniak/sysadmin-accelerator
49+
'endregion DownloadLocationNotice ####################################################
50+
51+
'region Acknowledgements ####################################################
52+
' None!
53+
'endregion Acknowledgements ####################################################
54+
55+
'region DependsOn ####################################################
56+
' GetComputerSystemInstances()
57+
' TestComputerIsVirtualMachineUsingComputerSystemInstances()
58+
'endregion DependsOn ####################################################
59+
60+
Dim intFunctionReturn
61+
Dim intReturnMultiplier
62+
Dim intReturnCode
63+
Dim boolInterimResult
64+
Dim arrComputerSystemInstances
65+
66+
intFunctionReturn = 0
67+
intReturnMultiplier = 1
68+
69+
intReturnCode = GetComputerSystemInstances(arrComputerSystemInstances)
70+
If intReturnCode < 0 Then
71+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
72+
Else
73+
' intReturnCode >= 0
74+
' At least one Win32_ComputerSystem instance was retrieved successfully
75+
intReturnCode = TestComputerIsVirtualMachineUsingComputerSystemInstances(boolInterimResult, arrComputerSystemInstances)
76+
If intReturnCode <> 0 Then
77+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
78+
Else
79+
' intReturnCode = 0
80+
' Successfully tested whether this system is a VM
81+
End If
82+
End If
83+
84+
If intFunctionReturn = 0 Then
85+
boolIsVM = boolInterimResult
86+
End If
87+
88+
TestComputerIsVirtualMachine = intFunctionReturn
89+
End Function

0 commit comments

Comments
 (0)