• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

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!


0
JamesonJendreas
Asked:
JamesonJendreas
1 Solution
 
chandru_solCommented:
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
0
 
SysExpertCommented:
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

0
 
RobSampsonCommented:
You can also use something like the following:

'==============
Option Explicit

' CHANGE THE %comspec& /k TO %comspec% /c BELOW TO MAKE
' THE PROMPT DISAPPEAR WHEN SCRIPT HAS FINISHED
' YOU CAN ALSO CHANGE THE objShell.Run(strCommand), 1, True TO
' objShell.Run(strCommand), 0, True TO HIDE THE COMMAND PROMPT
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 Remove Drive
Dim bForce, bUpdateProfile

'Dim arrDrives for drives and shares
Dim arrDrives, strDriveDetails, strDrive, strShare

'Declare Remove Drive
bForce = "True"
bUpdateProfile = "True"

Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("Shell.Application")

'Declare drive array, separated by the tilde (~) to it's share,
arrDrives = Array(_
      "S:~\\ginkgo\shared", _
      "G:~\\rainbowserver\graphics", _
      "Y:~\\server2\" & objNetwork.UserName & "$" _
      )

On Error Resume Next
' Section to map the network drive
For Each strDriveDetails In arrDrives
      strDrive = Split(strDriveDetails, "~")(0)
      strShare = Split(strDriveDetails, "~")(1)

      ' Removes strDriveLetter, with bForce, pUpdate Profile
      WScript.Echo "Attempting to remove connection to " & strDrive
      objNetwork.RemoveNetworkDrive strDrive, bforce, bUpdateProfile
      If Err.Number <> 0 Then
            WScript.Echo "Error removing " & strDrive & " - " & Err.Number & ": " & Err.Description
            Err.Clear
      Else
            WScript.Echo strDrive & " removed"
      End If

      ' Map the drive
      WScript.Echo "Attempting to map " & strDrive & " to " & strShare
      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 & " was successfully mapped."
      End If
Next

WScript.Quit
'==============

Please make that this array the correct details:
arrDrives = Array(_
      "S:~\\ginkgo\shared", _
      "G:~\\rainbowserver\graphics", _
      "Y:~\\server2\" & objNetwork.UserName & "$" _
      )

of which the parameters are:
"<DRIVE>" plus tilde plus "<\\SERVER\SHARE>"
so above, the Z drive is Z: plus tilde plus \\server1\allstaffshare

Regards,

Rob.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
JamesonJendreasAuthor Commented:
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!
0
 
RobSampsonCommented:
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.
0
 
JamesonJendreasAuthor Commented:
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
0
 
RobSampsonCommented:
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.
0
 
JamesonJendreasAuthor Commented:
Worked!  Thanks for all the help

Rock On
jamieJ
0
 
RobSampsonCommented:
No worries.  Thanks jamieJ

Rob.
0
 
pstrawserCommented:
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?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now