Script to Map a network drive does not work on first time run

Posted on 2009-12-17
Last Modified: 2013-11-09
I have a batch file that establishes a Microsoft VPN Client connection, executes a vbscript then closes the vpn connection.  I think my issue is in the vbscript where the network mapping takes place.  My code is attached and when I run it through the command prompt I get an error:

WSHNetwork.MapNetworkDrive: The network name cannot be found

However, when I run the script a second time, it is successful and finishes without any problems.

Any ideas would be great.


Dim WMI, Configs, Config, Adapters, Adapter

Dim IP, objShell, rteAddCmd, objFSO, logStream, objNetwork

Set objFSO = CreateObject("scripting.FileSystemObject")

Set objNetwork = CreateObject("WScript.Network")

Dim DateToAppend, tempDate, mo, d, ccyy

tempDate = Date()

mo = Month(tempDate)

d = Day(tempDate) 

ccyy = Year(tempDate)

If mo < 10 then

   mo = "0" & mo

End If

If d < 10 then

   d = "0" & d

End If

DateToAppend = ccyy & mo & d

Set WMI = GetObject("winmgmts:{impersonationlevel=impersonate}" _

  & "root/cimv2")


Set Configs = WMI.ExecQuery("Select * " _

  & "From Win32_NetworkAdapterConfiguration Where IPEnabled=True")


For Each Config In Configs


  Set Adapters = _

    WMI.AssociatorsOf("Win32_NetworkAdapterConfiguration.Index=" _

    & Config.Index, "Win32_NetworkAdapterSetting")


  For Each Adapter In Adapters

    If Left(Adapter.Description, 12) = "WAN Miniport" Then

      'WScript.Echo Config.IPAddress(0)

      IP = Config.IPAddress(0)

    End If



rteAddCmd = "route add externalIP mask " & IP

WScript.Echo "Creating new network route"

Set objShell = CreateObject("WScript.Shell") "cmd /c " & rteAddCmd

WScript.Echo "Mapping Network Drive"

objNetwork.MapNetworkDrive "V:", "\\server\folder\share", True, "folder\username", "password"

If Err.Number <> 0 Then

   objNetwork.RemoveNetworkDrive "V:", True, True

   objNetwork.MapNetworkDrive "V:", "\\server\folder\share", True, "folder\username", "password"

End If

WScript.Echo "Copying File"

If objFSO.fileExists("V:\filename.txt") then

   objFSO.CopyFile "V:\filename.txt" ,"Drive path", True

End If

WScript.Echo "Renaming File"

objFSO.MoveFile "D:\filename.txt", "D:\newfilename" & DateToAppend & ".txt"

WScript.Echo "Removing Network Drive"

objNetwork.RemoveNetworkDrive "Drive letter", True, True

Set objNetwork = nothing

WScript.Echo "Finished"

Open in new window

Question by:anewlevel4u
    LVL 7

    Accepted Solution

    before doing the Mapping,
    why you don't send a ping in a loop to the cifs server using the name of it, and if it replies successfully then continue with the map...
    LVL 7

    Expert Comment


    Author Closing Comment

    This appears to have worked.  I actually had to add the sleep after the "route add" command to get it to register before attempting the ping process.  When I tried the command and immediately tried to ping.  The ping failed.  Thanks for the solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    NetScaler Deployment Guides and Resources

    Citrix NetScaler is certified to support many of the most commonly deployed enterprise applications. Deployment guides provide in-depth recommendations on configuring NetScaler to meet specific application requirements.

    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 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

    13 Experts available now in Live!

    Get 1:1 Help Now