Solved

Wscript fails to map drives

Posted on 2009-05-05
13
574 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

0
Comment
Question by:jonf805
  • 7
  • 6
13 Comments
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24307639
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
 

Author Comment

by:jonf805
ID: 24309036
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
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24309084
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
 

Author Comment

by:jonf805
ID: 24309496
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
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24313654
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
 

Author Comment

by:jonf805
ID: 24316380
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:jonf805
ID: 24316437
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
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24316590
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
 

Accepted Solution

by:
jonf805 earned 0 total points
ID: 24317513
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
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24317989
Did my script help? Looks like it
0
 

Author Comment

by:jonf805
ID: 24318036
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
 
LVL 6

Expert Comment

by:Mark Pavlak
ID: 24318738
Wasnt look for points but thank you for finding my selfishness
0
 

Author Comment

by:jonf805
ID: 24320498
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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now