Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS - Login script to map drive with multuple NICs.

Status
Not open for further replies.

Sam1886

MIS
Sep 3, 2013
3
IN
Hi,

I am trying to create a script to map drive at logon as per IP address or subnet. I was able to find one script which works well on single MIC, but thre are few machines who have 2 Nics, and it does not work with them.

Here is modified script which i created for multiple NICs and error in picture.

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

Set colAdapters = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
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 

    Set colItems = objWMIService.ExecQuery _ 
        ("Select * From Win32_LogicalDisk Where DeviceID = 'G:'") 
If colItems.Count = 0 Then
For i = 0 To UBound(arrSubnets) 
Select Case strSubnet

       Case "10.10.10" 
            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
Next
End If

 

There's a lot that I can recommend to you as far as "better coding" goes. Here are a few pointers:

Use an Option Explicit statement in your code. By specifying an option Explicit your program will automatically error out if a variable used in the program is not defined or is misspelled.
Use DIM statement to define all the variables used in your program in conjunction with the Option Explicit statement.
For values that never or shouldn't change (like strComputer) define them as a constant (i.e. Const strComputer = ".") instead of defining them in the Dim statement. Why? Prevents an accident from happening in more complex code as constants can never change once defined.
When defining an array, set the array up as variable sized array by first specifying the array variable in the DIM statement with nothing between two brackets (i.e. arrSubnets() ). Once you have the array defined as variable in size, use a redim statement to set the initial size of the array to -1 as in:

Dim arrSubnets()
ReDIM arrSubnets(-1)

Instead of using the variable i to specify the array element you're loading use a "redim preserve" statement and modify the size upper bounds of your array and then use the upper bounds value to define what array element your going to load. This may seem a bit confusing but here's what it looks like:

ReDim Preserve arrSubnets(UBound(arrSubnets) + 1)
arrSubnets(Ubound(arrSubnets)) = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)

The ReDim preserve statement preserves the content of the array but adds another element to the array. So the first time you use it in your program the code automatically sets the array to hold 1 element (i.e. the zero element in any array). The next time it's used it then preserves the array and also adds another element. Using this code you can automatically handle arrays of any size. Why do this instead of using i? Well, in a very complex program you could accidentally modify the variable i somewhere else in the program and overwrite an existing element with another value and cause unexpected results which would be hard to debug. The above statements make arrays fairly easy to manage and avoids an accident. Again the above is just good coding as this code can be used over and over again for any array of any size you want to define in any program. Just remember to define it with a dim and then initially redim it to -1.

The main thing I saw in your program though dealt with the SELECT statement. The SELECT uses a variable named strSubnet but nowhere in the program did you assign that variable a value. I believe that before the SELECT you need to add a statement that reads:

strSubnet = arrSubnets(i)

So.... When it's all said and done I would suggest that your program should look something like this (Note that I did not test this):

Option Explicit
Dim objNetwork,objWMIService,colAdapters,i,objAdapter,colAdapters,arrSubnets(),colItems,strsubnet
ReDim arrSubnets(-1)
Const strComputer = "."

set objNetwork = CreateObject("Wscript.Network")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
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
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'G:'")
If colItems.Count = 0 Then
For i = 0 To UBound(arrSubnets)
strSubnet = arrSubnets(i)
Select Case strSubnet
Case "10.10.10"
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
Next
End If

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top