Thursday, June 18, 2015

Mass deleting Obsolete SCCM Clients

I would believe that every SCCM Admin would at some point of time need to cleanup obsolete SCCM Client objects from the SCCM Sites.
Most of the time when this is needed, all you will be provided is a list of the machines that are no longer in Active Directory and these SCCM Clients has to be purged from SCCM.
For this purpose, the below VBS will do just that

ON ERROR RESUME NEXT
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
I = 0

strServer=InputBox("Enter Site Server Nmae",,"SCCM-Pri-Site-Server")
strSiteCode=InputBox("Enter Site Code",,"P01")


Set fso = CreateObject("Scripting.FileSystemObject")
strfolder = fso.GetParentFolderName(wscript.ScriptFullName) 

'You may replace obsolete.txt with any text file name you prefer. The contents of this file is used to list out the SCCM clients that are to be purged.
FileName = strfolder & "\Obsolete.txt"
'Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = fso.OpenTextFile(Filename,ForReading)
strmachine = objFile.ReadAll
objFile.Close


arrcomp = split(strmachine, vbCrLf)
if isnull(strmachine) then
wscript.echo "No Machine info"
Else
For each strmachine in arrcomp
strcomputer = strmachine
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set WbemServices1 = locator.ConnectServer(strServer,"root\SMS\site_" & strSiteCode)
ResID = getResID(strComputer, WbemServices1)


Set sResource = WbemServices1.Get("SMS_R_System='" & ResID & "'")
sResource.Delete_
Set sResource = Nothing
I = I +1
next

wscript.echo "Housekeep Done! A total of " & I & " obsolete SCCM records were deleted"
End if

Function getResID(strComputer, oWbem)
strQry = "Select ResourceID from SMS_R_System where Name=" & "'" & strComputer & "'"
Set objEnumerator = oWbem.ExecQuery(strQry)
If Err <> 0 Then
GetResID = 0
Exit Function
End If
For Each objInstance in objEnumerator
For Each oProp in objInstance.Properties_
GetResID = oProp.Value
Next
Next
Set objEnumerator = Nothing
End Function

No comments:

Post a Comment