March 12 2010

Remove computer association from collection after SCCM task sequence

During SCCM OS deployment, you need to add a computer association so you can deploy your OS to a targeted machine. The problem with this is that the computer resource is still left in the collection after deployment. Use this technique to delete the resource once a successful OS deployment is completed:

Copy the below code into a VBS file called remove_from_collection.vbs.

Modify the code so that the sCollectionID = “ABC00013” has the collection ID of your collection from which the resource will be deleted:

On Error Resume Next
Dim oNetwork, oLocator, oSWbemServices, oCollection
Dim sComputerName, sSMSServer, sSMSSiteCode, sCollectionID, RuleSet
Set oNetwork = CreateObject("WScript.NetWork")
'CollectionID from which to remove the comouter
sCollectionID = "ABC00013"
'Get Command Line arguments
Set args = WScript.Arguments
sComputername = args.Item(0)
If sComputerName = NULL then
End if
'Main script
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.
Set swbemServices = swbemLocator.ConnectServer(".", "rootSMS")
Set oProviderLocation = swbemServices.InstancesOf("SMS_ProviderLocation")
For Each oLocation In oProviderLocation
If oLocation.ProviderForLocalSite = True Then
Set swbemServices = swbemLocator.ConnectServer(oLocation.Machine, "rootsmssite_" + oLocation.SiteCode)
End If
Set oCollection = SWbemServices.Get("SMS_Collection='" & sCollectionID & "'")
RuleSet = oCollection.CollectionRules
For Each Rule In RuleSet
If Rule.Path_.Class = "SMS_CollectionRuleDirect" Then
If LCase(Trim(Rule.RuleName)) = LCase(Trim(sComputerName)) Then
oCollection.DeleteMembershipRule Rule
End If
End If

Create a Status Filter rule by going to Site Database – Site Management – Primary Site – Site Settings – Status Filter Rules.

On the general tab, use Component : Task Sequence Manager, Message ID: 11171 .

On the actions tab, tick Run a program and use something like ‘cscript.exe D:SCRIPTSremove_from_collection.vbs %msgsys’.

This will trigger the script each time a task sequence is reported to the SCCM primary site to be completed successfully and will use the computername (%msgsys) from the status message and remove it from the collection in the script.

Original posting and more discussion here –>

Further samples and additional parameter information can be found here and

I use a maximum of one Google Ad per post to help offset some of my blog hosting costs.


Tags: , , ,

Posted March 12, 2010 by danovich in category "SMS / SCCM


  1. By Sachin on


    I did steps as you mentioned , but computer not deleted after “successful” reimage.

    Kindly advice …


  2. By Sachin on

    Thanks for reply..

    Yes I can see message id 11171 sent by component – Task Sequence Manager.


  3. By danovich (Post author) on

    I would also recommending using Jörgen’s updated script from here – . Personally on top of that, and as some comments recommend, I remove ‘and Obsolete = 0’ from line 49, so it looks something like:

    Option Explicit
    ‘ Constants for type of event log entry

    Dim Args
    Dim swbemLocator, SWbemServices, objCollection, oProviderLocation, oLocation
    Dim strComputerName, arrComputers, objComputer, sCollectionIDs
    Dim objDirectRule
    Dim strmessage, objshell
    Dim seventlog, sClearPxeflag

    On Error Resume Next

    ‘CollectionIDs from which to remove the computer
    ‘Should an eventlog entry be generated, set Seventlog=1

    sEventlog = “1”
    sCollectionIDs = “xxx000FC:xxx000FE:xxx000FB:xxx000FD”

    ‘Get Command Line arguments

    Set args = WScript.Arguments
    strComputername = args.Item(0)

    If strComputerName = NULL then
    End if

    ‘Main script

    set objShell = CreateObject(“WScript.Shell”)

    Set swbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
    swbemLocator.Security_.AuthenticationLevel = 6 ‘Packet Privacy.
    Set swbemServices = swbemLocator.ConnectServer(“.”, “rootSMS”)
    Set oProviderLocation = swbemServices.InstancesOf(“SMS_ProviderLocation”)
    For Each oLocation In oProviderLocation
    If oLocation.ProviderForLocalSite = True Then
    Set swbemServices = swbemLocator.ConnectServer(oLocation.Machine, “rootsmssite_” + oLocation.SiteCode)
    End If

    Set arrComputers = SWbemServices.ExecQuery(“select * from SMS_R_System where Name='” & strComputerName & “‘”)

    For Each objComputer In arrComputers
    RemoveCollectionMembership objComputer.ResourceID

    ‘Write to eventlog if Seventlog = 1
    If Seventlog = “1” then
    strMessage = strcomputername & ” will be removed from the following collection ID’s ” & scollectionids
    objShell.LogEvent EVENTLOG_INFORMATION, strMessage
    End IF


    Set objCollection = Nothing
    Set SWbemServices = Nothing
    Set SWbemLocator = Nothing


    Sub RemoveCollectionMembership(intresourceid)
    on error resume next

    Dim mCollectionID, i
    mCollectionID = Split (sCollectionIDs, “:”)
    for i = Lbound(mCollectionID) to UBound(mCollectionID)

    Set objCollection = SWbemServices.Get(“SMS_Collection='” & MCollectionID(i) & “‘”)
    Set ObjDirectRule = SWbemServices.Get(“SMS_CollectionRuleDirect”).SpawnInstance_
    ObjDirectRule.ResourceID = intresourceid
    ObjCollection.DeleteMembershipRule objDirectRule
    End Sub



  4. By faulknerp on

    This looks exactly like what I need! However I can’t quite seem to get it to work.

    I am getting an event log entry written by the status filter rule when a system completes a task sequences (with component “UNKNOWN COMPONENT”) but the object is still in the collection. When I run the script manually it removes it correctly. Has anyone got any ideas please?

  5. Pingback: Scheduled OS deployments - Page 2

Leave a Reply