【问题标题】:Using VBScript to return ipv4 addresses as variables使用 VBScript 将 ipv4 地址作为变量返回
【发布时间】:2017-02-27 11:29:52
【问题描述】:

我正在尝试使用.Exec 拉回机器的 IP 配置。然后,从命令的给定输出中,我想返回所有无线和以太网 LAN IPv4 地址或“媒体已断开连接”(如果不存在)。我非常坚持如何解决这个问题,因为用户通常可以拥有多个 NIC。我可以得到输出,但是我不确定如何遍历 ipconfig 的结果来存储我需要的信息。

Set objShell = CreateObject("WScript.Shell")
StrIPConfig = "ipconfig /all"
Set IPConfig = objShell.Exec(StrIPConfig)

strText = ""

Do While Not IPConfig.StdOut.AtEndOfStream
    strText = IPConfig.StdOut.Readline
    If InStr(strText, "Wireless Network Connection") Then
        strWLAN = IPConfig.StdOut.Readline +2
        WScript.Echo strWLAN
    End If
Loop

Do While Not IPConfig.StdOut.AtEndOfStream
    strText = IPConfig.StdOut.Readline
    If InStr(strText, "Local Area Connection") Then
        strLAN = IPConfig.StdOut.Readline +2
        WScript.Echo strWLAN
    End If
Loop

必须通过 WMI Windows 批处理文件或 WMI VBscript 或使用 Exec 的 VBScript 完成,如上所述。不幸的是,PowerShell 不是一种选择。

【问题讨论】:

标签: vbscript wmi


【解决方案1】:

使用WMI(特别是Win32_NetworkAdapterConfiguration 类)而不是炮击:

Set wmi = GetObject("winmgmgts://./root/cimv2")

qry = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True"

For Each nic In wmi.ExecQuery(qry)
  For Each addr In nic.IPAddress
    WScript.Echo addr
  Next
Next

【讨论】:

  • 有一个错字:它是winmgmts 而不是winmgmgts
【解决方案2】:

我使用由 VBScript 包装的 WMI 脚本改编了另一个论坛的答案。它目前将信息存储为字符串,但现在很容易适应:

strWLAN = ""
strLAN = ""
strComputer = "."

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPAdapterSet = objWMIService.ExecQuery("Select AdapterType, NetConnectionID, MACAddress from Win32_NetworkAdapter WHERE NetConnectionID LIKE 'Wireless network%'")


For Each IPConfig in IPAdapterSet
 Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "' ")
   For Each IPConf in IPConfigSet
    if NOT strWLAN="" then
      strWLAN = strWLAN & vbCrLf
    end if
    strWLAN = strWLAN  & " " & IPConfig.NetConnectionID & " "
    If Not IsNull(IPConf.IPAddress) Then
     For i = LBound(IPConf.IPAddress) to UBound(IPConf.IPAddress)
      If Not ((Instr(IPConf.IPAddress(i), ":") > 0) or (Instr(IPConf.IPAddress(i), "none") > 0)) Then
    if i=0 then
      strWLAN = strWLAN & " " & IPConf.IPAddress(i)
    else
      strWLAN = strWLAN & ", " & IPConf.IPAddress(i) 
    end if
      End If
     Next
    End If
   next
Next



Set objWMIService2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPAdapterSet2 = objWMIService2.ExecQuery("Select AdapterType, NetConnectionID, MACAddress from Win32_NetworkAdapter WHERE NetConnectionID LIKE 'Local Area Connection%'")


For Each IPConfig in IPAdapterSet2
 Set IPConfigSet = objWMIService.ExecQuery("Select IPEnabled, Description, Caption, IPAddress, MACAddress from Win32_NetworkAdapterConfiguration where IPEnabled='True' AND MACAddress = '" & IPConfig.MACAddress & "' ")
   For Each IPConf in IPConfigSet
    if NOT strLAN="" then
      strLAN = strLAN & vbCrLf
    end if
    strLAN = strLAN  & " " & IPConfig.NetConnectionID & " "
    If Not IsNull(IPConf.IPAddress) Then
     For i = LBound(IPConf.IPAddress) to UBound(IPConf.IPAddress)
      If Not ((Instr(IPConf.IPAddress(i), ":") > 0) or (Instr(IPConf.IPAddress(i), "none") > 0)) Then
    if i=0 then
      strLAN = strLAN & " " & IPConf.IPAddress(i)
    else
      strLAN = strLAN & ", " & IPConf.IPAddress(i) 
    end if
      End If
     Next
    End If
   next
Next


wscript.Echo strWLAN
wscript.Echo strLAN

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2023-04-05
    • 2016-09-20
    相关资源
    最近更新 更多