Share via


VBSCRIPT - Allow Users to choose which network drives to map, alert if process is running, find if current drive is mapped

The below script does quite a few things.

  1. This script will allow a user to chose an option 1-6 pick a network drive and it will map them to that network share.
  2. This script detects if a program is running and will not execute the commands until the process is closed for example notepad. This can be used to notify a user not to map the drive until a process is closed because it will effect how the program works. I have run into this issue so I added this in the script
  3. This script goes to the registry currentuser, network, z (drive letter), remote path and looks for the value rz_dword. The reason I did this is because this will include drives that are hidden with group policy.

Now the script can most likely be cleaned up but it works.

Also a word of advice if you look at the part objNetwork.RemoveNetwork & objNetwork.MapNetworkDrive   These can be customized based on force remove, persistent and profile based rules.

 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

'Creator Dominic Ace
 
Set objNetwork = CreateObject("Wscript.Network") 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'This is to look for the registry current user hive, this works with line 93, 131 and 31-36
Const HKEY_CURRENT_USER = &H80000001
 
'This is created variables to be called later
Dim strComputer, strKeyPath, strValueName, oReg, strValue  
dim mapdrive
Dim strRemotePath1, strDriveLetter1, strNewName1
Dim strRemotePath2, strDriveLetter2, strNewName2
Dim strRemotePath3, strDriveLetter3, strNewName3
Dim strRemotePath4, strDriveLetter4, strNewName4
Dim strRemotePath5, strDriveLetter5, strNewName5
 
 
 
 
'This is to declare variables for each z drive environment where it says example add your network path for example \\share1\folder1 with quotes
strDriveLetter1 = "z:"
strRemotePath1 = "\\example\folder1"
strDriveLetter2 = "z:"
strRemotePath2 = "\\example\folder1"
strDriveLetter3 = "z:"
strRemotePath3 = "\\example\folder1"
strDriveLetter4 = "z:"
strRemotePath4 = "\\example\folder1"
strDriveLetter5 = "z:"
strRemotePath5 = "\\example\folder1"
 
 
 
'This will look in the registry to find the z drive for mapped drive
strComputer = "."
Set oReg=GetObject( _
   "winmgmts:{impersonationLevel=impersonate}!\\" &_
    strComputer & "\root\default:StdRegProv")
strKeyPath = "Network\z"
strValueName = "RemotePath"
 
 
 
'This is the main box that will pop up and have user choose which environment they would like to map to. 
 mapdrive=InputBox("Press 1 - Map to Z Share1:" & Chr(13) & "Press 2 - Map to Z Share2:" & Chr(13) & "Press 3 - Map to Z Share3:" & Chr(13) & "Press 4 - Map to Z Share4:" & Chr(13) & "Press 5 - Map to Z Share5:" & Chr(13) & "Press 6 - FIND OUT CURRENT ENVIRONMENT:","MAP Drive To Specified Environment")
 
 
 'Look for windows process notepad.exe and tell user this script will not work until closed. You can add any process and it is case sensitive
set service = GetObject ("winmgmts:")
 
for each Process in Service.InstancesOf ("Win32_Process")
    If Process.Name = "notepad.exe" then
        wscript.echo "Notepad is running please close before mapping drive, and then try again."
        wscript.quit
    End If
next
 
 
 
 
'Below scripts are for user to choose a number and if number is chosen it will remove current z drive mapping and map new z drive path. 
if mapdrive=("1") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1, TRUE
End If
if mapdrive=("2") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2, TRUE
End If
if mapdrive=("3") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3, TRUE
End If
if mapdrive=("4") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter4, strRemotePath4, TRUE
End If
if mapdrive=("5") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter5, strRemotePath5, TRUE
End If
'This calls the registry from earlier in the script and will display the current z drive path.
if mapdrive=("6") Then
oReg.GetStringValue _
  HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Wscript.Echo "You Are Currently Mapped To: " & strValue
END If
'This is if user chose option 6 it will look one more time so user does not have to click on box again.
If mapdrive=("6") Then 
 mapdrive=InputBox("Press 1 - Map to Z Share1:" & Chr(13) & "Press 2 - Map to Z Share2:" & Chr(13) & "Press 3 - Map to Z Share3:" & Chr(13) & "Press 4 - Map to Z Share4:" & Chr(13) & "Press 5 - Map to Z Share5:" & Chr(13) & "Press 6 - FIND OUT CURRENT ENVIRONMENT:","MAP Drive To Specified Environment")
End if
 if mapdrive=("1") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1, TRUE
End If
if mapdrive=("2") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2, TRUE
End If
if mapdrive=("3") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter3, strRemotePath3, TRUE
End If
if mapdrive=("4") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter4, strRemotePath4, TRUE
End If
if mapdrive=("5") Then
On Error Resume Next
objNetwork.RemoveNetworkDrive "z:", false, TRUE
Wscript.Sleep 5000
Set objNetwork = CreateObject("WScript.Network") 
    objNetwork.MapNetworkDrive strDriveLetter5, strRemotePath5, TRUE
End If
'This calls the registry from earlier in the script and will display the current z drive path.
if mapdrive=("6") Then
oReg.GetStringValue _
  HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Wscript.Echo "You Are Currently Mapped To: " & strValue
END If