-
Notifications
You must be signed in to change notification settings - Fork 7
/
Create Collections based on Package or Application names
96 lines (82 loc) · 4.36 KB
/
Create Collections based on Package or Application names
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
<#
.Synopsis
This script creates packages/Applications based on their name
.DESCRIPTION
.EXAMPLE
Add-CMCollectionBasedOnPackageName.ps1 -SiteCode pr1 -SiteServer localhost -FolderName test1 -LimitingCollectionName "All Systems" -PackageType Package -DeviceCollection
.EXAMPLE
Add-CMCollectionBasedOnPackageName.ps1 -SiteCode pr1 -SiteServer localhost -FolderName "test 2" -LimitingCollectionName "All Systems" -PackageType Application -DeviceCollection
.EXAMPLE
Add-CMCollectionBasedOnPackageName.ps1 -SiteCode pr1 -SiteServer localhost -FolderName "test 2" -LimitingCollectionName "All Users" -PackageType Application -UserCollection
.EXAMPLE
Add-CMCollectionBasedOnPackageName.ps1 -SiteCode pr1 -SiteServer localhost -FolderName "test 2" -LimitingCollectionName "RAM 2010" -PackageType Application -UserCollection
.NOTES
Developed by Kaido Järvemets
Version 1.0
#>
[CMDLETBINDING()]
Param(
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM SiteCode",ParameterSetName='DeviceCollection')]
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM SiteCode",ParameterSetName='UserCollection')]
$SiteCode,
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Site Server",ParameterSetName='DeviceCollection')]
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Site Server",ParameterSetName='UserCollection')]
$SiteServer,
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Folder Name",ParameterSetName='DeviceCollection')]
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Folder Name",ParameterSetName='UserCollection')]
$FolderName,
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Limiting Collection Name",ParameterSetName='DeviceCollection')]
[Parameter(Mandatory=$True,HelpMessage="Please Enter CM Limiting Collection Name",ParameterSetName='UserCollection')]
$LimitingCollectionName,
[Parameter(Mandatory=$True,ParameterSetName='DeviceCollection')]
[Parameter(Mandatory=$True,ParameterSetName='UserCollection')]
[ValidateSet("Application","Package")]
$PackageType,
[Parameter(Mandatory=$True,ParameterSetName='DeviceCollection')]
[Switch]$DeviceCollection,
[Parameter(Mandatory=$True,ParameterSetName='UserCollection')]
[Switch]$UserCollection
)
Switch($PackageType)
{
"Application" {$ObjectType = 6000}
"Package" {$ObjectType = 2}
}
If([intPtr]::size -eq 4){
$CurentLocation = Get-Location
$FolderIDQuery = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_ObjectContainernode -Filter "Name='$FolderName' and ObjectType='$ObjectType'" -ComputerName $SiteServer
$ItemsInFolder = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_ObjectContainerItem -Filter "ContainerNodeID='$($FolderIDQuery.ContainerNodeID)' and ObjectType='$ObjectType'" -ComputerName $SiteServer
Import-Module $ENV:SMS_ADMIN_UI_PATH.replace("bin\i386","bin\ConfigurationManager.psd1")
$SiteCode = Get-PSDrive -PSProvider CMSITE | Where-Object {$_.Name -eq $SiteCode}
Set-Location "$($SiteCode):"
foreach($item in $ItemsInFolder)
{
Switch($PackageType)
{
"Package"
{
$PackageNameQUery = Get-CMPackage -Id $item.InstanceKey
If($DeviceCollection){
New-CMDeviceCollection -Name $PackageNameQUery.Name -LimitingCollectionName $LimitingCollectionName
}
If($UserCollection){
New-CMUserCollection -Name $PackageNameQUery.Name -LimitingCollectionName $LimitingCollectionName
}
}
"Application"
{
$ApplicationNameQuery = Get-WmiObject -Namespace "Root\SMS\Site_$SiteCode" -Class SMS_ApplicationLatest -Filter "ModelName='$($Item.InstanceKey)'" -ComputerName $SiteServer
If($DeviceCollection){
New-CMDeviceCollection -Name $ApplicationNameQuery.LocalizedDisplayName -LimitingCollectionName $LimitingCollectionName
}
If($UserCollection){
New-CMUserCollection -Name $ApplicationNameQuery.LocalizedDisplayName -LimitingCollectionName $LimitingCollectionName
}
}
}
}
Set-Location $CurentLocation
}
Else{
Write-host "Please Start x86 of Windows PowerShell" -ForegroundColor RED
}