forked from andrebocchini/sccm-powershell-automation-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCCM_Advertisement.psm1
427 lines (356 loc) · 13.7 KB
/
SCCM_Advertisement.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
<#
.SYNOPSIS
Creates a new SCCM advertisement.
.DESCRIPTION
Creates a new SCCM advertisement for a software distribution program and assigns it to a collection.
This function provides a limited set of parameters so it can create a basic package. It will create the object, save it to the
database and return a copy of it so you can finish customizing it. Once you finish customizing the properties of the object, save
it back using Save-SCCMAdvertisement. Follow the link in the LINK section of this documentation block to find out the options available
for customizing an advertisement.
.PARAMETER siteProvider
Site provider for the site where the advertisement will be created.
.PARAMETER siteCode
The 3-character code for the site where the advertisement will be created.
.PARAMETER advertisementName
Name of the new advertisement.
.PARAMETER collectionId
Collection ID of the collection where the advertisement will be assigned.
.PARAMETER packageId
ID of the package to be advertised.
.PARAMETER programName
Named of the program to be advertised that is part of the package definied by the parameter packageId.
.LINK
http://msdn.microsoft.com/en-us/library/cc146108.aspx
#>
Function New-SCCMAdvertisement {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateNotNull()]
[string]
$advertisementName,
[parameter(Mandatory=$true, Position=1)]
[ValidateLength(8,8)]
[string]
$collectionId,
[parameter(Mandatory=$true, Position=2)]
[ValidateLength(8,8)]
[string]
$packageId,
[parameter(Mandatory=$true, Position=3)]
[ValidateNotNull()]
[string]
$programName
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
if(!(Get-SCCMCollection -siteProvider $siteProvider -siteCode $siteCode -collectionId $collectionId)) {
Throw "Invalid collection with ID $collectionId"
} elseif(!(Get-SCCMPackage -siteProvider $siteProvider -siteCode $siteCode -packageId $packageId)) {
Throw "Invalid package with ID $packageId"
} elseif(!(Get-SCCMProgram -siteProvider $siteProvider -siteCode $siteCode $packageId $programName)) {
Throw "Invalid program with name `"$programName`""
} else {
$newAdvertisement = ([WMIClass]("\\$siteProvider\root\sms\site_" + "$siteCode" + ":SMS_Advertisement")).CreateInstance()
$newAdvertisement.AdvertisementName = $advertisementName
# For some reason, if you try to pass the collection ID with lowercase characters, the SCCM console will crash when you right-click it and
# visit the Advertisements tab. So just to be safe, we convert it to upper case.
$newAdvertisement.CollectionID = $collectionId.ToUpper()
$newAdvertisement.Comment = ""
$newAdvertisement.PackageID = $packageId
$newAdvertisement.ProgramName = $programName
$newAdvertisement.PresentTime = Convert-DateToSCCMDate $(Get-Date)
$newAdvertisement.AdvertFlags = 33554464
$newAdvertisement.RemoteClientFlags = 8240
$newAdvertisement.Priority = 2
$advertisementCreationResult = $newAdvertisement.Put()
$newAdvertisementId = $($advertisementCreationResult.RelativePath).TrimStart('SMS_Advertisement.AdvertisementID=')
$newAdvertisementId = $newAdvertisementId.Substring(1,8)
return Get-SCCMAdvertisement -siteProvider $siteProvider -siteCode $siteCode -advertisementId $newAdvertisementId
}
}
<#
.SYNOPSIS
Saves an advertisement back into the SCCM database.
.DESCRIPTION
The functions in this module that are used to create advertisements only have a limited number of supported parameters, but
SCCM advertisements are objects with a large number of settings. When you create an advertisement, it is likely that you will
want to edit some of those settings. Once you are finished editing the properties of the advertisement, you need to save it back
into the SCCM database by using this method.
.PARAMETER advertisement
The advertisement object to be put back into the database.
#>
Function Save-SCCMAdvertisement {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateNotNull()]
$advertisement
)
$advertisement.Put() | Out-Null
}
<#
.SYNOPSIS
Deletes an SCCM advertisement.
.DESCRIPTION
Deletes an SCCM advertisement based on an advertisement ID.
.PARAMETER siteProvider
The site provider for the site where the advertisement exists.
.PARAMETER siteCode
The 3-character code for the site where the advertisement exists.
.PARAMETER advertisementId
The ID of the advertisement to be deleted.
#>
Function Remove-SCCMAdvertisement {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$advertisementId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$advertisement = Get-SCCMAdvertisement -siteProvider $siteProvider -siteCode $siteCode -advertisementId $advertisementId
if($advertisement) {
$advertisement.Delete()
} else {
Throw "Invalid advertisement with ID $advertisementId"
}
}
<#
.SYNOPSIS
Retrieves SCCM advertisements from the site provider.
.DESCRIPTION
Takes in information about a specific site and an advertisement name and/or and advertisement ID and returns advertisements matching the specified parameters.
If no advertisement name or ID is specified, it returns all advertisements found on the site.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code for the site where the advertisement exists.
.PARAMETER advertisementName
Optional parameter. If specified, the function will try to find advertisements that match the specified name.
.PARAMETER advertisementId
Optional parameter. If specified, the function will try to find advertisements that match the specified ID.
.EXAMPLE
Get-SCCMAdvertisement -siteProvider MYSITEPROVIDER -siteCode SIT -advertisementName MYADVERTISEMENT
Description
-----------
Retrieve the advertisement named MYADVERTISEMENT from site SIT on MYSITEPROVIDER
.EXAMPLE
Get-SCCMAdvertisement -siteProvider MYSITEPROVIDER -siteCode SIT
Description
-----------
Retrieve all advertisements from site SIT on MYSITEPROVIDER
.EXAMPLE
Get-SCCMAdvertisement -siteProvider MYSITEPROVIDER -siteCode SIT | Select-Object Name,AdvertisementID
Description
-----------
Retrieve all advertisements from site SIT on MYSITEPROVIDER and filter out only their names and IDs
#>
Function Get-SCCMAdvertisement {
[CmdletBinding(DefaultParametersetName="default")]
param (
[parameter(ParameterSetName="name")]
[parameter(ParameterSetName="default")]
[parameter(ParameterSetName="id")]
[string]
$siteProvider,
[parameter(ParameterSetName="name")]
[parameter(ParameterSetName="default")]
[parameter(ParameterSetName="id")]
[string]
$siteCode,
[parameter(ParameterSetName="name", Position=0, ValueFromPipeline=$true)]
[ValidateNotNull()]
[string]
$advertisementName,
[parameter(Position=1)]
[parameter(ParameterSetName="id")]
[ValidateLength(8,8)]
[ValidateNotNull()]
[string]
$advertisementId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
if($advertisementName) {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Class "SMS_Advertisement" | where { $_.AdvertisementName -like $advertisementName }
} elseif($advertisementId) {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Class "SMS_Advertisement" | where { $_.AdvertisementID -eq $advertisementId }
} else {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_Advertisement"
}
}
<#
.SYNOPSIS
Returns a list of advertisements assigned to a collection.
.DESCRIPTION
Takes in information about a specific site, along with a collection ID and returns all advertisements assigned to that collection.
#>
Function Get-SCCMAdvertisementsForCollection {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$collectionId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
return Get-WMIObject -Computer $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * from SMS_Advertisement WHERE CollectionID='$collectionId'"
}
<#
.SYNOPSIS
Returns a list of advertisements assigned to a computer.
.DESCRIPTION
Takes in information about a specific site, along with a computer resource ID and returns all advertisements assigned to that computer.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character code for the site where the computer exists.
.PARAMETER resourceId
Resource ID of the computers whose advertisements are being retrieved.
#>
Function Get-SCCMAdvertisementsForComputer {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateScript( { $_ -gt 0 } )]
[int]
$resourceId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$collections = Get-SCCMCollectionsForComputer -siteProvider $siteProvider -siteCode $siteCode -resourceId $resourceId
$computerAdvertisements = @()
foreach($collection in $collections) {
$advertisements = Get-SCCMAdvertisementsForCollection -siteProvider $siteProvider -siteCode $siteCode $collection.CollectionID
foreach($advertisement in $advertisements) {
$computerAdvertisements += $advertisement
}
}
return $computerAdvertisements
}
<#
.SYNOPSIS
Returns the assigned schedule for an advertisement.
.DESCRIPTION
Returns an array containing all of the assigned schedule objects for an advertisement.
.PARAMETER advertisement
The advertisement object whose schedule is being retrieved.
.LINK
http://msdn.microsoft.com/en-us/library/cc145924.aspx
#>
Function Get-SCCMAdvertisementAssignedSchedule {
[CmdletBinding()]
param (
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNull()]
$advertisement
)
$advertisement.Get() | Out-Null
return $advertisement.AssignedSchedule
}
<#
.SYNOPSIS
Sets the assigned schedule for an advertisement.
.DESCRIPTION
Sets the assigned schedule for an advertisement using an array of SMS_ScheduleToken objects passed as a parameter.
.PARAMETER advertisement
The advertisement whose schedule is being set.
.PARAMETER schedule
The array of SMS_ScheduleToken objects to be set as the advertisement schedule. If a a $null or empty are passed, the
assignment schedule is cleared.
.NOTES
You can create schedule token objects using the schedule token functions in this module.
.LINK
http://msdn.microsoft.com/en-us/library/cc145924.aspx
#>
Function Set-SCCMAdvertisementAssignedSchedule {
[CmdletBinding()]
param (
[parameter(Mandatory=$true, Position=0)]
[ValidateNotNull()]
$advertisement,
[parameter(Mandatory=$true, Position=1)]
[AllowNull()]
$schedule
)
$advertisement.Get() | Out-Null
$advertisement.AssignedSchedule = $schedule
$advertisement.Put() | Out-Null
}
<#
.SYNOPSIS
Returns a list of advertisements for a specific package.
.DESCRIPTION
Takes in information about a specific site, along with a package ID and returns all advertisements for that package.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code for the site to be queried.
.PARAMETER packageId
The ID of the package whose advertisements the function should retrieve.
.EXAMPLE
Get-SCCMAdvertisementForPackage -siteProvider MYSITEPROVIDER -siteCode SIT -packageId MYPACKAGEID
Description
-----------
Retrieve all advertisements from site SIT on MYSITEPROVIDER for package with ID equal to MYPACKAGEID
#>
Function Get-SCCMAdvertisementsForPackage {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$packageId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Class "SMS_Advertisement" | where { $_.PackageID -eq $packageId }
}