Thursday, February 23, 2017

SCCM Copy Random members to a collection powershell script

This script will take a given source SCCM collection and randomly grab a percentage of that collection and add it to a destination collection


cd "C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin"
Import-Module .\ConfigurationManager.psd1
cd CAS:

#$SrcCollectionName = 'source collection name'
#$DstCollectionName = 'destination collection name'


$SiteServer = 'siteservername'
$SiteCode = 'threelettersitecode'
$PercentToGet = 23
$UpdateCollectionMembership = $true

#uncomment this if you want/need to prompt for credentials
#$cred = Get-credential

[System.Collections.ArrayList]$DstItemList = @()
[System.Collections.ArrayList]$SrcItemList = @()

write-output "Going to clear out direct members of $DstCollectionName and then randomly collect $PercentToGet percent of the members of $SrcCollectionName and add them to $DstCollectionName "

#Gather collection ID and list of Items in src and dst collections
$SrcCollectionID = Get-CMDeviceCollection -Name $SrcCollectionName | Select CollectionID
$DstCollectionID = Get-CMDeviceCollection -Name $DstCollectionName | Select CollectionID
$SrcItemList = @(Get-CMDevice -CollectionId $SrcCollectionID.CollectionID | Select -Property Name, ResourceID)
$DstItemList = @(Get-CMDevice -CollectionId $DstCollectionID.CollectionID | Select -Property Name, ResourceID)

#clear out existing destination collection members
ForEach ($DstItem in $DstItemList)
 {
    $name = $DstItem.name
    $ID = $DstItem.ResourceID
    write-output "Removing $name with ID $ID from collection $DstCollectionName"
    Remove-CMDeviceCollectionDirectMembershipRule -CollectionName $DstCollectionName -ResourceId $ID -force
    }

#run update membership to update destination collection 
write-output "Triggering update membership for collection $DstCollectionName "
    $ID = $DstCollectionID.CollectionID
    Invoke-WmiMethod -Path "ROOT\SMS\Site_$($SiteCode):SMS_Collection.CollectionId='$ID'" -Name RequestRefresh -ComputerName $SiteServer 

#wait 4 minutes for the collection to update
write-output "Waiting 4 minutes for the update membership for collection $DstCollectionName "
start-sleep -s 240
 
#run update membership to update source collection as source collection may exclude previous destination collection
#this may not be necessary?
write-output "Triggering update membership for collection $SrcCollectionName "
    $ID = $SrcCollectionID.CollectionID
    Invoke-WmiMethod -Path "ROOT\SMS\Site_$($SiteCode):SMS_Collection.CollectionId='$ID'" -Name RequestRefresh -ComputerName $SiteServer 

#wait 4 minutes for the collection to update
write-output "Waiting 4 minutes for the update membership for collection $SrcCollectionName "
start-sleep -s 240

#Refresh contents for source collection arraylist
$SrcItemList = @(Get-CMDevice -CollectionId $SrcCollectionID.CollectionID | Select -Property Name, ResourceID)
 
#clear list of destination items to be used for input
Clear-variable DstItemList
[System.Collections.ArrayList]$DstItemList = @()

#Calc number of destination-bound items that we want to end up with
$DstNumberOfItems = ($SrcItemList).count * ($PercentToGet / 100)
$DstNumberOfItems = [math]::truncate($DstNumberOfItems)

write-output "Going to search for and add $PercentToGet Percent - $DstNumberOfItems items - to collection $DstCollectionName "

#go through source list and pick random items till we have enough
While ($NumberOfItems -ne $DstNumberOfItems)
 {
 $PickedItem = ($SrcItemList | Get-Random)
    $DstItemList += $PickedItem
 $SrcItemList.remove($PickedItem)
    $NumberOfItems = $DstItemList.count
 }

#add items to destination collection list
ForEach ($DstItem in $DstItemList)
 {
    $name = $DstItem.name
    $ID = $DstItem.ResourceID
    write-output "Adding $name with ID $ID to collection $DstCollectionName "
 Add-CMDeviceCollectionDirectMembershipRule -CollectionName $DstCollectionName -ResourceId $ID
 }

# trigger update collection membership if enabled in script
# this portion of the script is from:
# https://www.petervanderwoude.nl/post/update-collection-membership-in-configmgr-2012-via-powershell/

If ($UpdateCollectionMembership)
    {
 write-output "Triggering update membership for collection $DstCollectionName "
    $ID = $DstCollectionID.CollectionID
    Invoke-WmiMethod -Path "ROOT\SMS\Site_$($SiteCode):SMS_Collection.CollectionId='$ID'" -Name RequestRefresh -ComputerName $SiteServer
 
 #wait 4 minutes for the collection to update
 write-output "Waiting 4 minutes for the update membership for collection $DstCollectionName "
 start-sleep -s 240
 
    write-output "Triggering update membership for collection $SrcCollectionName "
    $ID = $SrcCollectionID.CollectionID
    Invoke-WmiMethod -Path "ROOT\SMS\Site_$($SiteCode):SMS_Collection.CollectionId='$ID'" -Name RequestRefresh -ComputerName $SiteServer


    }

No comments: