Wscript fails to map drives

So I'm mapping replicated servers per each location (via IP address) with a VBS Wscript. The problem I am experiencing is failure for some computers to map the drives that are specified in the script. Each of the computers failing to map have dual nic's. (Dual Ethernet or Ethernet and WLAN)

It appears that the NIC's that are disconnected are somehow taking priority over the connected NIC. Wireless nics appear to always be the NIC that take priority or that are checked against the script even when disconnected. Can anyone tell me how to fix this? Is there something I need to modify or add to my script to check all or active connections? Maybe something in the registry needs to be modified?

Here is my VBS Wscript.

Set objNetwork = CreateObject("Wscript.Network")

strComputer = "."
On Error Resume Next
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
            strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
            x = 1
            Exit For
        End If
        If x = 1 Then
            Exit For
        End If
    Next
Next

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DeviceID = 'W:'")

If colItems.Count = 0 Then
    Select Case strSubnet
        Case "192.168.1"
            objNetwork.MapNetworkDrive "W:", "\\fry\WAFS"
        Case "192.168.3"  
            objNetwork.MapNetworkDrive "W:", "\\TinnyTim\WAFS"
        Case "192.168.5"
            objNetwork.MapNetworkDrive "W:", "\\Destructor\WAFS"
        Case "192.168.6"
            objNetwork.MapNetworkDrive "W:", "\\Boxy\WAFS"
    End Select
End If

jonf805Asked:
Who is Participating?
 
jonf805Connect With a Mentor Author Commented:
The following statement fixed the issue for us for anyone with the same issue.

Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where DHCPServer LIKE '192%' AND DNSdomain LIKE 'domain%'")
 
Here's the full script. Just replace 'domainname%' with your own. In order for this to work 100%, we had to include both the DHCP and DNSdomain in the script since some network cards that are disconnected will return a DHCP last address and not a domain.

Set objNetwork = CreateObject("Wscript.Network")

strComputer = "."
On Error Resume Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colAdapters = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where DHCPServer LIKE '192%' AND DNSdomain LIKE 'domain%'")

For Each objAdapter in colAdapters
For Each strAddress in objAdapter.IPAddress
arrOctets = Split(strAddress, ".")
If arrOctets(0) <> "" Then
strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
x = 1
Exit For
End If
If x = 1 Then
Exit For
End If
Next
Next

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_LogicalDisk Where DeviceID = 'W:'")

If colItems.Count = 0 Then
Select Case strSubnet
Case "192.168.1"
objNetwork.MapNetworkDrive "W:", "\\fry\WAFS"
Case "192.168.3"
objNetwork.MapNetworkDrive "W:", "\\TinnyTim\WAFS"
Case "192.168.5"
objNetwork.MapNetworkDrive "W:", "\\Destructor\WAFS"
Case "192.168.6"
objNetwork.MapNetworkDrive "W:", "\\Boxy\WAFS"
End Select
End If
 
0
 
