Skip to content

Commit 62464ab

Browse files
committed
Gather serial number
1 parent 089f3e8 commit 62464ab

File tree

4 files changed

+427
-0
lines changed

4 files changed

+427
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Function GetBIOSInstances(ByRef arrBIOSInstances)
2+
'region FunctionMetadata ####################################################
3+
' This function retrieves the Win32_BIOS instances and stores them in arrBIOSInstances.
4+
'
5+
' The function takes one positional argument (arrBIOSInstances), which is populated upon
6+
' success with the BIOS instances returned from WMI of type Win32_BIOS
7+
'
8+
' The function returns 0 if Win32_BIOS instances were retrieved successfully, and
9+
' there was one computer system instance (as expected). If no Win32_BIOS objects
10+
' could be retrieved, then the function returns a negative number. If there are
11+
' unexpectedly multiple instances of Win32_BIOS, then the function returns a
12+
' positive number equal to the number of WMI instances retrieved minus one.
13+
'
14+
' Example:
15+
' intReturnCode = GetBIOSInstances(arrBIOSInstances)
16+
' If intReturnCode = 0 Then
17+
' ' The Win32_BIOS instance was retrieved successfully and is available
18+
' ' at arrBIOSInstances.ItemIndex(0)
19+
' ElseIf intReturnCode > 0 Then
20+
' ' More than one Win32_BIOS instance was retrieved, which is
21+
' ' unexpected.
22+
' Else
23+
' ' An error occurred and no Win32_BIOS instances were retrieved
24+
' End If
25+
'
26+
' Version: 1.0.20210624.0
27+
'endregion FunctionMetadata ####################################################
28+
29+
'region License ####################################################
30+
' Copyright 2021 Frank Lesniak
31+
'
32+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
33+
' software and associated documentation files (the "Software"), to deal in the Software
34+
' without restriction, including without limitation the rights to use, copy, modify, merge,
35+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
36+
' persons to whom the Software is furnished to do so, subject to the following conditions:
37+
'
38+
' The above copyright notice and this permission notice shall be included in all copies or
39+
' substantial portions of the Software.
40+
'
41+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
42+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
43+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
44+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
45+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
46+
' DEALINGS IN THE SOFTWARE.
47+
'endregion License ####################################################
48+
49+
'region DownloadLocationNotice ####################################################
50+
' The most up-to-date version of this script can be found on the author's GitHub repository
51+
' at https://github.com/franklesniak/sysadmin-accelerator
52+
'endregion DownloadLocationNotice ####################################################
53+
54+
'region Acknowledgements ####################################################
55+
' None!
56+
'endregion Acknowledgements ####################################################
57+
58+
'region DependsOn ####################################################
59+
' ConnectLocalWMINamespace()
60+
' GetBIOSInstancesUsingWMINamespace()
61+
'endregion DependsOn ####################################################
62+
63+
Dim intFunctionReturn
64+
Dim intReturnMultiplier
65+
Dim intReturnCode
66+
Dim objSWbemServicesWMINamespace
67+
68+
intFunctionReturn = 0
69+
intReturnMultiplier = 8
70+
71+
intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
72+
If intReturnCode <> 0 Then
73+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
74+
Else
75+
intReturnCode = GetBIOSInstancesUsingWMINamespace(arrBIOSInstances, objSWbemServicesWMINamespace)
76+
intReturnMultiplier = 1
77+
intFunctionReturn = intFunctionReturn + (intReturnCode * intReturnMultiplier)
78+
End If
79+
80+
GetBIOSInstances = intFunctionReturn
81+
End Function
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
Function GetBIOSInstancesUsingWMINamespace(ByRef arrBIOSInstances, 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_BIOS instances and stores them
5+
' in arrBIOSInstances.
6+
'
7+
' The function takes two positional arguments:
8+
' - The first argument (arrBIOSInstances) is populated upon success with the BIOS
9+
' instances returned from WMI of type Win32_BIOS
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+
' The function returns 0 if Win32_BIOS instances were retrieved successfully, and there was
14+
' one BIOS instance (as expected). If no Win32_BIOS objects could be retrieved, then the
15+
' function returns a negative number. If there are unexpectedly multiple instances of
16+
' Win32_BIOS, then the function returns a positive number equal to the number of WMI
17+
' instances retrieved minus one.
18+
'
19+
' Example:
20+
' intReturnCode = ConnectLocalWMINamespace(objSWbemServicesWMINamespace, Null, Null)
21+
' If intReturnCode = 0 Then
22+
' ' Successfully connected to the local computer's root\CIMv2 WMI Namespace
23+
' intReturnCode = GetBIOSInstancesUsingWMINamespace(arrBIOSInstances, objSWbemServicesWMINamespace)
24+
' If intReturnCode = 0 Then
25+
' ' The Win32_BIOS instance was retrieved successfully and is available
26+
' ' at arrBIOSInstances.ItemIndex(0)
27+
' ElseIf intReturnCode > 0 Then
28+
' ' More than one Win32_BIOS instance was retrieved, which is
29+
' ' unexpected.
30+
' Else
31+
' ' An error occurred and no Win32_BIOS instances were retrieved
32+
' End If
33+
' End If
34+
'
35+
' Version: 1.0.20210624.0
36+
'endregion FunctionMetadata ####################################################
37+
38+
'region License ####################################################
39+
' Copyright 2021 Frank Lesniak
40+
'
41+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
42+
' software and associated documentation files (the "Software"), to deal in the Software
43+
' without restriction, including without limitation the rights to use, copy, modify, merge,
44+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
45+
' persons to whom the Software is furnished to do so, subject to the following conditions:
46+
'
47+
' The above copyright notice and this permission notice shall be included in all copies or
48+
' substantial portions of the Software.
49+
'
50+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
51+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
52+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
53+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
54+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
55+
' DEALINGS IN THE SOFTWARE.
56+
'endregion License ####################################################
57+
58+
'region DownloadLocationNotice ####################################################
59+
' The most up-to-date version of this script can be found on the author's GitHub repository
60+
' at https://github.com/franklesniak/sysadmin-accelerator
61+
'endregion DownloadLocationNotice ####################################################
62+
63+
'region Acknowledgements ####################################################
64+
' None!
65+
'endregion Acknowledgements ####################################################
66+
67+
'region DependsOn ####################################################
68+
' TestObjectForData()
69+
' TestObjectIsAnyTypeOfInteger()
70+
'endregion DependsOn ####################################################
71+
72+
Dim intFunctionReturn
73+
Dim intReturnMultiplier
74+
Dim arrWorkingBIOSInstances
75+
Dim intTemp
76+
77+
Err.Clear
78+
79+
intFunctionReturn = 0
80+
intReturnMultiplier = 1
81+
82+
If TestObjectForData(objWMINamespace) <> True Then
83+
intFunctionReturn = intFunctionReturn + (-2 * intReturnMultiplier)
84+
Else
85+
On Error Resume Next
86+
Set arrWorkingBIOSInstances = objWMINamespace.InstancesOf("Win32_BIOS")
87+
If Err Then
88+
On Error Goto 0
89+
Err.Clear
90+
intFunctionReturn = intFunctionReturn + (-3 * intReturnMultiplier)
91+
Else
92+
intTemp = arrWorkingBIOSInstances.Count
93+
If Err Then
94+
On Error Goto 0
95+
Err.Clear
96+
intFunctionReturn = intFunctionReturn + (-4 * intReturnMultiplier)
97+
Else
98+
On Error Goto 0
99+
If TestObjectIsAnyTypeOfInteger(intTemp) = False Then
100+
intFunctionReturn = intFunctionReturn + (-5 * intReturnMultiplier)
101+
Else
102+
If intTemp < 0 Then
103+
intFunctionReturn = intFunctionReturn + (-6 * intReturnMultiplier)
104+
Else
105+
' intTemp >= 0
106+
intFunctionReturn = intTemp - 1
107+
' -1 would be returned if there are no Win32_BIOS instances
108+
End If
109+
End If
110+
End If
111+
End If
112+
End If
113+
114+
If intFunctionReturn >= 0 Then
115+
On Error Resume Next
116+
Set arrBIOSInstances = objWMINamespace.InstancesOf("Win32_BIOS")
117+
If Err Then
118+
On Error Goto 0
119+
Err.Clear
120+
intFunctionReturn = (-7 * intReturnMultiplier)
121+
Else
122+
On Error Goto 0
123+
End If
124+
End If
125+
126+
GetBIOSInstancesUsingWMINamespace = intFunctionReturn
127+
End Function
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Function GetSerialNumber(ByRef strSerialNumber)
2+
'region FunctionMetadata ####################################################
3+
' This function obtains the computer's serial number
4+
'
5+
' The function takes one positional argument (strSerialNumber), which is populated upon
6+
' success with a string containing the computer's serial number as reported by WMI.
7+
'
8+
' The function returns a 0 if the serial number was obtained successfully. It returns a
9+
' negative integer if an error occurred retrieving the serial number. Finally, it returns
10+
' a positive integer if the serial number was obtained, but multiple BIOS instances were
11+
' present that contained data for the serial number. When this happens, only the first
12+
' Win32_BIOS instance containing data for the serial number is used.
13+
'
14+
' Example:
15+
' intReturnCode = GetSerialNumber(strSerialNumber)
16+
' If intReturnCode >= 0 Then
17+
' ' The computer serial number was retrieved successfully and is stored in
18+
' ' strSerialNumber
19+
' End If
20+
'
21+
' Version: 1.0.20210624.0
22+
'endregion FunctionMetadata ####################################################
23+
24+
'region License ####################################################
25+
' Copyright 2021 Frank Lesniak
26+
'
27+
' Permission is hereby granted, free of charge, to any person obtaining a copy of this
28+
' software and associated documentation files (the "Software"), to deal in the Software
29+
' without restriction, including without limitation the rights to use, copy, modify, merge,
30+
' publish, distribute, sublicense, and/or sell copies of the Software, and to permit
31+
' persons to whom the Software is furnished to do so, subject to the following conditions:
32+
'
33+
' The above copyright notice and this permission notice shall be included in all copies or
34+
' substantial portions of the Software.
35+
'
36+
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
37+
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
38+
' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
39+
' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
40+
' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
41+
' DEALINGS IN THE SOFTWARE.
42+
'endregion License ####################################################
43+
44+
'region DownloadLocationNotice ####################################################
45+
' The most up-to-date version of this script can be found on the author's GitHub repository
46+
' at https://github.com/franklesniak/sysadmin-accelerator
47+
'endregion DownloadLocationNotice ####################################################
48+
49+
'region Acknowledgements ####################################################
50+
' None!
51+
'endregion Acknowledgements ####################################################
52+
53+
'region DependsOn ####################################################
54+
' GetBIOSInstances()
55+
' GetSerialNumberUsingBIOSInstances()
56+
'endregion DependsOn ####################################################
57+
58+
Dim intFunctionReturn
59+
Dim arrBIOSInstances
60+
Dim strResult
61+
62+
intFunctionReturn = 0
63+
64+
intFunctionReturn = GetBIOSInstances(arrBIOSInstances)
65+
If intFunctionReturn >= 0 Then
66+
' At least one Win32_BIOS instance was retrieved successfully
67+
intFunctionReturn = GetSerialNumberUsingBIOSInstances(strResult, arrBIOSInstances)
68+
If intFunctionReturn >= 0 Then
69+
' The computer serial number was retrieved successfully and is stored in strResult
70+
strSerialNumber = strResult
71+
End If
72+
End If
73+
74+
GetSerialNumber = intFunctionReturn
75+
End Function

0 commit comments

Comments
 (0)