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
wscript.quit
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
Next
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
Next
WScript.Quit(0)

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 –> http://social.technet.microsoft.com/Forums/en-US/configmgrsdk/thread/9205e49b-9d0e-462e-8998-87e6c31f9c41

Further samples and additional parameter information can be found here http://technet.microsoft.com/en-us/library/bb693758.aspx and http://technet.microsoft.com/en-us/library/cc181183.aspx



----------------------------------------------------------------------------
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

8 thoughts on “Remove computer association from collection after SCCM task sequence

  1. Sachin

    Hi,

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

    Kindly advice …

    Regards

    Reply
  2. danovich (Post author)

    I would also recommending using Jörgen’s updated script from here – http://ccmexec.com/2010/03/remove-a-computer-from-a-collections-when-osd-task-sequence-is-completed/ . 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
    const EVENTLOG_INFORMATION = 4

    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
    wscript.quit
    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
    Next

    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

    Next

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

    Wscript.Quit

    ‘————————————————
    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
    next
    End Sub

    ‘————————————————

    WScript.Quit(0)

    Reply
  3. faulknerp

    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?

    Reply
  4. Pingback: Scheduled OS deployments - Page 2

Leave a Reply