Skip to content

Commit 71f1818

Browse files
committed
Desktop/laptop/server chassis detection
1 parent 14a2c11 commit 71f1818

9 files changed

+1499
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
Function TestComputerIsPhysicalServerChassis(ByRef boolIsPhysicalServerChassis)
2+
'region FunctionMetadata ####################################################
3+
' This function determines if the computer is a physical server chassis (i.e., a rackmount
4+
' chassis or blade chassis)
5+
'
6+
' The function takes one positional argument (boolIsPhysicalServerChassis), which is
7+
' populated upon success with a boolean value: True when the computer was determined to be
8+
' a physical server chassis (i.e., a rackmount or blade chassis), False otherwise
9+
'
10+
' The function returns a 0 when the function successfully evaluated whether the computer is
11+
' a physical server chassis (i.e., a rackmount or server chassis). The function returns a
12+
' negative integer if an error occurred.
13+
'
14+
' Example:
15+
' intReturnCode = TestComputerIsPhysicalServerChassis(boolIsPhysicalServerChassis)
16+
' If intReturnCode = 0 Then
17+
' ' Successfully tested whether this system is a physical server chassis
18+
' If boolIsPhysicalServerChassis = True Then
19+
' ' Computer is a physical server chassis (i.e., rackmount chassis or blade
20+
' ' chassis)
21+
' Else
22+
' ' Computer is not a physical server chassis, i.e., it is a stationary,
23+
' ' non-server physical computer (e.g., a desktop), a physical portable computer
24+
' ' (e.g., laptop or tablet), or it is a virtual machine
25+
' End If
26+
' End If
27+
'
28+
' Version: 1.0.20210629.0
29+
'endregion FunctionMetadata ####################################################
30+
31+
'region License ####################################################
32+
' Copyright 2021 Frank Lesniak
33+
'
34+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
35+
' software and associated documentation files (the "Software"), to deal in the Software
36+
' without restriction, including without limitation the rights to use, copy, modify, merge,
37+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
38+
' persons to whom the Software is furnished to do so, subject to the following conditions:
39+
'
40+
' The above copyright notice and this permission notice shall be included in all copies or
41+
' substantial portions of the Software.
42+
'
43+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
44+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
45+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
46+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
47+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48+
' DEALINGS IN THE SOFTWARE.
49+
'endregion License ####################################################
50+
51+
'region DownloadLocationNotice ####################################################
52+
' The most up-to-date version of this script can be found on the author's GitHub repository
53+
' at https://github.com/franklesniak/sysadmin-accelerator
54+
'endregion DownloadLocationNotice ####################################################
55+
56+
'region Acknowledgements ####################################################
57+
' None!
58+
'endregion Acknowledgements ####################################################
59+
60+
'region DependsOn ####################################################
61+
' ConnectLocalWMINamespace()
62+
' GetComputerSystemInstancesUsingWMINamespace()
63+
' GetSystemEnclosureInstancesUsingWMINamespace()
64+
' TestComputerIsPhysicalServerChassisUsingComputerSystemAndSystemEnclosureInstances()
65+
'endregion DependsOn ####################################################
66+
67+
Dim intFunctionReturn
68+
Dim intReturnMultiplier
69+
Dim intOffset
70+
Dim boolInterimResult
71+
Dim intReturnCode
72+
Dim objSWbemServicesWMINamespace
73+
Dim arrComputerSystemInstances
74+
Dim arrSystemEnclosureInstances
75+
76+
intFunctionReturn = 0
77+
intReturnMultiplier = 1
78+
intOffset = 1073741824
79+
80+
boolInterimResult = False
81+
82+
intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
83+
If intReturnCode <> 0 Then
84+
' Error occurred
85+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
86+
Else
87+
' intReturnCode = 0
88+
intReturnMultiplier = intReturnMultiplier * 16
89+
' Successfully connected to the local computer's root\CIMv2 WMI Namespace
90+
intReturnCode = GetComputerSystemInstancesUsingWMINamespace(arrComputerSystemInstances, objSWbemServicesWMINamespace)
91+
If intReturnCode < 0 Then
92+
' Error occurred
93+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
94+
Else
95+
' intReturnCode >= 0
96+
intReturnMultiplier = intReturnMultiplier * 8
97+
' At least one Win32_ComputerSystem instance was retrieved successfully
98+
intReturnCode = GetSystemEnclosureInstancesUsingWMINamespace(arrSystemEnclosureInstances, objSWbemServicesWMINamespace)
99+
If intReturnCode < 0 Then
100+
' Error occurred
101+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
102+
Else
103+
' intReturnCode >= 0
104+
intReturnMultiplier = 1
105+
intOffset = 0
106+
' One or more Win32_SystemEnclosure instances were retrieved. The first
107+
' instance is available at arrSystemEnclosureInstances.ItemIndex(0) and the
108+
' number of instances is available at arrSystemEnclosureInstances.Count. In
109+
' other words, the upper array boundary/index is
110+
' (arrSystemEnclosureInstances.Count - 1).
111+
intReturnCode = TestComputerIsPhysicalServerChassisUsingComputerSystemAndSystemEnclosureInstances(boolInterimResult, arrComputerSystemInstances, arrSystemEnclosureInstances)
112+
If intReturnCode <> 0 Then
113+
' Error occurred
114+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
115+
Else
116+
' intReturnCode = 0
117+
' Successfully tested whether this system is a stationary, non-server
118+
' physical computer
119+
End If
120+
End If
121+
End If
122+
End If
123+
124+
If intFunctionReturn = 0 Then
125+
boolIsPhysicalServerChassis = boolInterimResult
126+
End If
127+
128+
TestComputerIsPhysicalServerChassis = intFunctionReturn
129+
End Function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
Function TestComputerIsPhysicalServerChassisUsingComputerSystemAndSystemEnclosureInstances(ByRef boolIsPhysicalServerChassis, ByVal arrComputerSystemInstances, ByVal arrSystemEnclosureInstances)
2+
'region FunctionMetadata ####################################################
3+
' Assuming that arrComputerSystemInstances represents an array / collection of the
4+
' available computer system instances (of type Win32_ComputerSystem) and
5+
' arrSystemEnclosureInstances is an array/collection of Win32_SystemEnclosure objects, this
6+
' function determines if the computer is a physical server chassis (i.e., rackmount chassis
7+
' or blade chassis)
8+
'
9+
' The function takes three positional arguments:
10+
' - The first argument (boolIsPhysicalServerChassis) is populated upon success with a
11+
' boolean value: True when the computer was determined to be a physical server chassis
12+
' (i.e., a rackmount chassis or blade chassis), False otherwise
13+
' - The second argument (arrComputerSystemInstances) is a WMI collection/array that must
14+
' be pre-populated with a collection of Win32_ComputerSystem objects
15+
' - The third argument (arrSystemEnclosureInstances) is a WMI collection/array that must
16+
' be pre-populated with a collection of Win32_SystemEnclosure objects
17+
'
18+
' The function returns a 0 when the function successfully evaluated whether the computer is
19+
' a physical server chassis (i.e., a rackmount chassis or blade chassis). The function
20+
' returns a negative integer if an error occurred.
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 = GetComputerSystemInstancesUsingWMINamespace(arrComputerSystemInstances, objSWbemServicesWMINamespace)
27+
' If intReturnCode >= 0 Then
28+
' ' At least one Win32_ComputerSystem instance was retrieved successfully
29+
' intReturnCode = GetSystemEnclosureInstancesUsingWMINamespace(arrSystemEnclosureInstances, objSWbemServicesWMINamespace)
30+
' If intReturnCode > 0 Then
31+
' ' One or more Win32_SystemEnclosure instances were retrieved. The first
32+
' ' instance is available at arrSystemEnclosureInstances.ItemIndex(0) and the
33+
' ' number of instances is available at arrSystemEnclosureInstances.Count. In
34+
' ' other words, the upper array boundary/index is
35+
' ' (arrSystemEnclosureInstances.Count - 1).
36+
' intReturnCode = TestComputerIsPhysicalServerChassisUsingComputerSystemAndSystemEnclosureInstances(boolIsPhysicalServerChassis, arrComputerSystemInstances, arrSystemEnclosureInstances)
37+
' If intReturnCode = 0 Then
38+
' ' Successfully tested whether this system is a physical server chassis
39+
' If boolIsPhysicalServerChassis = True Then
40+
' ' Computer is a physical server chassis (i.e., rackmount chassis or
41+
' ' blade chassis)
42+
' Else
43+
' ' Computer is not a physical server chassis, i.e., it is a
44+
' ' stationary, non-server physical computer (e.g., a desktop), a
45+
' ' physical portable computer (e.g., laptop or tablet), or it is a
46+
' ' virtual machine
47+
' End If
48+
' End If
49+
' End If
50+
' End If
51+
' End If
52+
'
53+
' Version: 1.0.20210629.0
54+
'endregion FunctionMetadata ####################################################
55+
56+
'region License ####################################################
57+
' Copyright 2021 Frank Lesniak
58+
'
59+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
60+
' software and associated documentation files (the "Software"), to deal in the Software
61+
' without restriction, including without limitation the rights to use, copy, modify, merge,
62+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
63+
' persons to whom the Software is furnished to do so, subject to the following conditions:
64+
'
65+
' The above copyright notice and this permission notice shall be included in all copies or
66+
' substantial portions of the Software.
67+
'
68+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
69+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
70+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
71+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
72+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
73+
' DEALINGS IN THE SOFTWARE.
74+
'endregion License ####################################################
75+
76+
'region DownloadLocationNotice ####################################################
77+
' The most up-to-date version of this script can be found on the author's GitHub repository
78+
' at https://github.com/franklesniak/sysadmin-accelerator
79+
'endregion DownloadLocationNotice ####################################################
80+
81+
'region Acknowledgements ####################################################
82+
' None!
83+
'endregion Acknowledgements ####################################################
84+
85+
'region DependsOn ####################################################
86+
' TestObjectForData()
87+
' GetComputerManufacturerUsingComputerSystemInstances()
88+
' GetComputerModelUsingComputerSystemInstances()
89+
' TestComputerIsPhysicalServerChassisUsingManufacturerModelAndSystemEnclosureInstances()
90+
'endregion DependsOn ####################################################
91+
92+
Dim intFunctionReturn
93+
Dim intReturnMultiplier
94+
Dim intReturnCode
95+
Dim intOffset
96+
Dim boolInterimResult
97+
Dim strComputerManufacturer
98+
Dim strComputerModel
99+
100+
intFunctionReturn = 0
101+
intReturnMultiplier = 131072
102+
intOffset = 0
103+
104+
boolInterimResult = False
105+
106+
If TestObjectForData(arrComputerSystemInstances) <> True Then
107+
intFunctionReturn = intFunctionReturn + (-1 * intReturnMultiplier) + intOffset
108+
Else
109+
intFunctionReturn = intFunctionReturn * 2
110+
intReturnCode = GetComputerManufacturerUsingComputerSystemInstances(strComputerManufacturer, arrComputerSystemInstances)
111+
If intReturnCode < 0 Then
112+
' Error occurred
113+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
114+
Else
115+
' intReturnCode >= 0
116+
' The computer manufacturer was retrieved successfully and is stored in
117+
' strComputerManufacturer
118+
intOffset = 536870912
119+
intReturnCode = GetComputerModelUsingComputerSystemInstances(strComputerModel, arrComputerSystemInstances)
120+
If intReturnCode < 0 Then
121+
' Error occurred
122+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier) + intOffset
123+
Else
124+
intReturnMultiplier = 1
125+
intOffset = 0
126+
intReturnCode = TestComputerIsPhysicalServerChassisUsingManufacturerModelAndSystemEnclosureInstances(boolInterimResult, strComputerManufacturer, strComputerModel, arrSystemEnclosureInstances)
127+
If intReturnCode <> 0 Then
128+
' Error occurred
129+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
130+
Else
131+
'Success
132+
End If
133+
End If
134+
End If
135+
End If
136+
137+
If intFunctionReturn = 0 Then
138+
boolIsPhysicalServerChassis = boolInterimResult
139+
End If
140+
141+
TestComputerIsPhysicalServerChassisUsingComputerSystemAndSystemEnclosureInstances = intFunctionReturn
142+
End Function

0 commit comments

Comments
 (0)