Solved

Wscript fails to map drives

Posted on 2009-05-05
13
585 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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