trickling members from one collection to another collection in configmgr

UPDATE: one of my coworkers was using this script to push out a hefty backup agent.  he couldn’t get it to run so while looking at it, i realized the last if/then condition was flawed.  it was looking for members of a compared object when no comparison was issued.  if the second collection is empty and the first collection is greater than the number of clients to send at once, it would fail.  anyway, all fixed.

1000 clients.
1 gb payload.

generally not the situation you want to be in when you’re deploying something over configmgr.  it’s not that it can’t handle it.  there are just certain provisions you have to take so that you do not overwhelm your distribution point(s).  one way to get through this to break down your collection into multiple collections and tie advertisements to them.
an easier way to manage this would be to percolate members from one collection into another.  if you’ve been around awhile, you may remember the script that rick jones wrote to do just this.  you can get it HERE.
the concept is fantastic.  i give props to the guy.  there were some minor things about it that i wasn’t happy about.  basically, it chooses random members of the source collection without depleting the source collection.  because of this, it’s entirely possible to retrieve the same member thus reducing amount of members to trickle in per pass.  anyway, aside from that, it works pretty well.
i wrote a new script in powershell to do the same thing except in a different way.  it works like this:
  • find the specified source and destination collection by name
  • compare the objects in both collections, deriving a list to add to the destination
  • add a specified number of objects
instead of writing query rules, it uses direct membership rules with the object’s resourceid.  once complete, it refreshes the destination collection.  it’s to be used with a scheduled task specified at whatever interval you choose.  my only warning is not to kick it off in swift succession.  there’s some lag time after the collection refresh occurs.  if you query the collection members before the refresh is complete, you’ll end up populating the previous collection members.  bad.  i’m not sure if there’s a negative affect, but empty direct membership rules will show up.  :/
i haven’t tried it with sms 2003.  i presume it would work.  anyway, the script is below.  let me know what you think.
# -------------------------------------------------------------------------------------------------------
# Author: Marcus C. Oh
# Name: sccm_tricklecollection.ps1
# Date: 2/3/2009
# Desc: Adds objects from source collection to destination collection
# -------------------------------------------------------------------------------------------------------

# Variables ---------------------------------------------------------------------------------------------
$sSMSServer = 'mySMSServer'    # SMS Server
$sFirstCollection = 'mySourceCollectionName'    # Source collection
$sSecondCollection = 'myDestinationCollectionName'    # Destination collection
$iNumberOfMembers = 10    # Number of members to add to destination collection per execution


# Functions ---------------------------------------------------------------------------------------------

