2014-07-10 9 views
0

특정 사용자 정보가있는 Excel 시트를 생성하는 VBS 스크립트에 대한 도움이 필요합니다.IP 범위를 검색하고 사용자 정보를 Excel 시트로 가져 오는 스크립트입니다. 재활용 정보

그것은 작동합니다 ... 일종의. 문제는 부정확 한 결과를 생산하는 정보를 재활용하는 것처럼 보입니다. 아무도 정보가 없으면 스크립트 문서를 공백으로 두는 방법에 대해 알고 있습니까? 나는 그것이 가능하다는 것을 안다. 올바른 방향으로 조금 움직여 라.

감사합니다.

On Error Resume Next 

Dim FSO 
Dim objStream 

Const TriStateFalse = 0 
Const FILE_NAME = "Users.csv" 

Set FSO = CreateObject("Scripting.FileSystemObject") 

Set objStream = FSO.CreateTextFile(FILE_NAME, _ 
True, TristateFalse) 

strSubnetPrefix = "192.168.1." 
intBeginSubnet = 1 
intEndSubnet = 254 

For i = intBeginSubnet To intEndSubnet 
strComputer = strSubnetPrefix & i 
    'strcomputer = inputbox("Enter Computer Name or IP") 
    if strcomputer = "" then 
     wscript.quit 
    else 

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _ 
     ("select * from Win32_PingStatus where address = '" & strcomputer & "'") 
    For Each objStatus in objPing 
     If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
      'request timed out 
      'msgbox(strcomputer & " did not reply" & vbcrlf & vbcrlf & _ 
        '"Please check the name and try again") 
     else 

      set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _ 
     strComputer & "\root\cimv2") 
      Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") 
      For Each objComputer in colSettings 
            objStream.WriteLine objComputer.name & "," & objcomputer.username & "," & objcomputer.domain _ 
       & "," & strcomputer 
       'msgbox("System Name: " & objComputer.Name & vbcrlf & "User Logged in : " & _ 
       'objcomputer.username & vbcrlf & "Domain: " & objComputer.Domain) 
      Next 
     end if 
    next 
    end if 
Next 

Msgbox("Done Collecting") 

set objwmiservice = nothing 
set colsettings = nothing 
set objping = nothing 
+0

스 니펫에서 전달하기가 어렵지만 새로운 데이터를 채우기 전에 개체를 다시 설정하지 않았습니까? 이전 데이터를 지우지 않으면 유지됩니다. –

답변

0

EVIL 글로벌 On Error Resume Next을 사용합니다. 즉, 모든 오류는 무시되거나 숨겨지고 스크립트는 정의되지 않은 모든 실제 목적을 위해 계속됩니다 (다소 행복하게). 데모 스크립트

Option Explicit 

Dim a : a = Array(1,0,2) 

Bad a 
Good a 

Sub Bad(a) 
    Dim i, n 
    On Error Resume Next 
    For i = 0 To UBound(a) 
     n = 4712/a(i) 
     WScript.Echo "Bad", i, a(i), n 
    Next 
End Sub 

Sub Good(a) 
    Dim i, n 
    For i = 0 To UBound(a) 
     On Error Resume Next 
     n = 4712/a(i) 
     If Err.Number Then n = "value to use in case of error" 
     On Error GoTo 0 
     WScript.Echo "Good", i, a(i), n 
    Next 
End Sub 

출력 :

cscript oern.vbs 
Bad 0 1 4712 
Bad 1 0 4712 <--- assignment failed, 'old' value of n retained, no clue about problem 
Bad 2 2 2356 
Good 0 1 4712 
Good 1 0 value to use in case of error 
Good 2 2 2356 

엄격 지역 OERN는 특정 문제 (0으로 나누기는, 핑 오류가) 처리되어 있는지 확인하게하고,보고다른 모든 예외 프로그램을 개선 할 수 있습니다.

further food for thought

0

귀하의 WMI 호출 변수는 다시 설정하기 전에 아무것도 재설정해야합니다. 이 스크립트는 더 잘 작동합니다.

On Error Resume Next 

Dim FSO 
Dim objStream 

Const TriStateFalse = 0 
Const FILE_NAME = "Users.csv" 

Set FSO = CreateObject("Scripting.FileSystemObject") 

Set objStream = FSO.CreateTextFile(FILE_NAME, _ 
True, TristateFalse) 

strSubnetPrefix = "192.168.1." 
intBeginSubnet = 1 
intEndSubnet = 254 

For i = intBeginSubnet To intEndSubnet 
strComputer = strSubnetPrefix & i 
    'strcomputer = inputbox("Enter Computer Name or IP") 
    if strcomputer = "" then 
     wscript.quit 
    else 

    Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _ 
     ("select * from Win32_PingStatus where address = '" & strcomputer & "'") 
    For Each objStatus in objPing 
     If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then 
      'request timed out 
      'msgbox(strcomputer & " did not reply" & vbcrlf & vbcrlf & _ 
        '"Please check the name and try again") 
     else 

      set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & _ 
     strComputer & "\root\cimv2") 
      Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem") 
      For Each objComputer in colSettings 
            objStream.WriteLine objComputer.name & "," & objcomputer.username & "," & objcomputer.domain _ 
       & "," & strcomputer 
       'msgbox("System Name: " & objComputer.Name & vbcrlf & "User Logged in : " & _ 
       'objcomputer.username & vbcrlf & "Domain: " & objComputer.Domain) 
      Next 
      set objwmiservice = nothing 
      set colsettings = nothing 
     end if 
    next 
    end if 
    set objping = nothing 
Next 

Msgbox("Done Collecting")