ConfigMgr 2007 and removing a Computer from a Collection by script

I have to admit that it’s just really easy/ handy to create scripts to make life a bit easier. This also counts for this scenario… A customer wants to prevent, at all costs, that a computer can’t get re-imaged “by accident”. It already happened a few times that somebody by accident did a Clear Last PXE Advertisement on a Computer, or even on a Collection.

An easy solution for this scenario is to run a script at the end of a Task Sequence that will remove the Computer directly from the Collection. This makes sure that a computer can’t get re-imaged, as it’s not a member of the collection anymore. For this you can use the script from this post.

RemCompfrCollThe usage of this script is cscript <ScriptName>.vbs /CollectionID:[CollectionID] /ComputerName:[ComputerName]. Keep in mind that it needs to be run with an account that has enough rights in ConfigMgr. See also this picture for an example.

Option Explicit

DIM objSWbemLocator, objSWbemServices, ProviderLocation, Location, sSiteServerName
DIM sComputerName, sCollectionID, objCollection, colRuleSet, Rule, objArguments

‘=============================
‘ Check arguments
‘=============================
Set objArguments = Wscript.Arguments
If WScript.Arguments.Count = 2 Then
   sCollectionID = objArguments.Named.Item(“CollectionID”)
   sComputername = objArguments.Named.Item(“ComputerName”)
Else
   Wscript.Echo “Usage: RemoveComputerFromCollection.vbs /CollectionID:[CollectionID] /ComputerName:[ComputerName]”
   Wscript.Quit(0)
End If

‘=============================
‘ MAIN Script
‘=============================
sSiteServerName=”<SiteServerName>”
ConnectToSMSProvider(sSiteServerName)

Set objCollection = objSWbemServices.Get(“SMS_Collection='” & sCollectionID & “‘”) 
colRuleSet = objCollection.CollectionRules
For Each Rule In colRuleSet
    If Rule.Path_.Class = “SMS_CollectionRuleDirect” Then 
        If LCase(Trim(Rule.RuleName)) = LCase(Trim(sComputerName)) Then 
            objCollection.DeleteMembershipRule Rule
            Wscript.Echo “Succesfully removed ” & sComputerName & ” from collection: ” & sCollectionID
        End If
    End If
Next

‘=============================
‘ Sub Routine to Connect to the SMS Provider
‘=============================
Sub ConnectToSMSProvider(SiteServerName)
   Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
   Set objSWbemServices = objSWbemLocator.ConnectServer(SiteServerName, “root\sms”)
   Set ProviderLocation = objSWbemServices.InstancesOf(“SMS_ProviderLocation”)
   For Each Location In ProviderLocation
      If Location.ProviderForLocalSite = True Then
         Set objSWbemServices = objSWbemLocator.ConnectServer(Location.Machine, “root\sms\site_” + Location.SiteCode)
      End If
   Next
End Sub

WScript.Quit(0)

13 thoughts on “ConfigMgr 2007 and removing a Computer from a Collection by script”

  1. Nice script. I am using it. Thanks a lot for sharing all this nice stuff.
    I seem to have an issue sometimes where the 11171 event gets logged in the eventlog, but there is no WSH entry in eventlog stating that the computer has been removed from the collection.
    This results in installing the pc a second time. after which the WSH message IS mentioned in the log.
    Now the computer account is removed from the collection. Always seem to work the second time.
    This problem seems to occur on specific machines. other machines went ok the first time
    Any ideas?

    Reply
  2. Hi Peter , I want to delete machine name from collection using script in the TS sequence. I have used your script and followed the same step but TS . But the TS is getting stuck at the time of machine deletion when i used to run the script with the a/c from which i loggon on with the SCCM -server, Please give some suggetions

    Reply
  3. I pass the name of SCCM server through the variable sSiteServerName = ” SCCM-01″. I made the package of script in the same way as simple package ismade from SCCM. I create a new package of script and crate its distribution pt and update it. After then include the script in the last step of TS after OSD by adding a run command line . use the command : cscript filename.vbs /CollectionID:[collectionid] /ComputerName:[Computername] and ckeck the option to run the script with user a/c from which i loggon SCCM server.

    The same script run successfully by manually and machine get deleted from collection

    Reply
  4. Hi peter,

    How it will take collection ID ? where we need to provide collection ID
    Can we use it for multiple computers in a collection ?
    what is this below code

    “sCollectionID = objArguments.Named.Item(“CollectionID”)
    sComputername = objArguments.Named.Item(“ComputerName”)”

    I am plaining to delete the obsolute machines from a collection regulerely running this script using Task sheduler how this script will meet my requirement
    can you help me

    Reply
    • Hi Shinu,

      The usage is also mentioned in the code. You’ve got to specify the collection ID during starting the script, like this RemoveComputerFromCollection.vbs /CollectionID:[CollectionID] /ComputerName:[ComputerName]

      Peter

      Reply

Leave a Reply to Peter van der Woude Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.