Created
January 26, 2018 05:59
-
-
Save keithga/de8a602d2fd332c2d654baa14d33a5d4 to your computer and use it in GitHub Desktop.
Replacement script for Add-CMDeviceCollectionDirectMembership
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Example of how to create a Device Collection and populate it with computer objects | |
The Faster way. <Yea!> | |
#> | |
[cmdletbinding()] | |
param( | |
$CollBaseName = 'MyTestCol_0C_{0:D4}', | |
$name = 'PCTest*' | |
) | |
#region Replacement function | |
Function Add-ResourceToCollection { | |
[CmdLetBinding()] | |
Param( | |
[string] $SiteCode = 'CHQ', | |
[string] $SiteServer = $env:computerName, | |
[string] $CollectionName, | |
[parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
$System | |
) | |
begin { | |
$WmiArgs = @{ NameSpace = "Root\SMS\Site_$SiteCode"; ComputerName = $SiteServer } | |
$CollectionQuery = Get-WmiObject @WMIArgs -Class SMS_Collection -Filter "Name = '$CollectionName' and CollectionType='2'" | |
$InParams = $CollectionQuery.PSBase.GetMethodParameters('AddMembershipRules') | |
$Cls = [WMIClass]"Root\SMS\Site_$($SiteCode):SMS_CollectionRuleDirect" | |
$Rules = @() | |
} | |
process { | |
foreach ( $sys in $System ) { | |
$NewRule = $cls.CreateInstance() | |
$NewRule.ResourceClassName = "SMS_R_System" | |
$NewRule.ResourceID = $sys.ResourceID | |
$NewRule.Rulename = $sys.Name | |
$Rules += $NewRule.psobject.BaseObject | |
} | |
} | |
end { | |
$InParams.CollectionRules += $Rules.psobject.BaseOBject | |
$CollectionQuery.PSBase.InvokeMethod('AddMembershipRules',$InParams,$null) | Out-null | |
$CollectionQuery.RequestRefresh() | out-null | |
} | |
} | |
#endregion | |
foreach ( $Count in 5,50,500,5000 ) { | |
$CollName = $CollBaseName -f $Count | |
write-verbose "Create a collection called '$CollName'" | |
New-CMDeviceCollection -LimitingCollectionName 'All Systems' -Name $CollName | % name | write-Host | |
Measure-Command { | |
Write-Verbose "Find all Devices that match [$Name], grab only the first $Count, and add to Collection [$CollName]" | |
get-cmdevice -name $name -Fast | | |
Select-Object -first $count | | |
Add-ResourceToCollection -CollectionName $CollName | |
} | % TotalSeconds | write-Host | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment