-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathCreateDeviceCollections
305 lines (268 loc) · 15.9 KB
/
CreateDeviceCollections
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<#
V2020.12.04 by @gwblok
- Fix for Lenovo Models that start with "Think" instead of "Lenovo"
V2020.04.08 by @miketerrill
- Fixed collection model creation by initializing $Models as an array
- Added normalization support for Microsoft and VMware
- Simplified the CM Folder creation and added a variable for it (for Brain Mason)
- Move collections to folder if $CreateCollectionFolder is true
V2020.02.07 by merlinfrombelgium
- Fixed some typos and grammar
- Fixed WMI queries to 'select distinct' instead of just 'select'. This returned an array of ProductIDs, where only one is wanted.
- Removed @initParams from CM module import as it was never initialized.
- Added Sort-Object before Out-GridView to make list of Manufacturers and Models easier to read.
V2020.01.27
Updated Script to Create "Operational" Folder and then move newly created Collections there.
Borrowed Code from Benoit Lecours
Made script pull addtional info dynamic, you shouldn't have to make any environmental chanages for script to work.
Script now also creates an "All Workstation" Collection and uses that as the limiting collection, unless one with that name already exist, then uses that.
Change the Limiting Collection info by updating: $LimitingCollection
You can easily modify the Collection Prefix now by changing these below:
$ModelColPrefix = "ModelCol"
$ManufacturerColPreFix = "ManufacturerCol"
v2020.01.15 - Initial Release
Script "Scans" the Site Server's WMI for Device Manufacturer and Model info, then prompts for you to pick & choose what collections you'd like created.
REQUIREMENTS:
Run Script from Machine that has the Console Installed
Run with account that has rights to perform actions in ConfigMgr
HP: Hardware Inventory includes Baseboard: Product
Lenovo: Hardware Inventory includes ComputerSystemProduct: Name & Version
please follow this post to enable the additional info for Hardware Inventory: https://www.recastsoftware.com/blog/enable-product-in-win32-baseboard-for-hardware-inventory
References & Additional Credits:
Borrowed code for making Operational Folder and moving Collections from: https://gallery.technet.microsoft.com/Set-of-Operational-SCCM-19fa8178 (Benoit Lecours)
KNOWN BUGS (Unknown how to fix)
If you re-run the script after you've already run it, and you only select one model from the options, you'll get some odd output. Guessing the previous run's variables aren't cleared and causing this, but just haven't tracked it down.
DESIRED CHANGES
- Use CIM rather than WIM and PowerShell cmdlets for ConfigMgr (see https://github.com/saladproblems/CCM-Core)
- Optimize code for performance
- Create Functions and implement proper code formatting (params, description, help)
#>
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1"
}
#Get SiteCode
$SiteCode = Get-PSDrive -PSProvider CMSITE
$ProviderMachineName = (Get-PSDrive -PSProvider CMSITE).Root
Set-location $SiteCode":"
#Start Custom Script Vars
$ModelColPrefix = "" #If a prefix is required before the collection name enter it here
$ManufacturerColPreFix = "All " #If a prefix is required before the collection name enter it here
$Models = @()
$Model = $Null
$MachineDatabase = @()
$LimitingCollection = "All Workstations" #Creates this later if does not exist
$CreateManufacturersCollections = $true
$CreateModelCollections = $true
$CollectionFolderName = "Workstation Models"
$CreateCollectionFolder = $true
#Create Collection Folder
If ((-not (Test-Path -Path ($SiteCode.Name +":\DeviceCollection\$CollectionFolderName"))) -and ($CreateCollectionFolder))
{
Write-host "Device collection folder name $CollectionFolderName was not found. Creating folder..." -ForegroundColor Green
New-Item -Name $CollectionFolderName -Path ($SiteCode.Name +":\DeviceCollection")
$FolderPath = ($SiteCode.Name +":\DeviceCollection\$CollectionFolderName")
Write-host "Device collection folder $CollectionFolderName created." -ForegroundColor Green
}
elseif ((Test-Path -Path ($SiteCode.Name +":\DeviceCollection\$CollectionFolderName")) -and ($CreateCollectionFolder))
{
Write-host "Device collection folder name $CollectionFolderName already exists...will move newly created collections to this folder." -ForegroundColor Yellow
$FolderPath = ($SiteCode.Name +":\DeviceCollection\$CollectionFolderName")
}
#Set Schedule to Evaluate Weekly (from the time you run the script)
$Schedule = New-CMSchedule -Start (Get-Date).DateTime -RecurInterval Days -RecurCount 7
#Confirm All Workstation Collection, or create it if needed
$AllWorkstationCollection = Get-CMCollection -Name $LimitingCollection
if ($AllWorkstationCollection -eq $Null)
{
$CollectionQueryAllWorkstations = @"
select SMS_R_System.Name from SMS_R_System where SMS_R_System.OperatingSystemNameandVersion like "Microsoft Windows NT Workstation%"
"@
New-CMDeviceCollection -Name $LimitingCollection -Comment "Collection of all workstation machines" -LimitingCollectionName "All Systems" -RefreshSchedule $Schedule -RefreshType 2 |Out-Null
Add-CMDeviceCollectionQueryMembershipRule -RuleName "All Workstations" -CollectionName $LimitingCollection -QueryExpression $CollectionQueryAllWorkstations | Out-Null
$AllWorkstationCollection = Get-CMCollection -Name $LimitingCollection
Write-Host "Created All Workstations Collection ID: $($AllWorkstationCollection.CollectionID), which will be used as the limiting collections moving forward" -ForegroundColor Green
}
else {Write-Host "Found All Workstations Collection ID: $($AllWorkstationCollection.CollectionID), which will be used as the limiting collections moving forward" -ForegroundColor Green}
#Start Populating $MachineDatabase Array with the Models you select
#Pick from Manufacturers
$query = @"
select DISTINCT SMS_G_System_COMPUTER_SYSTEM.Manufacturer FROM SMS_G_System_COMPUTER_SYSTEM
"@
$Manufs = (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query).Manufacturer | Sort-Object Manufacturer | Out-GridView -title "Select the Manufacturers which have models you wish to create Collections for" -PassThru
#Create Model Collections Area
if ($CreateModelCollections -eq $true)
{
#Pick Models from those Manufacturers
foreach ($Manuf in $Manufs)
{
if ($Manuf -match "Len*")
{
$query = @"
select DISTINCT SMS_G_System_COMPUTER_SYSTEM_PRODUCT.Version FROM SMS_G_System_COMPUTER_SYSTEM_PRODUCT
"@
$Models += (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query | Where-Object {$_.Version -Match "Leno" -or $_.Version -Match "Think"}).Version | Sort-Object Version | Out-GridView -title "Select the $Manuf Models you wish to create Collections for" -PassThru
}
else
{
$query = @"
select DISTINCT SMS_G_System_COMPUTER_SYSTEM.Model FROM SMS_G_System_COMPUTER_SYSTEM
where SMS_G_System_COMPUTER_SYSTEM.Manufacturer = "$Manuf"
"@
$Models += (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query).Model | Sort-Object Model | Out-GridView -title "Select the $Manuf Models you wish to create Collections for" -PassThru
}
}
#Build the Database Array, going model by model
foreach ($model in $Models)
{
$ProductID = $null
#Add Manufacturer info to the Model in the Arrary
$query = @"
select DISTINCT SMS_G_System_COMPUTER_SYSTEM.Manufacturer FROM SMS_G_System_COMPUTER_SYSTEM where SMS_G_System_COMPUTER_SYSTEM.Model = "$model"
"@
if ($Model -match "Len*" -or $Model -match "Think*"){$Manufacturer = "Lenovo"}
else{$Manufacturer = (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query).Manufacturer}
#Add Product Info to the Model in the Arrary
if ($Model -match "Len*" -or $Model -match "Think*")
{
$query = @"
select SMS_G_System_COMPUTER_SYSTEM_PRODUCT.Name FROM SMS_G_System_COMPUTER_SYSTEM_PRODUCT
where SMS_G_System_COMPUTER_SYSTEM_PRODUCT.Version = "$model"
"@
$ProductID = (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query).Name
}
Else
{
$query = @"
select SMS_G_System_BASEBOARD.Product FROM SMS_G_System_BASEBOARD
LEFT JOIN SMS_G_System_COMPUTER_SYSTEM ON SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_G_System_BASEBOARD.ResourceID
where SMS_G_System_COMPUTER_SYSTEM.Model = "$model"
"@
$ProductID = (Get-WmiObject -ComputerName $ProviderMachineName -Namespace "ROOT\SMS\site_$SiteCode" -Query $query).Product | Select-Object -Last 1
}
#Take the Items and place in PS Object
$MachineDatabaseObject = New-Object PSObject -Property @{
Manufacturer = $Manufacturer
Model = $Model
ProductID = $ProductID
}
#Take the PS Object and append the Database Array
$MachineDatabase += $MachineDatabaseObject
}
#Start Building Required components to create Collections
foreach ($Machine in $MachineDatabase)
{
$ColModel = $Machine.Model
$ColProductID = $Machine.ProductID
$ColManufacturer = $Machine.Manufacturer
#Standardize Dell & HP Manufacturer Names
#Set $Collection Name to ModelCol - Manufauturer - Model (or similar variation for HP)
if ($ColManufacturer -like "De*") {$ColManufacturer = "Dell"}
if ($ColManufacturer -like "Mic*") {$ColManufacturer = "Microsoft"}
if ($ColManufacturer -like "VMw*") {$ColManufacturer = "VMware"}
if ($ColManufacturer -like "H*")
{
$ColManufacturer = "HP"
$CollectionName = "$ModelColPrefix$ColModel - $ColProductID"
$Comment = "Requires Win32_BaseBoard.Product in Inventory: https://www.recastsoftware.com/blog/enable-product-in-win32-baseboard-for-hardware-inventory"
}
elseif ($ColManufacturer -like "Leno*")
{
$CollectionName = "$ModelColPrefix$ColModel - $ColProductID"
$Comment = "Requires Win32_ComputerSystemProduct.Name & .Version in Inventory: https://www.recastsoftware.com/blog/enable-product-in-win32-baseboard-for-hardware-inventory"
}
else
{
$CollectionName = "$ModelColPrefix$ColManufacturer $ColModel"
$Comment = "No Comment"
}
#Collection Query based on Product ID (HP)
$CollectionQueryProduct = @"
select SMS_R_System.Name FROM SMS_R_System
INNER JOIN SMS_G_System_BASEBOARD ON SMS_G_System_BASEBOARD.ResourceID = SMS_R_System.ResourceID
WHERE SMS_G_System_BASEBOARD.Product = "$ColProductID"
"@
#Collection Query based on Model (Lenovo)
$CollectionQueryName = @"
select SMS_R_System.Name FROM SMS_R_System
INNER JOIN SMS_G_System_COMPUTER_SYSTEM_PRODUCT ON SMS_G_System_COMPUTER_SYSTEM_PRODUCT.ResourceID = SMS_R_System.ResourceID
WHERE SMS_G_System_COMPUTER_SYSTEM_PRODUCT.Name = "$ColProductID"
"@
#Collection Query based on Model (Dell & Etc)
$CollectionQueryModel = @"
select SMS_R_System.Name FROM SMS_R_System
INNER JOIN SMS_G_System_COMPUTER_SYSTEM ON SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceID
WHERE SMS_G_System_COMPUTER_SYSTEM.Model = "$ColModel"
"@
#Create Model Collections
$CurrentCollectionID = (Get-CMCollection -Name $CollectionName).CollectionID
if ($CurrentCollectionID -eq $null)
{
Write-Host "Creating Collection: $CollectionName" -ForegroundColor Green
New-CMDeviceCollection -Name $CollectionName -LimitingCollectionName $LimitingCollection -RefreshSchedule $Schedule -RefreshType Periodic -Comment $Comment | Out-Null
if ($ColManufacturer -like "H*")
{
Write-host "Using Baseboard $ColProductID" -ForegroundColor Green
Add-CMDeviceCollectionQueryMembershipRule -RuleName "Query $ColModel" -CollectionName $CollectionName -QueryExpression $CollectionQueryProduct | Out-Null
}
elseif ($ColManufacturer -like "Len*")
{
Write-host "Using Product $ColProductID" -ForegroundColor Green
Add-CMDeviceCollectionQueryMembershipRule -RuleName "Query $ColProductID" -CollectionName $CollectionName -QueryExpression $CollectionQueryName | Out-Null
}
else
{
Write-host "Using Model $ColModel" -ForegroundColor Green
Add-CMDeviceCollectionQueryMembershipRule -RuleName "Query $ColModel" -CollectionName "$CollectionName" -QueryExpression $CollectionQueryModel | Out-Null
}
Write-Host "New Collection Created with Name: $CollectionName & ID: $((Get-CMCollection -Name $CollectionName).CollectionID)" -ForegroundColor Green
If ($CreateCollectionFolder)
{
Move-CMObject -FolderPath $FolderPath -InputObject $(Get-CMDeviceCollection -Name $CollectionName)
Write-host *** Collection $CollectionName moved to $CollectionFolder.Name folder***
}
}
Else{Write-Host "Collection: $CollectionName already exist with ID: $CurrentCollectionID" -ForegroundColor Yellow}
Write-Host "-----" -ForegroundColor DarkGray
}
}
if ($CreateManufacturersCollections -eq $true)
{
foreach ($Manuf in $Manufs)
{
#Set Manufacturer for use in Query
$ColManufacturer = $Manuf
if ($ColManufacturer -like "H*"){$ColManufacturer = "H"}
if ($ColManufacturer -like "Dell*"){$ColManufacturer = "Dell"}
if ($ColManufacturer -like "Mic*"){$ColManufacturer = "Microsoft"}
if ($ColManufacturer -like "VMw*"){$ColManufacturer = "VMware"}
else {$ColManufacturer = $Manuf.Substring(0,$Manuf.Length-1)}
$CollectionQueryManufacturer = @"
select SMS_R_SYSTEM.Name from SMS_R_System inner join SMS_G_System_COMPUTER_SYSTEM on SMS_G_System_COMPUTER_SYSTEM.ResourceId = SMS_R_System.ResourceId where SMS_G_System_COMPUTER_SYSTEM.Manufacturer like "$($ColManufacturer)%"
"@
#Set Manufacturer for use in Collection Name
$ColManufacturer = $Manuf
if ($ColManufacturer -like "H*"){$ColManufacturer = "HP"}
if ($ColManufacturer -like "Dell*"){$ColManufacturer = "Dell"}
if ($ColManufacturer -like "LEN*"){$ColManufacturer = "Lenovo"}
if ($ColManufacturer -like "Mic*"){$ColManufacturer = "Microsoft"}
if ($ColManufacturer -like "VMw*"){$ColManufacturer = "VMware"}
#Start Creation of ManufacturerCollection
$CollectionName = "$ManufacturerColPreFix$ColManufacturer Systems"
$CurrentCollectionID = (Get-CMCollection -Name $CollectionName).CollectionID
if ($CurrentCollectionID -eq $null)
{
Write-Host "Creating Collection: $CollectionName" -ForegroundColor Green
New-CMDeviceCollection -Name $CollectionName -LimitingCollectionName $LimitingCollection -RefreshSchedule $Schedule -RefreshType Periodic | Out-Null
Add-CMDeviceCollectionQueryMembershipRule -RuleName "Query $ColManufacturer" -CollectionName "$CollectionName" -QueryExpression $CollectionQueryManufacturer | Out-Null
Write-Host "New Collection Created with Name: $CollectionName & ID: $((Get-CMCollection -Name $CollectionName).CollectionID)" -ForegroundColor Green
If ($CreateCollectionFolder)
{
Move-CMObject -FolderPath $FolderPath -InputObject $(Get-CMDeviceCollection -Name $CollectionName)
Write-host *** Collection $CollectionName moved to $CollectionFolder.Name folder***
}
}
Else{Write-Host "Collection: $CollectionName already exsit with ID: $CurrentCollectionID" -ForegroundColor Yellow}
Write-Host "-----" -ForegroundColor DarkGray
}
}