脚本获取错误的NIC IP详细信息

时间:2016-03-25 13:40:23

标签: vbscript

我使用以下脚本获取目标计算机的IP地址,然后通过调整最后一个八位字节将驱动器映射到同一网络上的另一台PC。

这很好但我现在必须在具有两个NIC(名为Primary和Internal)的计算机上运行此脚本,并且脚本会获取错误NIC的IP(内部)

如何让它查看另一块网卡的IP?

Dim HostIPAddress : HostIPAddress = ""
Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim colAdapters : Set colAdapters = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Dim objAdapter
For Each objAdapter In colAdapters
  If Not IsNull(objAdapter.IPAddress) Then HostIPAddress = Trim(objAdapter.IPAddress(0))
  Exit For
Next

strIP = HostIPAddress
i = InStrRev(strIP, ".")
strIP = Left(strIP, i) & "15"

Dim objNetwork 
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile

strDriveLetter = "Z:" 
strRemotePath = "\\"&strIP&"\c$" 
strUser = "User"
strPassword = "Password!"
strProfile = "false"

Set objNetwork = WScript.CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
  strProfile, strUser, strPassword

1 个答案:

答案 0 :(得分:1)

获取名为" primary"的NIC的设备ID。通过Win32_NetworkAdapter类,然后使用该ID从Win32_NetworkAdatperConfiguration类中选择正确的适配器。

nicName = "primary"

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

deviceQry = "SELECT * FROM Win32_NetworkAdapter " & _
            "WHERE NetConnectionId = '" & nicName & "'"
For Each adapter In wmi.ExecQuery(deviceQry)
  addressQry = "SELECT * FROM Win32_NetworkAdapterConfiguration " & _
               "WHERE Index = " & adapter.DeviceId
  For Each config In wmi.ExecQuery(addressQry)
    If Not IsNull(config.IPAddress) Then
      HostIPAddress = Trim(config.IPAddress(0))
      Exit For
    End If
  Next
Next