今天需要刪除SCCM服務器上大量的客戶端計算機記錄,有一個txt的文件裏存放了這些要刪除的計算機名。如果要去集合裏一臺臺找這些機器刪除的話,那要累死人了,體力活。網上找了一下發現有批量刪除計算機記錄的腳本,這裏分享一下:
- '填入SCCM服務器名稱
- strSMSServer = "WISEFAQ2003"
- strSMSSiteCode = ""
- Set objFSO = CreateObject("Scripting.FileSystemObject")
- '填入del_list.txt的文件路徑
- If not objFSO.FileExists("C:\computer_lists\sms_deletion.txt") Then
- WScript.Quit(99)
- End If
- '填入del_list.txt的文件路徑
- 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_" & _
- Loc.SiteCode)
- strSMSSiteCode = Loc.SiteCode
- end If
- Next
- 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
- WScript.Quit
- 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.
- '下面這一行是先判斷是否能夠ping的通,調用最下邊的Reachable函數。
- 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
- else
- objResource.Delete_
- wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
- WScript.sleep 5000
- End If
- On Error GoTo 0
- Else
- wscript.echo "Not Found in SMS: " & strComputer
- End If
- '這裏以下三行是如果能ping的通就做一下動作。
- Else
- wscript.echo "Is alive: " & strComputer
- End If
- On Error GoTo 0
- Loop
- WScript.Quit
- 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
- Next
- End Function
- '檢查機器是否能夠ping通的函數
- 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 unreacable, return false
- Else
- Reachable = True 'if computer is reachable, return true
- End If
- Next
- End Function
這個腳本回去找txt文件裏的機器,做刪除操作,刪除前有一個判斷,如果機器名可以ping通,即不做刪除。也可以不做判斷,把那個IF給註釋掉就可以了。
原文地址:
http://blog.wisefaq.com/2010/03/ ... 2007-with-a-script/