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.Shel l")
set objNetwork = CreateObject("WScript.Netw ork")
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FileSystemObje ct")
IF objFso.DriveExists(strDriv eLetter1) then AlreadyCOnnected = true
If AlreadyConnected = true then
objNetwork.RemoveNetworkDr ive strDriveLetter1
objShell.PopUp "Disconnecting Shared Drive"
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive strDriveLetter1, strRemotePath1
If objFso.DriveExists(strDriv eLetter2) then AlreadyConnected2 = true
If AlreadyConnected2 = True Then
objNetwork.RemoveNetworkDr ive 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!
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.Shel
set objNetwork = CreateObject("WScript.Netw
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FileSystemObje
IF objFso.DriveExists(strDriv
If AlreadyConnected = true then
objNetwork.RemoveNetworkDr
objShell.PopUp "Disconnecting Shared Drive"
objNetwork.AddNetworkDrive
objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive
If objFso.DriveExists(strDriv
If AlreadyConnected2 = True Then
objNetwork.RemoveNetworkDr
objShell.PopUp "Disconnecting Graphics"
objNetwork.AddNetworkDrive
objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive
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!
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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.
Regards,
Rob.
ASKER
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.FullNa me, 11)) = "wscript.exe" Then
' strPath = WScript.ScriptFullName
' strCommand = "%comspec% /k cscript" " & strPath &"
' set objShell = CreateObject("WScript.Shel l")
' 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.Netw ork")
set objShell = CreateObject ("WScript.Shell")
arrDrive = Array(_
"S:~\\Ginkgo\shared",_
"G:~\\Rainbowserver\graphi cs"_
)
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.RemoveNetworkDr ive 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
Option Explicit
Dim strPath, strCommand, objShell
'If LCase(Right(WScript.FullNa
' strPath = WScript.ScriptFullName
' strCommand = "%comspec% /k cscript" " & strPath &"
' set objShell = CreateObject("WScript.Shel
' 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.Netw
set objShell = CreateObject ("WScript.Shell")
arrDrive = Array(_
"S:~\\Ginkgo\shared",_
"G:~\\Rainbowserver\graphi
)
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.RemoveNetworkDr
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
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.
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.
ASKER
Worked! Thanks for all the help
Rock On
jamieJ
Rock On
jamieJ
No worries. Thanks jamieJ
Rob.
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?
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.Shel
set objNetwork = CreateObject("WScript.Netw
On Error Resume Next
AlreadyConnected = False
AlreadyConnected2 = False
set objFso = CreateObject ("Scripting.FileSystemObje
IF objFso.DriveExists(strDriv
If AlreadyConnected = true then
objNetwork.RemoveNetworkDr
objShell.PopUp "Disconnecting Shared Drive"
objNetwork.AddNetworkDrive
objShell.PopUp "Reconnecting To Shared Drive"
Else
objNetwork.AddNetworkDrive
If objFso.DriveExists(strDriv
If AlreadyConnected2 = True Then
objNetwork.RemoveNetworkDr
objShell.PopUp "Disconnecting Graphics"
objNetwork.AddNetworkDrive
objShell.PopUp "Reconnecting Graphics"
Else
objNetwork.AddNetworkDrive
End If
End IF