# Retrieves the SMS site code
    function Get-SMSSiteCode([string]$sSMSServer) {
    
        $oSMSPath = Get-WmiObject -Namespace 'root\sms' `
            -Query 'select * from sms_providerlocation where providerforlocalsite = true' `
            -ComputerName $sSMSServer | Select-String sitecode
        
        $Script:sSiteCode = $oSMSPath.ToString().Split("""")[1]
    
    }


# Main --------------------------------------------------------------------------------------------------
@"

Trickle Collection --------------------------------------------------------------------------------------
SMS Server Name:     [$sSMSServer]
Source Collection:     [$sFirstCollection]
Dest Collection:    [$sSecondCollection]
Members to Add:        [$iNumberOfMembers]
---------------------------------------------------------------------------------------------------------

"@

Get-SMSSiteCode $sSMSServer

Write-Host "Retrieved SMS site code - $sSiteCode.  Searching collections..."

# Get a list of collections and find the objects to build the collection list.
$oCollections = Get-WmiObject -ComputerName $sSMSServer  -Namespace "root\sms\site_$sSiteCode" -Class 'SMS_Collection'
$oFirstColl = $oCollections | Where-Object { $_.Name -eq $sFirstCollection }
$oSecondColl = $oCollections | Where-Object { $_.Name -eq $sSecondCollection }

Write-Host "Retrieved main collection and trickle collection.  Retrieving members..."

# Grab the Resource IDs of the first collection
$oFirstCollMembers = Get-WmiObject -ComputerName $sSMSServer -Namespace "root\sms\site_$sSiteCode" `
    -Query "select * from SMS_CM_RES_COLL_$($oFirstColl.CollectionID)" | Select-Object $_.ResourceID

# Again for the second collection
$oSecondCollMembers = Get-WmiObject -ComputerName $sSMSServer -Namespace "root\sms\site_$sSiteCode" `
    -Query "select * from SMS_CM_RES_COLL_$($oSecondColl.CollectionID)" | Select-Object $_.ResourceID

Write-Host "Retrieved members of both collections.  Comparing objects..."

# Create an object to hold the collection rule
$oCollRule = [wmiclass]"\\$sSMSServer\root\sms\site_$($ssitecode):sms_collectionruledirect"
$oCollRule.PSBase.Properties["ResourceClassName"].Value = "SMS_R_System"


# Only execute if the second collection has members already
if ($oSecondCollMembers -ne $null) {
    
    #Compare the objects to see what is not in the second collection
    $oComparedMembers = Compare-Object -referenceObject $oFirstCollMembers -differenceObject $oSecondCollMembers `
        -SyncWindow 2000 -property 'ResourceID' | `
        Where-Object { $_.SideIndicator -eq '<=' } | `
        Select-Object $_.ResourceID
    
    if ( $oComparedMembers -eq $null ) {
        Write-Host -foregroundcolor Red "No members to add.  Exiting..."
        exit
    } else {
        Write-Host "Found members to add.  Continuing..."
    }
    
    Write-Host "Objects compared.  Adding members from source collection to destination collection...`n"
    
    # Add anything missing from the first collection, to the second collection
    if ( ($oComparedMembers | Measure-Object).Count -le $iNumberOfMembers ) {
        $oComparedMembers | `
            ForEach-Object {
                Write-Host -foregroundcolor Yellow "`tAdding Resource ID: $($_.ResourceID) to $sSecondCollection."
                $oCollRule.PSBase.Properties["ResourceID"].Value = $_.ResourceID
                $oSecondColl.AddMembershipRule($oCollRule) | Out-Null
            }
    } else {
    
        if ($oComparedMembers -ne $null) {
            $oComparedMembers[0..$iNumberOfMembers] | `
            ForEach-Object {
                Write-Host -foregroundcolor Yellow "`tAdding Resource ID: $($_.ResourceID) to $sSecondCollection."
                $oCollRule.PSBase.Properties["ResourceID"].Value = $_.ResourceID
                $oSecondColl.AddMembershipRule($oCollRule) | Out-Null
            }
        } else {
            Write-Host -foregroundcolor red "There are no new members to add.  Exiting..."
        }
    }

} else {
    # Since the second collection is blank, send all values from the first through
    
    Write-Host "Nothing to compare.  Adding members from source collection to destination collection..."
    
    if ( ($oFirstCollMembers | Measure-Object).Count -le $iNumberOfMembers ) {
        $oFirstCollMembers | `
            ForEach-Object {
                Write-Host -foregroundcolor Yellow "`tAdding Resource ID: $($_.ResourceID) to $sSecondCollection."
                $oCollRule.PSBase.Properties["ResourceID"].Value = $_.ResourceID
                $oSecondColl.AddMembershipRule($oCollRule) | Out-Null
            }
    } else {
        $oFirstCollMembers[0..$iNumberOfMembers] | ForEach-Object {
            Write-Host -foregroundcolor Yellow "`tAdding Resource ID: $($_.ResourceID) to $sSecondCollection."
            $oCollRule.PSBase.Properties["ResourceID"].Value = $_.ResourceID
            $oSecondColl.AddMembershipRule($oCollRule) | Out-Null
        }
    }
}

# Refresh the collection
$oSecondColl.RequestRefresh($False) | Out-Null

Write-Host -foregroundcolor green "`nMember addition complete."

Comments

  1. Wow. Great script. Is that what you were doing all day at SMUG? -- Matthew

    ReplyDelete
  2. thanks! it was good seeing you. you ran off before i could say bye. @ smug i was on facebook all day. who isn't? come on! :)

    ReplyDelete

Post a Comment