Wednesday, April 24, 2013

Clear out OSD collections in 2007

Do you have OSD collections in 2007 that you drop machines into for imaging.  What happens when  a machine fails to image completely or send the completion code and remove from the collection.  Well they say in an users see that "Operating System Deployment is ready..."  so here is some code to help you remove computers from a collection, create a task on the primary site to run at a given time:


    set objShell = CreateObject("WScript.Shell")
    Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
    swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.
    Set swbemServices = swbemLocator.ConnectServer("US1153SCCMC03", "root\sms\site_C03")
    sCollectionIDs = "ABC00052:ABC00054:ABC00053:ABC00055"
    mCollectionID = Split (sCollectionIDs, ":")

for i = Lbound(mCollectionID) to UBound(mCollectionID)
    query= "SELECT ResourceID FROM SMS_CM_Res_Coll_" & mCollectionID(i)   
    Set arrComputers = SWbemServices.ExecQuery(query)
    For Each objComputer In arrComputers
       RemoveCollectionMembership objComputer.ResourceID,mCollectionID(i)
    Next
Next
Set objCollection = Nothing
Set SWbemServices = Nothing
Set SWbemLocator = Nothing
'-*-------------------------------------------------------------------
Sub RemoveCollectionMembership(intresourceid,CollectionID)
 on error resume next
 set objCollection = SWbemServices.Get("SMS_Collection='" & CollectionID & "'")
 set ObjDirectRule = SWbemServices.Get("SMS_CollectionRuleDirect").SpawnInstance_
 ObjDirectRule.ResourceID = intresourceid
 ObjCollection.DeleteMembershipRule objDirectRule
End Sub