Below is a script which will delete a list of computers from SMS 2003.  And quite possibly SCCM 2007 as well (not tested).  It was originally taken from the SMS 2003 Recipe Book, modified by Paul Town, and further improved by me.
The changes I made were:

  • the script will read a text file of computers to be deleted, and loop though them.
  • ping the computer to be deleted, and if it’s “pingable”, don’t delete it.
  • fixed a bug with the WMI GetObject call.

strSMSServer = "WISEFAQ2003"
strSMSSiteCode = ""

Set objFSO = CreateObject("Scripting.FileSystemObject")
If not objFSO.FileExists("C:\computer_lists\sms_deletion.txt") Then
End If

Set ObjStream = objFSO.OpenTextFile("C:\computer_lists\sms_deletion.txt",1)
Set objLoc =  CreateObject("WbemScripting.SWbemLocator")
Set objSMS= objLoc.ConnectServer(strSMSServer, "root\sms")
Set Results = objSMS.ExecQuery _
("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")

For each Loc in Results
If Loc.ProviderForLocalSite = True Then
Set objSMS = objLoc.ConnectServer(Loc.Machine, “root\sms\site_” & _
strSMSSiteCode = Loc.SiteCode
end If

Do While Not ObjStream.AtEndOfStream
strComputer = ObjStream.ReadLine
' strComputer shouldn't be blank, if it is, there is something wrong with the input file.

If strComputer = “”    Then
End If

‘get the resource ID of the computer
bIsInSMS = False
intResourceID = GetResourceID(strComputer,bIsInSMS)
   ' we check to see if the computer is "pingable".
'  If it is, then perhaps we shouldn't be deleting it.

If Not Reachable(strComputer) Then
If bIsInSMS Then
On Error Resume Next
Set objResource = GetObject( “WinMgmts:\\” & strSMSServer & _
“\root\SMS\site_” & strSMSSiteCode & _
“:SMS_R_System.ResourceID=” & cstr(intResourceID))
‘ don’t try and delete the computer if we couldn’t get a handle to it.
If Err.Number <> 0 Then
wscript.echo “An SMS error occurred: ” + Err.Description + ” (” + cstr(Err.Number) + “) “& strComputer
wscript.echo “Deleted ” & strComputer & “(” & intResourceID & “)”
WScript.sleep 5000
End If
On Error GoTo 0
wscript.echo “Not Found in SMS: ” & strComputer
End If
wscript.echo “Is alive: ” & strComputer
End If

On Error GoTo 0


Function GetResourceID(strComputerName,bFoundInSms)
bFoundInSms = False
Set colResourceIDs = objSMS.ExecQuery _
("select ResourceID from SMS_R_System where Name = '" & _
strComputer & "'")
for Each objResID in colResourceIDs
GetResourceID = objResID.ResourceID
bFoundInSms = True
End Function

Function Reachable(strComputer)
‘     On Error Resume Next
Dim wmiQuery, objWMIService, objPing, objStatus
wmiQuery = “Select * From Win32_PingStatus Where Address = ‘” & strComputer & “‘”
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
Set objPing = objWMIService.ExecQuery(wmiQuery)
For Each objStatus In objPing

If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
Reachable = False ‘if computer is unreachable, return false
Reachable = True ‘if computer is reachable, return true
End If

End Function

You can download the code sample here.

Update Sept 2010:
there is a new version of the Reachable function here.
Bookmark and Share

Comments are closed.