VBS-用于将驱动器映射到多个NIC的登录脚本

时间:2013-09-03 18:01:42

标签: authentication vbscript drive-mapping

我正在尝试创建一个脚本,以便根据IP地址或子网在登录时映射驱动器。我能够找到一个在单个MIC上运行良好的脚本,但是很少有2个Nics的机器,并且它不适用于它们。

这是修改过的脚本。

set objNetwork = CreateObject("Wscript.Network")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colAdapters = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")


ReDim arrSubnets(-1)
For Each objAdapter in colAdapters
  For Each strAddress in objAdapter.IPAddress
    arrOctets = Split(strAddress, ".")
    If arrOctets(0) <> "" Then
      ReDim Preserve arrSubnets(UBound(arrSubnets)+1)
      arrSubnets(UBound(arrSubnets)) = arrOctets(0) & "." & arrOctets(1) & "." _
        & arrOctets(2)

    End If
  Next
Next

    Set colItems = objWMIService.ExecQuery _ 
        ("Select * From Win32_LogicalDisk Where DeviceID = 'G:'") 
If colItems.Count = 0 Then


    strSubnet = arrSubnets(UBound(arrSubnets))
 Select Case strSubnet


       Case "10.1.1" 
            objNetwork.MapNetworkDrive "G:", "\\10.1.1.62\zShared",True
        objNetwork.MapNetworkDrive "F:", "\\10.1.1.62\zShared2",True

        Case "10.1.20" 
            objNetwork.MapNetworkDrive "G:", "\\10.1.20.150\sharedch",True
        objNetwork.MapNetworkDrive "F:", "\\10.1.20.150\sharedch1",True 
End Select

End If

1 个答案:

答案 0 :(得分:1)

您在不初始化(或调整大小)的情况下使用arrSubnets。此外,当您应该回显arrSubnetIPs(i)时,您正试图回复arrSubnets(i)。改变这个:

i = 0

For Each objAdapter in colAdapters
  For Each strAddress in objAdapter.IPAddress
    arrOctets = Split(strAddress, ".")
    If arrOctets(0) <> "" Then
      arrSubnets(i) = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
      i = i + 1
      WScript.Echo arrSubnetIPs(i)
    End If
  Next
Next

进入这个:

ReDim arrSubnets(-1)
For Each objAdapter in colAdapters
  For Each strAddress in objAdapter.IPAddress
    arrOctets = Split(strAddress, ".")
    If arrOctets(0) <> "" Then
      ReDim Preserve arrSubnets(UBound(arrSubnets)+1)
      arrSubnets(UBound(arrSubnets)) = arrOctets(0) & "." & arrOctets(1) & "." _
        & arrOctets(2)
      WScript.Echo arrSubnets(UBound(arrSubnets))
    End If
  Next
Next