Thanks go to the guys over at ccmexec.com for the soluttion to this one.
They posted the following script that deletes a PC from specified collections when a Task Sequences successfully completes. This allows you to set the Task Sequence Advertisment to always rerun, and prevent endless rebuild loops..
Option Explicit
' Constants for type of event log entry
const EVENTLOG_INFORMATION = 4
Dim Args
Dim swbemLocator, SWbemServices, objCollection, oProviderLocation, oLocation
Dim strComputerName, arrComputers, objComputer, sCollectionIDs
Dim objDirectRule
Dim strmessage, objshell
Dim seventlog, sClearPxeflag
On Error Resume Next
'CollectionIDs from which to remove the computer
'Should an eventlog entry be generated, set Seventlog=1
sEventlog = "1"
sCollectionIDs = "00100053:0010004A:00100069"
'------------------------------------------------------------
'Get Command Line arguments
Set args = WScript.Arguments
strComputername = args.Item(0)
If strComputerName = NULL then
wscript.quit
End if
'------------------------------------------------------------
'Main script
set objShell = CreateObject("WScript.Shell")
Set swbemLocator = CreateObject("WbemScripting.SWbemLocator")
swbemLocator.Security_.AuthenticationLevel = 6 'Packet Privacy.
Set swbemServices = swbemLocator.ConnectServer(".", "root\SMS")
Set oProviderLocation = swbemServices.InstancesOf("SMS_ProviderLocation")
For Each oLocation In oProviderLocation
If oLocation.ProviderForLocalSite = True Then
Set swbemServices = swbemLocator.ConnectServer(oLocation.Machine, "root\sms\site_" + oLocation.SiteCode)
End If
Next
Set arrComputers = SWbemServices.ExecQuery("select * from SMS_R_System where Name='" & strComputerName & "' and Obsolete = 0")
For Each objComputer In arrComputers
RemoveCollectionMembership objComputer.ResourceID
'Write to eventlog if Seventlog = 1
If Seventlog = "1" then
strMessage = strcomputername & " will be removed from the following collection ID's " & scollectionids
objShell.LogEvent EVENTLOG_INFORMATION, strMessage
End IF
Next
Set objCollection = Nothing
Set SWbemServices = Nothing
Set SWbemLocator = Nothing
Wscript.Quit
'------------------------------------------------
Sub RemoveCollectionMembership(intresourceid)
on error resume next
Dim mCollectionID, i
mCollectionID = Split (sCollectionIDs, ":")
for i = Lbound(mCollectionID) to UBound(mCollectionID)
Set objCollection = SWbemServices.Get("SMS_Collection='" & MCollectionID(i) & "'")
Set ObjDirectRule = SWbemServices.Get("SMS_CollectionRuleDirect").SpawnInstance_
ObjDirectRule.ResourceID = intresourceid
ObjCollection.DeleteMembershipRule objDirectRule
next
End Sub
'------------------------------------------------
WScript.Quit(0)
Simply edit the following lines, replacing the IDs with the IDs of your Collections and save it as something like remove.vbs
Then, create a System Filter Rule in ConfigMgr with the following info:
General Tab
Component: Task Sequence Manager
Message ID: 11171
Actions Tab
Report to the Event Log
Run a program: cscript.exe e:\sccmtools\remove.vbs %msgsys
And thats it..
Have fun!
Comments