批量刪除SCCM服務器上無用的計算機記錄

 今天需要刪除SCCM服務器上大量的客戶端計算機記錄,有一個txt的文件裏存放了這些要刪除的計算機名。如果要去集合裏一臺臺找這些機器刪除的話,那要累死人了,體力活。網上找了一下發現有批量刪除計算機記錄的腳本,這裏分享一下:

  1. '填入SCCM服務器名稱
  2. strSMSServer = "WISEFAQ2003"
  3. strSMSSiteCode = ""
  4.  
  5. Set objFSO = CreateObject("Scripting.FileSystemObject")
  6.  
  7. '填入del_list.txt的文件路徑
  8. If not objFSO.FileExists("C:\computer_lists\sms_deletion.txt") Then
  9.         WScript.Quit(99)
  10. End If
  11.  
  12. '填入del_list.txt的文件路徑
  13. Set ObjStream = objFSO.OpenTextFile("C:\computer_lists\sms_deletion.txt",1)
  14.  
  15. Set objLoc =  CreateObject("WbemScripting.SWbemLocator")
  16. Set objSMS= objLoc.ConnectServer(strSMSServer, "root\sms")
  17. Set Results = objSMS.ExecQuery _
  18.     ("SELECT * From SMS_ProviderLocation WHERE ProviderForLocalSite = true")
  19. For each Loc in Results
  20.     If Loc.ProviderForLocalSite = True Then
  21.         Set objSMS = objLoc.ConnectServer(Loc.Machine, "root\sms\site_" & _
  22.             Loc.SiteCode)
  23.         strSMSSiteCode = Loc.SiteCode
  24.     end If
  25. Next
  26.  
  27.  
  28. Do While Not ObjStream.AtEndOfStream
  29.         strComputer = ObjStream.ReadLine
  30.         
  31.         ' strComputer shouldn't be blank, if it is, there is something wrong with the input file.
  32.         If strComputer = ""        Then
  33.                 WScript.Quit
  34.         End If
  35.                 
  36.         'get the resource ID of the computer
  37.         bIsInSMS = False
  38.         intResourceID = GetResourceID(strComputer,bIsInSMS)
  39.  
  40.         ' we check to see if the computer is "pingable".
  41.         '  If it is, then perhaps we shouldn't be deleting it.
  42.         
  43.         
  44.         
  45.         '下面這一行是先判斷是否能夠ping的通,調用最下邊的Reachable函數。
  46.         If Not Reachable(strComputer) Then
  47.                 If bIsInSMS Then
  48.                         On Error Resume Next
  49.                         Set objResource = GetObject( "WinMgmts:\\" & strSMSServer & _
  50.                             "\root\SMS\site_" & strSMSSiteCode & _
  51.                             ":SMS_R_System.ResourceID=" & cstr(intResourceID))
  52.                         
  53.                         ' don't try and delete the computer if we couldn't get a handle to it.
  54.                     If Err.Number <> 0 Then
  55.                             wscript.echo "An SMS error occurred: " + Err.Description + " (" + cstr(Err.Number) + ") "& strComputer
  56.                     else
  57.                                 objResource.Delete_
  58.                                 wscript.echo "Deleted " & strComputer & "(" & intResourceID & ")"
  59.                                 WScript.sleep 5000
  60.                         End If
  61.                         On Error GoTo 0
  62.                 Else
  63.                         wscript.echo "Not Found in SMS: " & strComputer
  64.                 End If
  65.                         '這裏以下三行是如果能ping的通就做一下動作。
  66.                         Else 
  67.                   wscript.echo "Is alive: " & strComputer
  68.         End If
  69.  
  70.         On Error GoTo 0
  71. Loop
  72.  
  73. WScript.Quit
  74.  
  75. Function GetResourceID(strComputerName,bFoundInSms)
  76.     
  77.     bFoundInSms = False
  78.     Set colResourceIDs = objSMS.ExecQuery _
  79.         ("select ResourceID from SMS_R_System where Name = '" & _
  80.              strComputer & "'")
  81.     for Each objResID in colResourceIDs
  82.         GetResourceID = objResID.ResourceID
  83.         bFoundInSms = True
  84.     Next
  85. End Function
  86.  
  87. '檢查機器是否能夠ping通的函數
  88. Function Reachable(strComputer)
  89. '     On Error Resume Next
  90.  
  91.   Dim wmiQuery, objWMIService, objPing, objStatus
  92.   
  93.   wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
  94.   
  95.   Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  96.   Set objPing = objWMIService.ExecQuery(wmiQuery)
  97.   
  98.   For Each objStatus In objPing
  99.       If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
  100.           Reachable = False 'if computer is unreacable, return false
  101.       Else
  102.           Reachable = True 'if computer is reachable, return true
  103.       End If
  104.   Next
  105. End Function
複製代碼

這個腳本回去找txt文件裏的機器,做刪除操作,刪除前有一個判斷,如果機器名可以ping通,即不做刪除。也可以不做判斷,把那個IF給註釋掉就可以了。

原文地址:
http://blog.wisefaq.com/2010/03/ ... 2007-with-a-script/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章