今天需要删除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/