[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-12-17
3
Medium Priority
?
680 Views
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.

Thanks,
Doug

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")

' BEGIN CALLOUT A
Set Configs = WMI.ExecQuery("Select * " _
  & "From Win32_NetworkAdapterConfiguration Where IPEnabled=True")
' END CALLOUT A

For Each Config In Configs
' BEGIN CALLOUT B
  Set Adapters = _
    WMI.AssociatorsOf("Win32_NetworkAdapterConfiguration.Index=" _
    & Config.Index, "Win32_NetworkAdapterSetting")
' END CALLOUT B
  For Each Adapter In Adapters
    If Left(Adapter.Description, 12) = "WAN Miniport" Then
      'WScript.Echo Config.IPAddress(0)
      IP = Config.IPAddress(0)
    End If
  Next
Next

rteAddCmd = "route add externalIP mask 255.255.255.0 " & IP

WScript.Echo "Creating new network route"
Set objShell = CreateObject("WScript.Shell")
objShell.run "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

0
Comment
Question by:anewlevel4u
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
geergon earned 2000 total points
ID: 26077222
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...
0
 
LVL 7

Expert Comment

by:geergon
ID: 26077231
0
 

Author Closing Comment

by:anewlevel4u
ID: 31667385
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.
0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
I recently attended Cisco Live! in Las Vegas, a conference that boasted over 28,000 techies in attendance, and a week of hands-on learning hosted by a solid partner with which Concerto goes to market.  Every year, Cisco displays cutting-edge technol…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

831 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