VBScript: Run an app that requires a Mapped Drive

Some software is just a pain. We encountered another program yesterday that requires a mapped drive to run off the network. You can’t use a UNC path as the hyperlinks don’t work properly to a number of linked documents. We don’t have mapped drives except for the users data and a single share. So we didn’t want another one persistent just for this program. Not going to name the application, but here is a workaround.

This simple script solves this problem. It does the following:

  1. Maps a drive to a specified location
  2. Checks that the drive has mapped correctly
  3. Runs the application from the mapped drive
  4. Monitors the current processes for the application to close
  5. Disconnects the mapped drive

All you need to do is edit the variables at the top to make it run for your application, and choose an appropriate drive letter. The command to run is purposely a different variable in case you need to run an application that is in the subfolder of the share.

Option Explicit
Dim objNetwork, intResult, objShell, intInstances, objWMIService, strWMIQuery, strDriveLettertoMap, strUNCPathtoMap, strApplicationName, strCommandtoRun
'Variables
strDriveLettertoMap = "O:"
strUNCPathtoMap = \\yourserver\yoursharename"
strApplicationName = "AppName.exe"
strCommandtoRun = "O:\SubFolder\AppName.exe"
'Map the network drive
Set objNetwork = WScript.CreateObject("WScript.Network")
intResult = objNetwork.MapNetworkDrive(strDriveLettertoMap , strUNCPathtoMap , False)
'Check that the drive mapped correctly. 
If intResult <> 0 Then
 Msgbox("There has been a problem starting the application. Please report this to your Network Administrator.")
 WScript.Quit
End If
'Start the Application
Set objShell = WScript.CreateObject ("WScript.shell")
objShell.run strCommandtoRun
Set objShell = Nothing
'Monitor the services and wait for the application to close. 
strWMIQuery = "Select * from Win32_Process where name like '" & strApplicationName & "'"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 
intInstances = 1
Do While intInstances > 0
    Wscript.Sleep 5000
    intInstances = objWMIService.ExecQuery(strWMIQuery).Count
Loop
'Remove the network drive
WScript.Sleep 2000
objNetwork.RemoveNetworkDrive strDriveLettertoMap

4 people found this post useful.


2 thoughts on “VBScript: Run an app that requires a Mapped Drive

  1. Hi,

    Please can you help me as i’m new to VB and struggling.

    I need to map several network drives then call on the application which is on the local C drive to run but is dependant on these drives.

    When the application is closed then disconnect these drives.

    Many Thanks

    Gary

    1. You will just need to copy the lines to map the drive (line 10 in the original) for each drive, checking that all of the drives mapped. The bit that will need some work is if a drive map fails, you would probably want to clean up all drives that were successfully mapped.

      So, I would suggest something like below (untested). It is a little crude in that we just keep adding more variables, and an array might be more elegant if you wanted something more flexible.

      'Variables
      strDriveLettertoMap1 = "O:"
      strUNCPathtoMap1 = \\yourserver\yourshare1name"
      strDriveLettertoMap2 = "P:"
      strUNCPathtoMap2 = \\yourserver\yourshare2name"

      strApplicationName = "AppName.exe"
      strCommandtoRun = "O:\SubFolder\AppName.exe"

      'Map the network drive
      Set objNetwork = WScript.CreateObject("WScript.Network")
      intResult = 0
      intResult + objNetwork.MapNetworkDrive(strDriveLettertoMap1 , strUNCPathtoMap1 , False)
      intResult + objNetwork.MapNetworkDrive(strDriveLettertoMap2 , strUNCPathtoMap2 , False)

      'Check that the drive mapped correctly.
      If intResult <> 0 Then
      Msgbox("There has been a problem starting the application. Please report this to your Network Administrator.")
      WScript.Quit
      Else
      objNetwork.RemoveNetworkDrive(strDriveLettertoMap1, True)
      objNetwork.RemoveNetworkDrive(strDriveLettertoMap2, True)
      End If

      'Start the Application
      Set objShell = WScript.CreateObject ("WScript.shell")
      objShell.run strCommandtoRun
      Set objShell = Nothing

      'Monitor the services and wait for the application to close.
      strWMIQuery = "Select * from Win32_Process where name like '" & strApplicationName & "'"
      Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
      intInstances = 1
      Do While intInstances > 0
      Wscript.Sleep 5000
      intInstances = objWMIService.ExecQuery(strWMIQuery).Count
      Loop

      'Remove the network drive
      WScript.Sleep 2000
      objNetwork.RemoveNetworkDrive(strDriveLettertoMap1, True)
      objNetwork.RemoveNetworkDrive(strDriveLettertoMap2, True)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.