This script will read the machine names from a text file called MachineList.Txt and delete the machine(s) from SMS as well as list the machines Resource ID(s) for the deleted machines.
Note: Update your collection membership in the SMS console to reflect the changes.
Vbs Script:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "Site Server"
objExcel.Cells(1, 3).Value = "Site Code"
objExcel.Cells(1, 4).Value = "Resource ID"
objExcel.Cells(1, 5).Value = "Status"
strServer = InputBox("Enter Site Server Name")
strSiteCode = InputBox("Enter Site Code")
Set Fso = CreateObject("Scripting.FileSystemObject")
Set InputFile = fso.OpenTextFile("MachineList.Txt")
Do While Not (InputFile.atEndOfStream)
strComputer = InputFile.ReadLine
Set locator = CreateObject("WbemScripting.SWbemLocator")
Dim WbemServices1
Set WbemServices1 = locator.ConnectServer( strServer,"root\SMS\site_" & strSiteCode)
objExcel.Cells(intRow, 1).Value = UCase(strComputer)
objExcel.Cells(intRow, 2).Value = UCase(strServer)
objExcel.Cells(intRow, 3).Value = UCase(strSiteCode)
ResID = getResID(strComputer, WbemServices1)
If ResID = Empty Then
objExcel.Cells(intRow, 4).Value = "Unable To Detect"
Else
objExcel.Cells(intRow, 4).Value = ResID
Set sResource = WbemServices1.Get("SMS_R_System='" & ResID & "'")
sResource.Delete_
If Err = 0 Then
objExcel.Cells(intRow, 5).Value = "Removed"
objExcel.Cells(intRow, 5).Value = "Error"
End If
intRow = intRow + 1
Set sResource = NOTHING
loop
Function GetResID(strComputer, oWbem)
Dim strQry
strQry = "Select ResourceID from SMS_R_System where Name=" & "'" & strComputer & "'"
Set objEnumerator = oWbem.ExecQuery(strQry)
If Err <> 0 Then
GetResID = 0
Exit Function
For Each objInstance in objEnumerator
For Each oProp in objInstance.Properties_
GetResID = oProp.Value
Next
End Function
objExcel.Range("A1:E1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True
objExcel.Cells.EntireColumn.AutoFit
Set oobjExcel = Nothing
Set Fso = Nothing
Set Inputfile = Nothing
Set Locator = Nothing
Set WbemServices1 = Nothing
Set objEnumerator = Nothing
MsgBox "Done"
This Vbs script will allow you to enter a machine name and delete it from the SMS database. It will prompt