Link to home
Start Free TrialLog in
Avatar of JamesonJendreas
JamesonJendreas

asked on

VBScript Multiple network Drives

Hi,
I'm attempting to set up some network drives via VBScript, but I'm having issues ending the script.  I haven't had much VB practice in years, so my code might be sloppy. I'm Trying to have these drives attempt to set up with the option that they may already be connected and require a preliminary disconnect. My code is as Follows

Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell, objFso
Dim AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FileSystemObject")
IF objFso.DriveExists(strDriveLetter1) then AlreadyCOnnected = true
If AlreadyConnected = true then
      objNetwork.RemoveNetworkDrive strDriveLetter1
      objShell.PopUp "Disconnecting Shared Drive"
      objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
      objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
If objFso.DriveExists(strDriveLetter2) then AlreadyConnected2 = true
If AlreadyConnected2 = True Then
      objNetwork.RemoveNetworkDrive strDriveLetter2
      objShell.PopUp "Disconnecting Graphics"
      objNetwork.AddNetworkDrive strDriveletter2, strRemotePath2
      objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive strdriveLetter2, strRemotePath2
End If


I keep getting an error that for an Expected "End" or expected "IF" statement in line 32, the last line of the script.  I am at a lost for ideas as I   haven't done much scripting lately.  Thank You!


Avatar of chandru_sol
chandru_sol
Flag of India image

Hi,

You were missing one End if at the end

try this
Option Explicit
Dim strDriveLetter1, strRemotePath1, strDriveLetter2, strRemotePath2
Dim objNetwork, objShell, objFso
Dim AlreadyConnected, AlreadyConnected2
strDriveLetter1 = "s:"
strRemotePath1 = "\\ginkgo\shared"
strDriveLetter2 = "g:"
strRemotePath2 = "\\rainbowserver\graphics"
set objShell = CreateObject("WScript.Shell")
set objNetwork = CreateObject("WScript.Network")
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FileSystemObject")
IF objFso.DriveExists(strDriveLetter1) then AlreadyCOnnected = true
If AlreadyConnected = true then
      objNetwork.RemoveNetworkDrive strDriveLetter1
      objShell.PopUp "Disconnecting Shared Drive"
      objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
      objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
If objFso.DriveExists(strDriveLetter2) then AlreadyConnected2 = true
If AlreadyConnected2 = True Then
      objNetwork.RemoveNetworkDrive strDriveLetter2
      objShell.PopUp "Disconnecting Graphics"
      objNetwork.AddNetworkDrive strDriveletter2, strRemotePath2
      objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive strdriveLetter2, strRemotePath2
End If
End IF
Avatar of SysExpert
also see the net use command in a batch file

 net use x: \\server\share  /persistent:no

also

net use \\server /delete , or drive letter before doing the above.

Check the options

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JamesonJendreas
JamesonJendreas

ASKER

Sysexpert - I actually already have batch files I've made to map drives, I wanted to try and write VBScript mostly to see if I could get it to work, but also I feel like I can do more with it in the long run...  The more tools one has the better, eh?

Rob - I'm going to give your method of using arrays a try, its a little more complex than I'm currently used to using, but the whole reason to try using VBScript over batch files is to get to know the more complex ways to work with login scripts and what not.

Thanks! I'll give it a whirl and we'll see what happens!
Yeah, it seems more complex, but using an array does make multiple operations on the same type of information much easier.....let me know how it goes...

Regards,

Rob.
Thanks Rob, this method worked like a charm, I ended up taking out the part that brings up the command console altogether because I couldn't get it to work.  I'm going to drop the script in, If you can tell me whats wrong with the beggining of the script I'll fix it, otherwise this works just fine.  Thanks!


Option Explicit
Dim strPath, strCommand, objShell
'If LCase(Right(WScript.FullName, 11)) = "wscript.exe" Then
'      strPath = WScript.ScriptFullName
'      strCommand = "%comspec% /k cscript" " & strPath &"
'      set objShell = CreateObject("WScript.Shell")
'      objShell.Run(strCommand), 1, True
'      Wscript.Quit
'End IF      

Dim objNetwork
Dim RemoveDrive
Dim bForce, bUpdateProfile

Dim arrDrive, strDriveDetails, strDrive, strShare

bForce = "True"
bUpdateProfile = "True"

set objNetwork = CreateObject("WScript.Network")
set objShell = CreateObject ("WScript.Shell")

arrDrive = Array(_
      "S:~\\Ginkgo\shared",_
      "G:~\\Rainbowserver\graphics"_
      )
On Error Resume Next
For Each strDriveDetails In arrDrive
      strDrive = Split(strDriveDetails, "~")(0)
      strShare = Split (strDriveDetails, "~")(1)       
      
WScript.Echo "Attempting to Remove Connection to " &strDrive
objNetwork.RemoveNetworkDrive strDrive, bForce, bUpdateProfile
If Err.Number <>0 Then
      WScript.Echo "Error Removing Drive " & strDrive & "-" & Err.Number & ":" & Err.Description
      Err.Clear
Else
      WScript.Echo "Removed"
End If

WScript.Echo strDrive & "Attached, happy Sharing"
objNetwork.MapNetworkDrive strDrive, strShare, bUpdateProfile
If Err.Number <>0 Then
      WScript.Echo "Error Mapping " & strDrive & "- " & Err.Number &": "& Err.Description
      Err.Clear
Else
      WScript.Echo strDrive & "Mapped Succesfully" End If
Next
WScript.Quit
Hi, the code is fine, but in the first bit, change
strCommand = "%comspec% /k cscript" " & strPath &"
to
strCommand = "%comspec% /k cscript """ & strPath & """"

The reason is, to be able to include quotes inside a string, you need to double them up, to equate to one inside the string.  So you'll see, after cscript, two quote symbols to place on into the command, then another to close that string so you can use strPath, then another doubled one to add that to the end of the string.

Regards,

Rob.
Worked!  Thanks for all the help

Rock On
jamieJ
No worries.  Thanks jamieJ

Rob.
i just wanted all the drive to be deleted and add... i don't want the echo telling me which is being deleted and which is added... how do i remove that?