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.
The 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.
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”)
Wscript.Echo “Usage: RemoveComputerFromCollection.vbs /CollectionID:[CollectionID] /ComputerName:[ComputerName]”
‘ MAIN Script
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
Wscript.Echo “Succesfully removed ” & sComputerName & ” from collection: ” & sCollectionID
‘ Sub Routine to Connect to the SMS Provider
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)
13 thoughts on “ConfigMgr 2007 and removing a Computer from a Collection by script”
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
I’m sorry but you’ve already lost me with event 11171?? Are you sure about this one??
Are you familiar with the error
40,4 swbenlocator: the rpc server is unavaileble ?
That can be almost everything… Firewall, Security Rights, Network connection, etc..
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
I assume you filled in the correct SiteServerName with the variable sSiteServerName. If so then how did you configure the package and the Task Sequence -step (please provide all settings).
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
I assume you use direct memberships, as this script won’t work otherwise… I also noticed a space in your site server name sSiteServerName = ” SCCM-01″…
I didn’t use direct membership and there is no space in SiteServer name.
If you didn’t use direct memberships then that’s the problem, as this script will only delete a direct membership -rule (from the specified machine in the specified collection).
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
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]