This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
SCCM_Folder.psm1
440 lines (369 loc) · 12.8 KB
/
SCCM_Folder.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
<#
.SYNOPSIS
Returns an SCCM folder object.
.DESCRIPTION
Returns an SCCM folder object. Currently, the objects returned can only be of Type 2 or 3 (Package folders and Advertisement folders, respectively).
If no folder name or ID are specified, this function returns all folders on the site.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER folderName
The name of the folder to be returned. If more than one folder is found with the same name, an array containing all matches is returned.
.PARAMETER folderNodeId
Unique folder ID of the folder to be returned.
.LINK
http://msdn.microsoft.com/en-us/library/cc145264.aspx
#>
Function Get-SCCMFolder {
[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(Position=0)]
[parameter(ParameterSetName="name")]
[string]
$folderName,
[parameter(ParameterSetName="id")]
[ValidateScript( { $_ -gt 0 } )]
[int]
$folderNodeId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
if($folderName) {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerNode" | Where { ($_.Name -like $folderName) -and (@(2,3) -contains $_.ObjectType) }
} elseif($folderNodeId) {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerNode" | Where { ($_.ContainerNodeID -eq $folderNodeId) -and (@(2,3) -contains $_.ObjectType) }
} else {
return Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerNode" | Where { @(2,3) -contains $_.ObjectType }
}
}
<#
.SYNOPSIS
Creates a new SCCM folder.
.DESCRIPTION
Creates a new SCCM folder. Currently, the supported folder types are Package and Advertisement folders. If there is already a folder with the specified
name under the same parent, an exception will be raised.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER folderName
The name of the new folder.
.PARAMETER folderType
The type of folder. Allowed values are 2 and 3 (Package and Advertisement, respectively).
.PARAMETER parentFolderNodeID
ID of the parent folder. If you specify 0, the folder will be placed under the root node for that folder type.
.LINK
http://msdn.microsoft.com/en-us/library/cc145264.aspx
#>
Function New-SCCMFolder {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateNotNull()]
[string]
$folderName,
[parameter(Mandatory=$true, Position=1)]
[ValidateRange(2,3)]
[int]
$folderType,
[parameter(Mandatory=$true, Position=2)]
[ValidateScript( { $_ -ge 0 } )]
[int]
$parentFolderNodeId=0
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
# We need to find out if there's another folder with that name under the same parent. If there is, we throw an exception and don't create the folder.
$allFolders = Get-SCCMFolder -siteProvider $siteProvider -siteCode $siteCode
foreach($folder in $allFolders) {
if( ($folder.Name -eq $folderName) -and ($folder.ParentContainerNodeID -eq $parentFolderNodeId) -and ($folder.ObjectType -eq $folderType) ) {
Throw "There is already a folder named $folderName under the same parent folder with ID $parentFolderNodeId"
}
}
# If we made it down here is because we need to go ahead with the folder creation.
$folder = ([wmiclass]("\\$siteProvider\ROOT\sms\site_" + $siteCode + ":SMS_ObjectContainerNode")).CreateInstance()
if($folder) {
$folder.Name = $folderName
$folder.ObjectType = $folderType
$folder.ParentContainerNodeID = $parentFolderNodeId
$folder.Put() | Out-Null
return $folder
} else {
Throw "There was a problem creating the folder"
}
}
<#
.SYNOPSIS
Saves a folder back into the SCCM database.
.DESCRIPTION
This function is used to save direct property changes to folders back to the SCCM database.
.PARAMETER folder
The folder object to be put back into the database.
#>
Function Save-SCCMFolder {
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateNotNull()]
$folder
)
$folder.Put() | Out-Null
}
<#
.SYNOPSIS
Delete an SCCM folder.
.DESCRIPTION
Deletes and SCCM folder. If you delete a parent folder without deleting its children, SCCM will by default assign the topmost
child folder the parent folder ID of 0.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER folderNodeId
The unique node ID for the folder being deleted.
.LINK
http://msdn.microsoft.com/en-us/library/cc145264.aspx
#>
Function Remove-SCCMFolder {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateScript( { $_ -gt 0 } )]
[ValidateNotNull()]
[int]
$folderNodeId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$folder = Get-SCCMFolder -siteProvider $siteProvider -siteCode $siteCode -folderNodeId $folderNodeId
if($folder) {
$folder.Delete() | Out-Null
}
}
<#
.SYNOPSIS
Moves an SCCM Folder.
.DESCRIPTION
Moves an SCCM Folder.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER folderNodeId
The unique node ID for the folder being moved.
.PARAMETER newParentFolderNodeId
The unique node ID of the new parent folder.
.LINK
http://msdn.microsoft.com/en-us/library/cc145264.aspx
#>
Function Move-SCCMFolder {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateScript( { $_ -gt 0 } )]
[ValidateNotNull()]
[int]
$folderNodeId,
[parameter(Mandatory=$true, Position=1)]
[ValidateScript( { $_ -ge 0 } )]
[ValidateNotNull()][int]
$newParentFolderNodeId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
# We need to make sure the parent actually exists, if it doesn't, we're moving the folder to the root folder for that object type.
if($newParentFolderNodeId -ne 0) {
$parentFolder = Get-SCCMFolder -siteProvider $siteProvider -siteCode $siteCode -folderNodeId $newParentFolderNodeId
if(!$parentFolder) {
$newParentFolderNodeId = 0
}
}
$folder = Get-SCCMFolder -siteProvider $siteProvider -siteCode $siteCode -folderNodeId $folderNodeId
if($folder) {
$folder.ParentContainerNodeID = $newParentFolderNodeId
$folder.Put() | Out-Null
return $folder
}
}
<#
.SYNOPSIS
Moves objects between containers.
.DESCRIPTION
Moves objects between containers.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER packageId
The instance key of the object to be moved.
.PARAMETER targetContainerNodeId
Node ID of the target container. If it is 0, the object is moved back to the root container of objects of the same type.
.PARAMETER objectType
The type of object being moved. Current supported values are 2 (packages) and 3 (advertisements).
.LINK
http://msdn.microsoft.com/en-us/library/cc144997.aspx
.LINK
http://msdn.microsoft.com/en-us/library/cc146279.aspx
.LINK
http://msdn.microsoft.com/en-us/library/cc145264.aspx
#>
Function Move-ObjectToContainer {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$instanceKey,
[parameter(Mandatory=$true, Position=1)]
[ValidateScript( { $_ -ge 0 } )]
[ValidateNotNull()]
[int]
$targetContainerNodeId,
[parameter(Mandatory=$true, Position=3)]
[ValidateRange(2,3)]
[int]
$objectType
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$sourceContainerId = 0
$sourceContainer = Get-WMIObject -ComputerName $siteProvider -Namespace "root\sms\site_$siteCode" -Query "Select * From SMS_ObjectContainerItem Where InstanceKey='$instanceKey'"
if($sourceContainer) {
# The object is in a folder other than the root folder.
$sourceContainerId = $sourceContainer.ContainerNodeID
}
$containerClass = [WMIClass]("\\$siteProvider\root\sms\site_" + "$siteCode" + ":SMS_ObjectContainerItem")
$result = $containerClass.MoveMembers($instanceKey, $sourceContainerId, $targetContainerNodeId, $objectType)
return $result.ReturnValue
}
<#
.SYNOPSIS
Moves an SCCM Package to a folder.
.DESCRIPTION
Moves an SCCM Package to a folder.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER packageId
The ID of the package to be moved.
.PARAMETER targetFolderNodeId
The folder to which the package is to be moved. If this value is 0, the package is removed from all folders.
#>
Function Move-SCCMPackageToFolder {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$packageId,
[parameter(Mandatory=$true, Position=1)]
[ValidateScript( { $_ -ge 0 } )]
[ValidateNotNull()]
[int]
$targetFolderNodeId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$result = Move-ObjectToContainer -siteProvider $siteProvider -siteCode $siteCode -instanceKey $packageId -targetContainerNodeId $targetFolderNodeId -objectType 2
if($result -ne 0) {
Throw "There was a problem moving package with ID $packageId to folder with ID $targetFolderNodeId"
}
}
<#
.SYNOPSIS
Moves an SCCM advertisement to a folder.
.DESCRIPTION
Moves an SCCM advertisement to a folder.
.PARAMETER siteProvider
The name of the site provider.
.PARAMETER siteCode
The 3-character site code.
.PARAMETER advertisementId
The ID of the advertisement to be moved.
.PARAMETER targetFolderNodeId
The folder to which the advertisement is to be moved. If this value is 0, the advertisement is removed from all folders.
#>
Function Move-SCCMAdvertisementToFolder {
[CmdletBinding()]
param (
[string]
$siteProvider,
[string]
$siteCode,
[parameter(Mandatory=$true, Position=0)]
[ValidateLength(8,8)]
[string]
$advertisementId,
[parameter(Mandatory=$true, Position=1)]
[ValidateScript( { $_ -ge 0 } )]
[ValidateNotNull()]
[int]
$targetFolderNodeId
)
if(!($PSBoundParameters) -or !($PSBoundParameters.siteProvider)) {
$siteProvider = Get-SCCMSiteProvider
}
if(!($PSBoundParameters) -or !($PSBoundParameters.siteCode)) {
$siteCode = Get-SCCMSiteCode
}
$result = Move-ObjectToContainer -siteProvider $siteProvider -siteCode $siteCode -instanceKey $advertisementId -targetContainerNodeId $targetFolderNodeId -objectType 3
if($result -ne 0) {
Throw "There was a problem moving advertisement with ID $advertisementId to folder with ID $folderNodeId"
}
}