How To Move Computers Between SCCM 2007 Collections
At the office we use Microsoft System Center Configuration Manager (SCCM) 2007 to deploy Microsoft Windows Vista to desktop computers. We have an SCCM collection called PUSH-NOW, which, as the name suggests, is used to immediately push the operating system to all collection members. However, sometimes it's necessary to delay the deployment of the operating system to outside of business hours (so as not to interrupt our hard working users). To address this requirement, I created a second collection named PUSH-LATER. I then use a scheduled task, which is configured to execute daily at 7PM, to move computers from the PUSH-LATER collection to the PUSH-NOW collection.
The PUSH-LATER collection is not linked to an advertisement; it is simply used as a temporary placeholder. Each day, the computers that need to be re-imaged that night with Windows Vista are assigned to the PUSH-LATER collection. At 7PM the scheduled task moves the computers into the PUSH-NOW collection, where they fall under the scope of an advertisement that forces an immediate deployment of Windows Vista.
The script used to move computers between the two collections is listed below. It is assumed that the script will execute under the context of an account with the permissions required to modify collections, and that all collection members are assigned using direct membership rules. To use the script, you will need to set the SOURCE_COLLECTION and TARGET_COLLECTION variables to the relevant collection identifiers, and SMS_SERVER to the name of the SCCM server.
SOURCE_COLLECTION = "XXX0001"
TARGET_COLLECTION = "XXX0002"
SMS_SERVER = "SERVERNAME"
Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oService = oLocator.ConnectServer(SMS_SERVER, "root\sms")
Set oProviderLoc = oService.InstancesOf("SMS_ProviderLocation")
For Each oLocation In oProviderLoc
If oLocation.ProviderForLocalSite = True Then
Set oService = oLocator.ConnectServer(oLocation.Machine, "root\sms\site_" + oLocation.SiteCode)
End If
Next
Set oSourceCollection = oService.Get("SMS_Collection.CollectionID='" & SOURCE_COLLECTION & "'")
Set oTargetCollection = oService.Get("SMS_Collection.CollectionID='" & TARGET_COLLECTION & "'")
If Not IsNull(oSourceCollection.CollectionRules) Then
For Each Rule In oSourceCollection.CollectionRules
Set NewResources = oService.ExecQuery("SELECT ResourceId FROM SMS_R_System WHERE NetbiosName ='" & Rule.RuleName & "'")
ResourceID = 0
For Each Resource In NewResources
ResourceID = Resource.ResourceID
Next
If ResourceID <> 0 Then
Set DirectRule = oService.Get("SMS_CollectionRuleDirect").SpawnInstance_()
DirectRule.ResourceClassName = "SMS_R_System"
DirectRule.ResourceID = ResourceID
DirectRule.RuleName = Rule.RuleName
WScript.Echo "Adding rule: " & Rule.RuleName
oTargetCollection.AddMembershipRule DirectRule, SMSContext
oTargetCollection.RequestRefresh False
End If
oSourceCollection.DeleteMembershipRule Rule
Next
End If