troubleshooting Question

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

Avatar of anewlevel4u
anewlevel4uFlag for United States of America asked on
VPNScripting LanguagesWindows Batch
3 Comments1 Solution698 ViewsLast Modified:
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"
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros