【问题标题】:Script that scans IP range and gets user information to Excel sheet. Recycles information扫描 IP 范围并将用户信息获取到 Excel 工作表的脚本。回收信息
【发布时间】:2014-09-01 05:04:57
【问题描述】:

我需要有关 VBS 脚本的帮助,该脚本可生成包含特定用户信息的 Excel 工作表。

它有效...有点。问题是它似乎在回收产生不准确结果的信息。任何人都知道当没有可用信息时,我将如何让 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

【问题讨论】:

  • 很难告诉你 sn-p,但可能是你没有在用新数据填充对象之前重新设置它吗?如果您不清除旧数据,它将被保留。

标签: excel vbscript ip network-scan


【解决方案1】:

您的 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")

【讨论】:

    【解决方案2】:

    您使用 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 确保处理特定问题(除零、ping 失败),并报告所有其他异常,以便改进程序。

    further food for thought

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 1970-01-01
      • 2014-07-01
      • 2022-07-06
      • 2020-05-05
      • 2020-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多