2013-04-18 3 views
0

Excel 시트의 목록에있는 컴퓨터에서 프린터를 찾고 WMI를 통해 찾은 VBScript에 문제가 있습니다. IP 주소 이름을 통해 일치시킨 다음 설치할 수있는 배치 파일을 작성합니다. 내 문제는 내가 꺼져있는 컴퓨터가있을 때 462 오류가 발생했다가 지워지지 만 이전 컴퓨터의 프린터가 다시 쓰여지는 것입니다. 나는 이것에 아주 새롭다 그래서 나가 다만 진짜로 여기에서 무언가를 놓치는 경우에 나는 확실하지 않다.오류가있는 컴퓨터 건너 뛰기

Batch = "printerOutput.txt" 
Const ForWriting = 2 'Set to 8 for appending data 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile(Batch, ForWriting) 

On Error Resume Next 

Dim printerDictionary 'Create Printer dictionary of names and IP addresses 
     Set printerDictionary = CreateObject("Scripting.Dictionary") 
      printerDictionary.Add "Printer","xxx.xxx.xxx.xxx" 


Set objExcel_1 = CreateObject("Excel.Application") 
' Statement will open the Excel Workbook needed. 

Set objWorkbook = objExcel_1.Workbooks.Open _ 
("x\p.xls") 

If Err.Number <> 0 Then 
    MsgBox "File not Found" 
    Wscript.Quit 

End If 
'Checks for errors 
f = 1 'Sets variable that will loop through Excel column 


Do 

' Msgbox f,, "Begining of Do Loop" 


    strComputer = objExcel_1.Cells(f, 1).Value 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")  
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer") 
    For Each objPrinter in colPrinters 'For ever objPrinter found in the computers WMIService 


    If Err.Number = 0 Then 

      objFile.WriteLine Err.Number 
      If InStr(ObjPrinter.PortName,".") = 4 then 'If the printers IP port name is written like 128.xxx.xxx.xxx 

       'MsgBox ObjPrinter.Name & " " & ObjPrinter.PortName,, "IfStatement" 
       PrtDict ObjPrinter.PortName, StrComputer 

      ElseIf InStr(ObjPrinter.PortName,"_") = 3 Then 'If the printers IP port name is written like IP_128.xxx.xxx.xxx 
       cleanIP = GetIPAddress(objPrinter.PortName) 'Clean IP 

       PrtDict cleanIP, StrComputer   
      End If 

    Else 

     objFile.WriteLine "REM " & strComputer & " - Error: " & err.number 
     Err.Clear 

    End If 

    Next 



    f = f + 1 


Loop Until objExcel_1.Cells(f, 1).Value = "" 

objExcel_1.ActiveWorkbook.Close 
ObjExcel_1.Quit 

Function PrtDict(PrtMn, CMP) 'Loops through the dictionary to find a match from the IP address found 


     For Each x in printerDictionary 
      'MsgBox PrtMn & "=" & printerDictionary.Item(x),,"InPtrDict" 
      If printerDictionary.Item(x) = PrtMn Then 

       objFile.WriteLine "psexec -u \%1 -p %2 " & CMP & " path\" & x & ".bat" 

      End If 
     Next 

End Function 
'100 
Function GetIPAddress(Address) 'For cleaning up IP address with names like IP_128.xxx.xxx.xxx 

    IPtext = InStr(Address,"_") 
    IPaddress = len(Address) - IPtext 

GetIPAddress = Right(Address,IPaddress) 
End Function 

답변

0

은 무슨 일이 있습니다 : 사용하지 않도록 결코 때문에

  1. On Error Resume Next

    이 스크립트의 나머지 부분에 대한 오류 처리 (또는 오히려 오류 억제를) 할 수 있습니다.

  2. Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

    이 명령이 실패, strComputer에 연결할 수 없기 때문에. 오류로 인해 변수 Err이 설정되고 objWMIService은 이전 값을 유지합니다.

  3. Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer")

    이 명령은 성공하고 objWMIService 아직도 그 컴퓨터에 참조하기 때문에, 이전 컴퓨터에서 프린터 목록을 재-읽습니다. colPrinters 이전 컴퓨터 (다시), 중 & hellip에서 프린터로 채워 얻었 기 때문에

  4. For Each objPrinter in colPrinters

    스크립트는 루프에 들어갑니다;

  5. If Err.Number = 0 Then ... Else ... End If

    중 & hellip; Err여전히 설정되어 있기 때문에하지만, 스크립트 오류가보고 및 취소되어 Else 지점으로 이동 :

    objFile.WriteLine "REM " & strComputer & " - Error: " & err.number 
    Err.Clear 
    
  6. 그런 다음 스크립트는 루프의 다음 반복으로 간다. Err이 지워졌으므로 colPrinters에있는 나머지 프린터가 정상적으로 처리되고 있습니다. On Error Resume Next 글로벌

모든 악의 뿌리입니다. 이러지 마. 적.

On Error Resume Next을 반드시 사용해야하는 경우 로컬에서 오류 처리를 사용하도록 설정하고 실제 오류 처리 코드를 제자리에두고 오류 처리를 즉시 해제 할 수 있습니다. 귀하의 경우에는 다음과 같이 구현할 수 있습니다.

... 
Do 
    strComputer = objExcel_1.Cells(f, 1).Value 
    On Error Resume Next 
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
    If Err Then objFile.WriteLine "REM " & strComputer & " - Error: " & err.number Set objWMIService = Nothing End If On Error Goto 0 If Not objWMIService Is Nothing Then 
    Set colPrinters = objWMIService.ExecQuery("Select * From Win32_Printer") 
    For Each objPrinter in colPrinters 
     ... 
    Next 
    f = f + 1 
    End If 
Loop Until objExcel_1.Cells(f, 1).Value = "" 
...