none
Need script to remove machines from multiple collections

    Question

  • Environment : SCCM 2007

    I need a script (VB/Powershell/sql) to remove machines from multiple collection. For example,

    MachineABC needs to be removed from Collection1, Collection2, collecton3 etc.

    it is really difficult to go to each collection and remove the machine manually. Thanks for your help.

    Monday, June 17, 2013 7:43 PM

All replies

  • Here's a VBScript that may help you. Just change the Sitecode and Server. When calling the sub just put the CollID and System Name to remove from collection.

    On Error Resume Next  
     
    Dim oLocator, oSWbemServices, oCollection  
    Dim sComputerName, ConfigMgrServer, ConfigMgrSiteCode, RuleSet  
    
    ConfigMgrServer = "CM07PS1" ' Server Name
    ConfigMgrSiteCode = "PS1" ' Your SiteCode
    
    
    DeleteComputerFromCollection "PS10000D", "WINXP" ' Enter Collection ID and Computer Name to remove from collection
    DeleteComputerFromCollection "PS10000E", "WINXP" ' Enter Collection ID and Computer Name to remove from collection
    DeleteComputerFromCollection "PS10000F", "WINXP" ' Enter Collection ID and Computer Name to remove from collection
    DeleteComputerFromCollection "PS10000G", "WINXP" ' Enter Collection ID and Computer Name to remove from collection
     
    Sub DeleteComputerFromCollection (CollID, ComputerName)
    
    	Set oLocator = CreateObject("WbemScripting.SWbemLocator")  
    	oLocator.Security_.AuthenticationLevel = 6 
    	Set oSWbemServices = oLocator.ConnectServer(ConfigMgrServer, "root\sms\site_" & ConfigMgrSiteCode)
    	Set oCollection = oSWbemServices.Get("SMS_Collection='" & CollID & "'")  
    	 
    	RuleSet = oCollection.CollectionRules  
    
    	For Each Rule In RuleSet  
    		If Rule.Path_.Class = "SMS_CollectionRuleDirect" Then  
    			If LCase(Trim(Rule.RuleName)) = LCase(Trim(ComputerName)) Then
    				oCollection.DeleteMembershipRule Rule ' Delete Computer From Collection
    				If err.number = 0 Then Wscript.Echo "Successfully removed " & ComputerName & " from " & CollID
    			End If  
    		End If  
    	Next  
    	 
    End Sub
     
    WScript.Quit(err.number) 


    Justin Chalfant | My Blog | LinkedIn | Please mark as helpful/answer if this resolved your post

    Tuesday, June 18, 2013 4:05 AM