This repository has been archived by the owner on Mar 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Functions.psm1
673 lines (571 loc) · 27.7 KB
/
Functions.psm1
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
function Get-ApplicationFolder
{
<#
.DESCRIPTION
Gets the folder path that an application object lives in.
We have to do this via WMI because there is no CM Powershell
Library function to look this up.
.PARAMETER CMSite
CM site code
.PARAMETER CMServer
CM site server
.PARAMETER App
Application object
.OUTPUTS
Returns a string with application folder location
.EXAMPLE
Get-ApplicationFolder 'ABC' 'SITESERVER.fqdn.com' $AppObject
#>
param (
[Parameter(Mandatory=$true)]
[string]$CMSite,
[Parameter(Mandatory=$true)]
[string]$CMServer,
[Parameter(Mandatory=$true)]
$App
)
#$AppCI_UniqueID = $App.CI_UniqueID.Substring(0,$App.CI_UniqueID.Length-($App.CIVersion.Length+1))
$temp = $App.CI_UniqueID.split("/")
$AppCI_UniqueID = $temp[0] + "/" + $temp[1]
$Query = "SELECT ContainerNodeID FROM SMS_ObjectContainerItem WHERE InstanceKey = '$AppCI_UniqueID'"
$ContainerItem = Get-WmiObject -Namespace root/SMS/site_$($CMSite) -ComputerName $CMServer -Query $Query
$ContainerNodeID = $ContainerItem.ContainerNodeID
If ($ContainerNodeID)
{
While ($ContainerNodeID -ne '0')
{
$Query = "SELECT Name,ParentContainerNodeID FROM SMS_ObjectContainerNode WHERE ContainerNodeID = '$ContainerNodeID' AND SearchFolder = 0"
$Container = Get-WmiObject -Namespace root/SMS/site_$($CMSite) -ComputerName $CMServer -Query $Query
$ContainerPath = $Container.Name + "\" + $ContainerPath
$ContainerNodeID = $Container.ParentContainerNodeID
}
}
Else
{
$ContainerPath = "\"
}
Return $ContainerPath
}
function Get-CollectionFolder
{
<#
.DESCRIPTION
Gets the folder path that an collection object lives in.
We have to do this via WMI because there is no CM Powershell
Library function to look this up.
.PARAMETER CMSite
CM site code
.PARAMETER CMServer
CM site server
.PARAMETER App
Application object
.OUTPUTS
Returns a string with collection folder location
.EXAMPLE
Get-CollectionFolder 'ABC' 'SITESERVER.fqdn.com' $CollectionObject
#>
param (
[Parameter(Mandatory=$true)]
[string]$CMSite,
[Parameter(Mandatory=$true)]
[string]$CMServer,
[Parameter(Mandatory=$true)]
$Collection
)
$CollectionID = $Collection.CollectionID
$Query = "SELECT ContainerNodeID FROM SMS_ObjectContainerItem WHERE InstanceKey = '$CollectionID'"
$ContainerItem = Get-WmiObject -Namespace root/SMS/site_$($CMSite) -ComputerName $CMServer -Query $Query
$ContainerNodeID = $ContainerItem.ContainerNodeID
If ($ContainerNodeID)
{
While ($ContainerNodeID -ne '0')
{
$Query = "SELECT Name,ParentContainerNodeID FROM SMS_ObjectContainerNode WHERE ContainerNodeID = '$ContainerNodeID' AND SearchFolder = 0"
$Container = Get-WmiObject -Namespace root/SMS/site_$($CMSite) -ComputerName $CMServer -Query $Query
$ContainerPath = $Container.Name + "\" + $ContainerPath
$ContainerNodeID = $Container.ParentContainerNodeID
}
}
Else
{
$ContainerPath = "\"
}
Return $ContainerPath
}
function Remove-AllDeployments
{
<#
.DESCRIPTION
Removes the app from all collections. We pass in a list of
collections to search through, so this can be limited to a
set number of collections.
.PARAMETER App
CM site code
.PARAMETER DeviceCollections
CM site server
.PARAMETER UserCollections
Application object
.OUTPUTS
Nothing
.EXAMPLE
Remove-AllDeployments $AppObject $DeviceCollectionObjects $UserCollectionObjects
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$DeviceCollections,
[Parameter(Mandatory=$true)]
$UserCollections
)
$SoftwareLibraryPrefix = 'Software Library - '
$SxS_NamePrefix = '[SxS '
$AppNameStringSearch = '*'+$App.LocalizedDisplayName+'*'
ForEach ($Collection in $DeviceCollections)
{
#Remove direct app deployment and software library deployment
If (($Collection.Name -eq $App.LocalizedDisplayName) -or $Collection.Name.StartsWith($SoftwareLibraryPrefix))
{
LogIt -message ("Removing deployment from device collection: " + $Collection.Name) -component "Remove-AllDeployments()" -type "Verbose" -LogFile $LogFile
Remove-CMDeployment -ApplicationName $App.LocalizedDisplayName -CollectionName $Collection.Name -Force
}
#Remove SxS collection deployments
If ($Collection.Name.StartsWith($SxS_NamePrefix) -and ($Collection.Name -like $AppNameStringSearch))
{
LogIt -message ("Removing deployment from device collection: " + $Collection.Name) -component "Remove-AllDeployments()" -type "Verbose" -LogFile $LogFile
Remove-CMDeployment -ApplicationName $App.LocalizedDisplayName -CollectionName $Collection.Name -Force
}
}
ForEach ($Collection in $UserCollections)
{
#Remove direct app deployment and software library deployment
If (($Collection.Name -eq $App.LocalizedDisplayName) -or $Collection.Name.StartsWith($SoftwareLibraryPrefix))
{
LogIt -message ("Removing deployment from user collection: " + $Collection.Name) -component "Remove-AllDeployments()" -type "Verbose" -LogFile $LogFile
Remove-CMDeployment -ApplicationName $App.LocalizedDisplayName -CollectionName $Collection.Name -Force
}
#Remove SxS collection deployments
If ($Collection.Name.StartsWith($SxS_NamePrefix) -and ($Collection.Name -like $AppNameStringSearch))
{
LogIt -message ("Removing deployment from user collection: " + $Collection.Name) -component "Remove-AllDeployments()" -type "Verbose" -LogFile $LogFile
Remove-CMDeployment -ApplicationName $App.LocalizedDisplayName -CollectionName $Collection.Name -Force
}
}
Return
}
function Deploy-ApplicationtoCollection
{
<#
.DESCRIPTION
Deploys an application to a specific collection.
.PARAMETER App
Application object
.PARAMETER CollectionName
Name of collection (not the collection object)
.PARAMETER Install
If the deployment is for install or uninstall
.PARAMETER DeployPurpose
Whether the deployment is availible or required. Uninstall deployments are ALWAYS required.
.PARAMETER Deployments
Object from Get-CMDeployments. This will be used to search through to see if the application is already deployed.
.OUTPUTS
Nothing
.EXAMPLE
Deploy-ApplicationtoCollection $AppObject $CollectionName 'Install' 'Required' $DeploymentObject
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$CollectionName,
[Parameter(Mandatory=$true)]
[ValidateSet("Install","Uninstall")]
$Install,
[Parameter(Mandatory=$true)]
[ValidateSet("Available","Required")]
$DeployPurpose,
[Parameter(Mandatory=$true)]
$Deployments
)
$DeploymentsforApplication = @($Deployments) -match $App.PackageID
If ($DeploymentsforApplication.CollectionName -notcontains $CollectionName)
{
LogIt -message ("Deploying " + $App.LocalizedDisplayName + " to " + $CollectionName) -component "Deploy-ApplicationtoCollection()" -type "Verbose" -LogFile $LogFile
$Return = Start-CMApplicationDeployment -CollectionName $CollectionName -Name $App.LocalizedDisplayName -AppRequiresApproval $false -DeployAction $Install -DeployPurpose $DeployPurpose -EnableMomAlert $false -RebootOutsideServiceWindow $false -UseMeteredNetwork $false -UserNotification DisplaySoftwareCenterOnly -Force -WarningAction SilentlyContinue
}
Else
{
LogIt -message ($App.LocalizedDisplayName + " is already deployed to " + $CollectionName) -component "Deploy-ApplicationtoCollection()" -type "Verbose" -LogFile $LogFile
}
Return
}
function Remove-ApplicationtoCollection
{
<#
.DESCRIPTION
Removes an application deployment from a specific collection.
.PARAMETER App
Application object
.PARAMETER CollectionName
Name of collection (not the collection object)
.PARAMETER Deployments
Object from Get-CMDeployments. This will be used to search through to see if the application is already deployed.
.OUTPUTS
Nothing
.EXAMPLE
Deploy-ApplicationtoCollection $AppObject $CollectionName $DeploymentObject
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$CollectionName,
[Parameter(Mandatory=$true)]
$Deployments
)
$DeploymentsforApplication = @($Deployments) -match $App.PackageID
If ($DeploymentsforApplication.CollectionName -contains $CollectionName)
{
LogIt -message ("Removing deployment of " + $App.LocalizedDisplayName + " from " + $CollectionName) -component "Remove-ApplicationtoCollection()" -type "Verbose" -LogFile $LogFile
$Return = Remove-CMDeployment -ApplicationName $App.LocalizedDisplayName -CollectionName $CollectionName -Force
}
Else
{
LogIt -message ($App.LocalizedDisplayName + " is not deployed to " + $CollectionName) -component "Remove-ApplicationtoCollection()" -type "Verbose" -LogFile $LogFile
}
Return
}
function Create-Collection
{
<#
.DESCRIPTION
Creates a new collection and moves it to the desired location. If collection already exists, just move it to the desired location.
.PARAMETER CollectionName
Name of new collection
.PARAMETER FolderPath
Path to the desired folder
.PARAMETER Collections
Object from Get-CMDeviceCollection or Get-CMUserCollection. This will be used to search through to see if the collection already exists.
.PARAMETER CollectionType
Device collection or user collection
.OUTPUTS
Collection object for the created/found collection
.EXAMPLE
Create-Collection $App.LocalizedDisplayName $FolderPath $Collections 'Device'
#>
param (
[Parameter(Mandatory=$true)]
$CollectionName,
[Parameter(Mandatory=$true)]
$FolderPath,
[Parameter(Mandatory=$true)]
$Collections,
[Parameter(Mandatory=$true)]
[ValidateSet("Device","User")]
$CollectionType
)
If ($Collections.Name -notcontains $CollectionName)
{
LogIt -message ("Create Collection " + $CollectionName) -component "Create-Collection()" -type "Verbose" -LogFile $LogFile
$OBJ_RefreshSchedule = New-CMSchedule –RecurInterval Days –RecurCount 1
If ($CollectionType -eq 'Device')
{
$ReturnCollection = New-CMDeviceCollection -LimitingCollectionId UB100024 -Name $CollectionName -RefreshType Periodic -RefreshSchedule $OBJ_RefreshSchedule
}
ElseIf ($CollectionType -eq 'User')
{
$ReturnCollection = New-CMUserCollection -LimitingCollectionId SMS00004 -Name $CollectionName -RefreshType Periodic -RefreshSchedule $OBJ_RefreshSchedule
}
Else
{
LogIt -message ("Collection type unknown.") -component "Create-Collection()" -type "Error" -LogFile $LogFile
Return
}
Move-CMObject -FolderPath $FolderPath -InputObject $ReturnCollection
}
Else
{
LogIt -message ("Collection " + $CollectionName + " already exists.") -component "Create-Collection()" -type "Verbose" -LogFile $LogFile
$ReturnCollection = Get-CMDeviceCollection -Name $CollectionName
$Ignore = Move-CMObject -FolderPath $FolderPath -InputObject $ReturnCollection
}
Return $ReturnCollection
}
function Deploy-ApplicationtoSxS
{
<#
.DESCRIPTION
Creates a set of collections for Side-by-Side (Sxs) deployments. This consists of two collections, one that will be a reporting only collection and one that will be a deployment collection.
.PARAMETER App
Application object
.PARAMETERCollections
Object for all (device) collections, so we can check if the collection exists or not
.PARAMETERDeployments
Object for all deployments, so we can check if it's already deployed or not
.PARAMETERCMSite
CM site code is required because collection paths require the site code at the start (unlike all other path objects...)
.OUTPUTS
Nothing
.EXAMPLE
Deploy-ApplicationtoSxS $App $Collections $Deployments $CMSite
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$Collections,
[Parameter(Mandatory=$true)]
$Deployments,
[Parameter(Mandatory=$true)]
$CMSite
)
$SxS_InstalledName = "[SxS Installed] " + $App.LocalizedDisplayName
$SxS_Name = "[SxS Deployment] " + $App.LocalizedDisplayName
$SxS_CollectionLocation = $CMSite + ":\DeviceCollection\Operating System Deployment\SxS"
$SxSInstalledCollection = Create-Collection $SxS_InstalledName $SxS_CollectionLocation $Collections 'Device'
Add-CollectionVariable $App $SxSInstalledCollection
Create-DeviceCollectionQuery $App $SxSInstalledCollection
$SxSDeploymentCollection = Create-Collection $SxS_Name $SxS_CollectionLocation $Collections 'Device'
Deploy-ApplicationtoCollection $App $SxS_Name 'Install' 'Required' $Deployments
Return
}
function Add-CollectionVariable
{
<#
.DESCRIPTION
Creates a set of collection variables for Side-by-Side (Sxs) deployments.
This consists of four variables, three are 'debugging' level variables
(to allow us to tie this collection to a unique application),
and the SxS_ variable is the one used for actual variable based deployments.
.PARAMETER App
Application object
.PARAMETER Collection
Collection object (should contain just one collection)
.OUTPUTS
Nothing
.EXAMPLE
Deploy-ApplicationtoSxS $App $Collection
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$Collection
)
$VariableApplicationName = "ApplicationName"
$VariableApplicationID = "ApplicationID"
$VariableApplicationUniqueID = "ApplicationUniqueID"
$VariableApplicationOSD = "SxS_" + $App.CI_ID
$VariableApplicationOSD_Exists = Get-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationOSD
$VariableApplicationName_Exists = Get-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationName
$VariableApplicationID_Exists = Get-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationID
$VariableApplicationUniqueID_Exists = Get-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationUniqueID
$temp = $App.CI_UniqueID.split("/") | select -skip 1
$ApplicationGUID = $temp.split(" ") | select -First 1
If (-not $VariableApplicationOSD_Exists)
{
LogIt -message ("Variable for OSD does not exist.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = New-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationOSD -Value $App.LocalizedDisplayName
}
else
{
LogIt -message ("Variable for OSD exists. Setting to current value.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = Set-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationOSD -NewVariableValue $App.LocalizedDisplayName
}
If (-not $VariableApplicationName_Exists)
{
LogIt -message ("Variable for Application name does not exist.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = New-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationName -Value $App.LocalizedDisplayName
}
else
{
LogIt -message ("Variable for Application name exists. Setting to current value.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = Set-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationName -NewVariableValue $App.LocalizedDisplayName
}
$temp = $App.CI_UniqueID.split("/") | select -skip 1
$ApplicationGUID = $temp.split(" ") | select -First 1
If (-not $VariableApplicationUniqueID_Exists)
{
LogIt -message ("Variable for Application Unique ID does not exist.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = New-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationUniqueID -Value $ApplicationGUID
}
else
{
LogIt -message ("Variable for Application Unique ID exists. Setting to current value.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = Set-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationUniqueID -NewVariableValue $ApplicationGUID
}
If (-not $VariableApplicationID_Exists)
{
LogIt -message ("Variable for Application ID does not exist.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = New-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationID -Value $App.CI_ID
}
else
{
LogIt -message ("Variable for Application ID exists. Setting to current value.") -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
$Return = Set-CMDeviceCollectionVariable -Collection $Collection -VariableName $VariableApplicationID -NewVariableValue $App.CI_ID
}
Return
}
function Create-DeviceCollectionQuery
{
<#
.DESCRIPTION
Creates a set of collection queries for Side-by-Side (Sxs) deployments.
This consists of (up to) three queries, two for the Product Code found in the
deployment type window (same field location in the GUI, two locations in the AppXML)
and one for the Product Code found in the detection method.
.PARAMETER App
Application object
.PARAMETER Collection
Collection object (should contain just one collection)
.OUTPUTS
Nothing
.EXAMPLE
Create-DeviceCollectionQuery $App $Collection
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$Collection
)
$DeploymentTypes = Get-CMDeploymentType -ApplicationName $App.LocalizedDisplayName
foreach ($DeploymentType in $DeploymentTypes)
{
$temp = $DeploymentType.CI_UniqueID.split("/") | select -skip 1
$DeploymentTypeID = $temp.split(" ") | select -First 1
$XML = ([xml]$DeploymentType.SDMPackageXML).AppMgmtDigest.DeploymentType | ? { $_.LogicalName -eq $DeploymentTypeID }
$InstallTechnology = $XML.Technology
$ProductCode = $XML.Installer.CustomData.ProductCode
$SourceUpdateProductCode = $XML.Installer.CustomData.SourceUpdateProductCode
$DetectionMethodProductCode = $XML.Installer.CustomData.EnhancedDetectionMethod.Settings.MSI.ProductCode
LogIt -message ("Install Technology: " + $InstallTechnology) -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
LogIt -message ("Product Code: " + $ProductCode) -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
LogIt -message ("Source Update Product Code: " + $SourceUpdateProductCode) -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
LogIt -message ("Detection Method Product Code: " + $DetectionMethodProductCode) -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
$RuleName_ProductCode = 'ProductCode_' + $App.CI_ID + '_' + $DeploymentType.LocalizedDisplayName
$RuleName_SourceUpdateProductCode = 'SourceUpdate_' + $App.CI_ID + '_' + $DeploymentType.LocalizedDisplayName
$RuleName_DetectionMethodProductCode = 'DetectionCode_' + $App.CI_ID + '_' + $DeploymentType.LocalizedDisplayName
$Query_ProductCode = Get-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_ProductCode
$Query_SourceUpdateProductCode = Get-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_SourceUpdateProductCode
$Query_DetectionMethodProductCode = Get-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_DetectionMethodProductCode
LogIt -message ("Processing Product Code") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
If ($Query_ProductCode)
{
LogIt -message ("Query (" + $RuleName_ProductCode + ") already exists.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
ElseIf (-not $ProductCode)
{
LogIt -message ("Product Code does not exist for this Deployment Type.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
Else
{
LogIt -message ("Query (" + $RuleName_ProductCode + ") missing.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
$CollQuery = "select SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.Client from SMS_R_System inner join SMS_G_System_INSTALLED_SOFTWARE on SMS_G_System_INSTALLED_SOFTWARE.ResourceID = SMS_R_System.ResourceId where UPPER(SMS_G_System_INSTALLED_SOFTWARE.SoftwareCode) = `"" + $ProductCode + "`""
Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_ProductCode -QueryExpression $CollQuery
}
LogIt -message ("Processing Source Update Product Code") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
If ($Query_SourceUpdateProductCode)
{
LogIt -message ("Query (" + $RuleName_SourceUpdateProductCode + ") already exists.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
ElseIf (-not $SourceUpdateProductCode)
{
LogIt -message ("Source Update Product Code does not exist for this Deployment Type.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
Else
{
LogIt -message ("Query (" + $RuleName_SourceUpdateProductCode + ") missing.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
$CollQuery = "select SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.Client from SMS_R_System inner join SMS_G_System_INSTALLED_SOFTWARE on SMS_G_System_INSTALLED_SOFTWARE.ResourceID = SMS_R_System.ResourceId where UPPER(SMS_G_System_INSTALLED_SOFTWARE.SoftwareCode) = `"" + $SourceUpdateProductCode + "`""
Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_SourceUpdateProductCode -QueryExpression $CollQuery
}
LogIt -message ("Processing Detection Method Product Code") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
If ($Query_DetectionMethodProductCode)
{
LogIt -message ("Query (" + $RuleName_DetectionMethodProductCode + ") already exists.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
ElseIf (-not $DetectionMethodProductCode)
{
LogIt -message ("MSI Detection Method Product Code does not exist for this Deployment Type.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
}
Else
{
LogIt -message ("Query (" + $RuleName_DetectionMethodProductCode + ") missing.") -component "Create-DeviceCollectionQuery()" -type "Verbose" -LogFile $LogFile
$CollQuery = "select SMS_R_System.ResourceId, SMS_R_System.ResourceType, SMS_R_System.Name, SMS_R_System.SMSUniqueIdentifier, SMS_R_System.ResourceDomainORWorkgroup, SMS_R_System.Client from SMS_R_System inner join SMS_G_System_INSTALLED_SOFTWARE on SMS_G_System_INSTALLED_SOFTWARE.ResourceID = SMS_R_System.ResourceId where UPPER(SMS_G_System_INSTALLED_SOFTWARE.SoftwareCode) = `"" + $DetectionMethodProductCode + "`""
Add-CMDeviceCollectionQueryMembershipRule -CollectionName $Collection.Name -RuleName $RuleName_DetectionMethodProductCode -QueryExpression $CollQuery
}
}
Return
}
function Deploy-ApptoDistributionPointGroup
{
<#
.DESCRIPTION
Deploys an app to Distribution Point Groups
.PARAMETER App
Application object
.PARAMETER DistributionPointGroups
An Array of Distribution Point Group names
.OUTPUTS
Nothing
.EXAMPLE
Deploy-ApptoDistributionPointGroup $App @("Datacenters - All - Internal","Regional","DMZ - Internet-Facing","Stores - All")
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$DistributionPointGroups
)
ForEach ($DistributionPointGroup in $DistributionPointGroups)
{
LogIt -message ("Distributing " + $App.LocalizedDisplayName + " to " + $DistributionPointGroup) -component "Deploy-ApptoDistributionPointGroup()" -type "Verbose" -LogFile $LogFile
#We have to wrap this in a Try/Catch because if it's already deployed to the group, it will throw an error, which ErrorAction SilentlyContinue does not actually silently continue.
#This means the try will ALWAYS fail, unless everything is completely valid *AND* it's the first time we've every deployed this to the DP group.
Try
{
Start-CMContentDistribution -Application $App -DistributionPointGroupName $DistributionPointGroup -ErrorAction SilentlyContinue
}
Catch
{
#Do nothing
}
}
Return
}
function Remove-ApptoDistributionPointGroup
{
<#
.DESCRIPTION
Removes an app from Distribution Point Groups
.PARAMETER App
Application object
.PARAMETER DistributionPointGroups
An Array of Distribution Point Group names
.OUTPUTS
Nothing
.EXAMPLE
Remove-ApptoDistributionPointGroup $App @("Datacenters - All - Internal","Regional","DMZ - Internet-Facing","Stores - All")
#>
param (
[Parameter(Mandatory=$true)]
$App,
[Parameter(Mandatory=$true)]
$DistributionPointGroups
)
ForEach ($DistributionPointGroup in $DistributionPointGroups)
{
LogIt -message ("Removing " + $App.LocalizedDisplayName + " from " + $DistributionPointGroup) -component "AddCollectionVariable()" -type "Verbose" -LogFile $LogFile
#We have to wrap this in a Try/Catch because if it's already deployed to the group, it will throw an error, which ErrorAction SilentlyContinue does not actually silently continue.
#This means the try will ALWAYS fail, unless everything is completely valid *AND* it's the first time we've every deployed this to the DP group.
Try
{
Remove-CMContentDistribution -Application $App -DistributionPointGroupName $DistributionPointGroup -Force -ErrorAction SilentlyContinue
}
Catch
{
#Do nothing
}
}
Return
}