Mark PavlakCommented:
Try this sub I have it disconnects the drive prior to mapping  You would add to the bottom of your script and relpace the objNetwork.MapNetworkDrive "W:","\\ddd\ddddddd" with Drivemapper ("w:","\\xxx\ddddd\")

Sub drivemapper (drive,share)
'Map Network drive
Dim network,drives,i
Set network = WScript.CreateObject("WScript.Network")
Set drives = network.EnumNetworkDrives
For i=0 to drives.count -1 Step 2
If LCase(drive)= LCase(drives.item(i)) Then
network.RemoveNetworkDrive drive, True,True 
End If
Next
network.MapNetworkDrive drive, share
End Sub

Open in new window

0
 
jonf805Author Commented:
Same result. Maps when both WLAN and Lan are connected. Does not map when WLAN is disconnected.

Set objNetwork = CreateObject("Wscript.Network")

strComputer = "."
On Error Resume Next
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
            strSubnet = arrOctets(0) & "." & arrOctets(1) & "." & arrOctets(2)
            x = 1
            Exit For
        End If
        If x = 1 Then
            Exit For
        End If
    Next
Next

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DeviceID = 'W:'")

If colItems.Count = 0 Then
    Select Case strSubnet
        Case "192.168.1"
            Drivemapper "W:", "\\fry\WAFS"
        Case "192.168.3"  
            Drivemapper "W:", "\\TinnyTim\WAFS"
        Case "192.168.5"
            Drivemapper "W:", "\\Destructor\WAFS"
        Case "192.168.6"
            Drivemapper "W:", "\\Boxy\WAFS"
    End Select
End If
Sub drivemapper (drive,share)
'Map Network drive
Dim network,drives,i
Set network = WScript.CreateObject("WScript.Network")
Set drives = network.EnumNetworkDrives
For i=0 to drives.count -1 Step 2
If LCase(drive)= LCase(drives.item(i)) Then
network.RemoveNetworkDrive drive, True,True
End If
Next
network.MapNetworkDrive drive, share
End Sub
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Mark PavlakCommented:
Have you tried setting a GPO?  There is one inparticular that tells the PC to wait for the network prior to login.  This would force the NIC to come up 100% prior to logon
0
 
jonf805Author Commented:
It's not an issue of the network not being on or ready. It's an issue of 2 NICS.

If one is disabled in  network connections, it maps fine. As stated in the first post, it's as if the nic that is not on net is a primary in the system and the script is checking against the primary NIC and not the Secondary (Primary=WLAN, Secondary = Ethernet)

The script in question os a script run after a logon script and is referenced by the logon script. In order for the Wscript to run, it must be on net with a valid DHCP address to even access the Logon script.

I don't know how to inform the system via the Wscript to check all NICS in the system and check IP's to map accordingly.
0
 
Mark PavlakCommented:
I can give you a copy of a script I wrote that cyucles through the nics using WMI and extracts their IP's or  If you go under your network connections on the tool bar select advance then advanced options you can set the order of your nics there
0
 
jonf805Author Commented:
I'm willing to try your script.
Changing the order of the NIC's does not fix it. If laptops are generally connected via ethernet and we set the LAN interface and 1, it will map when on LAN. It will not map on WLAN.
This is just giving preference to one interface over another.
Can something be added to the script to specifiy a nic by ip? run on interface if ip address = 192.168.x.x
 
0
 
jonf805Author Commented:
By the way, changing Interface order per your suggestion did not work. Even rebooted to be sure.
Still only maps if WLAN is disabled when ethernet is connected.
 
0
 
Mark PavlakCommented:
Here is a snippet you can play with to get it to lookup via IP.  It will cycle through WMI and get All IP's on all nics
Sub  Win32_NetworkAdapterConfiguration (strComputerAccount)
On Error Resume Next
'Variables
'=============================================================================================
Dim objWMI,objPCAttribute,objPC
Dim arrIP
Dim intArraySize,intPlace
'=============================================================================================
'Initalize WMI
'=============================================================================================
Set objWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputerAccount & "\root\cimv2")
Set objPC = objWMI.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration")
'=============================================================================================
'Get Attribues from Win32_NetworkAdapterConfiguration
'=============================================================================================
For Each objPCAttribute In objPC
	 If isNull(objPCAttribute.IPAddress) Then
    Else
    	intPlace = 0
        intCount = intCount + 1
		objWorkSheet.cells(1,intCount).value = "MAC Address on Adapter "&intPlace
		objWorkSheet.cells(2,intCount).value = objPCAttribute.MACAddress
       ' wscript.echo "IP Addresses: "&Join(objPCAttribute.IPAddress, "  ")
        
       intArraySize = UBound (objPCAttribute.IPAddress)+ 1
       Do While intArraySize <> 0
       intCount = intCount + 1
       	objWorkSheet.cells(1,intCount).value = "IP Address "&intPlace+1
      	objWorkSheet.cells(2,intCount).value = objPCAttribute.IPAddress(intPlace)
      	intPlace = intPlace + 1
      	intArraySize = intArraySize - 1
       Loop
    End If
'=============================================================================================
Next
End Sub

Open in new window

0
 
Mark PavlakCommented:
Did my script help? Looks like it
0
 
jonf805Author Commented:
We did not use any part of your script. It did not help direct us in any way. Sorry, I can not provide points.
0
 
Mark PavlakCommented:
Wasnt look for points but thank you for finding my selfishness
0
 
jonf805Author Commented:
I'm sure your script may have a solution within it. It just turned out that we figured out a solution around the same time you posted your script. I just assumed you were assuming points would be awarded. Sorry if I came across as a jerk. I was intending to be brief.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.