We help IT Professionals succeed at work.

Wscript fails to map drives

Jon Fulkerson
on
618 Views
Last Modified: 2012-05-06
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

Comment
Watch Question

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

Jon FulkersonIT Manager

Author

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
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
Jon FulkersonIT Manager

Author

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.
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
Jon FulkersonIT Manager

Author

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
 
Jon FulkersonIT Manager

Author

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.
 
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

IT Manager
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Did my script help? Looks like it
Jon FulkersonIT Manager

Author

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.
Wasnt look for points but thank you for finding my selfishness
Jon FulkersonIT Manager

Author

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.