'How Can I Map a Drive Based on the Computer’s Subnet?' Question
Hey,

In the article on mapping a drive based on a computer's subnet, a brief mention is made about the difficulty of doing this with a PC that has two or more active NICs. It was mentioned as something that the scripting guys would have to tackle in the future. Well, there's no time like the present! :-) (http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar08/hey0304.mspx)

We're trying to deploy this script in our AD domain, but we do have many users with multiple NICs and our tests with these machines have not been positive. Has anyone had any luck adjusting this script in that kind of circumstance?

Thanks!

John


October 31st, 2008 9:52pm

Hi John

Looking at the scrpt in the article it already enumerates all NICs however it will set the strSubnet variable to the last NICs, first IP address.

Depending on what the criteria is for mapping the drive you will need to do one of the following:

Looking for Specific Subnet
If you are only interested in one subnet then you should enumerate all adapters and all IP Addresses until you find it then exit. The adapter code would need to be modified as follows:

1ForEachobjAdapterincolAdapters
2ForEachstrAddressinobjAdapter.IPAddress
3arrOctets=Split(strAddress,".")
4IfarrOctets(0)<>""Then
5strSubnet=arrOctets(0)&"."&arrOctets(1)&"."&arrOctets(2)
6IfstrSubnet="192.186.1"Then
7x=1
8ExitFor
9EndIf
10EndIf
11Next
12Ifx=1Then
13ExitFor
14EndIf
15Next
16

The code changes check if the subnet matches a specific subnet and if so exits both For...Next loops (the original code does not exit both For...Next loops as the If x = 1 Then block needs to come after the first For..Next block.

Mapping Drives for Multiple Subnets
If you want to map a drive for multiple subnets, as in the original script, then you will be best off storing the subnets in an array and then enumerating the array to map the drives as shown below:
1i=0
2
3ForEachobjAdapterincolAdapters
4ForEachstrAddressinobjAdapter.IPAddress
5arrOctets=Split(strAddress,".")
6IfarrOctets(0)<>""Then
7arrSubnets(i)=arrOctets(0)&"."&arrOctets(1)&"."&arrOctets(2)
8i=i+1
9EndIf
10Next
11Next
12
13SetcolItems=objWMIService.ExecQuery_
14("Select*FromWin32_LogicalDiskWhereDeviceID='H:'")
15
16IfcolItems.Count=0Then
17Fori=0ToUBound(arrSubnets)
18SelectCasearrSubnets(i)
19Case"192.168.1"
20objNetwork.MapNetworkDrive"H:","\\arizona-fs-01\public"
21Case"192.168.2"
22objNetwork.MapNetworkDrive"H:","\\newmexico-fs-01\public"
23Case"192.168.3"
24objNetwork.MapNetworkDrive"H:","\\texas-fs-01\public"
25EndSelect
26Next
27EndIf
28

This code stores each subnet for all IP addresses for all NICs in the arrSubnets array then enumerates this array to map the relevant drive. This means that if one machine has 2 NICs with each NICs subnet requiring a mapped drive, all relevant drives will get mapped.





Free Windows Admin Tool Kit Click here and download it now
November 4th, 2008 11:01pm

HI,

I tried your code for mapping drive with Multiple NICs, but i am getting this error.

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

September 3rd, 2013 8:25pm

Hi,

1. This question is already marked as answered.

2. In general we recommend mapping drives using group policy preferences instead of script.

Bill

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2013 8:53pm

Hi Bill,

Thanks for your reply.. I am going to post it on another thread.

I understand your recommendation but in mine case a user login at multiple locations which force me to use such kind of scripts.

September 4th, 2013 8:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics