VBScript:  Select Case

detox1978
detox1978 used Ask the Experts™
on
Hi All,

How do I run multiple lines of code in a select case?


Select Case UserString
    Case "user1"
         WSHNetwork.MapNetworkDrive "L:", "\\server1\accounts",True
         WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True
         WSHNetwork.MapNetworkDrive "X:", "\\server1\projects",True

    Case "user2"
         WSHNetwork.MapNetworkDrive "L:", "\\server1\accounts",True
         WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True

End Select

Open in new window



The above code will only map the L drive.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
what os is on server 1

Author

Commented:
Windows 2008, but I may also map to a Windows 2003 server.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I would expect having to enclose your statements in BEGIN / END.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Can you give  me a example?


thanks
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Sorry, I was wrong with that. You do not need anything. See this example, it prints out both lines.
userstring = "user1"

Select Case UserString
    Case "user1"
         wscript.echo "one"
         wscript.echo "three"

    Case "user2"
         WSHNetwork.MapNetworkDrive "L:", "\\server1\accounts",True
         WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True

End Select

Open in new window

Probably you get an error when mapping drives, and the script is terminated.

Commented:
I think it will work if you do it this way

Select Case UserString
    Case "user1"
         WSHNetwork.MapNetworkDrive "L:", "\\server1\accounts",True ,
         WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True ,
         WSHNetwork.MapNetworkDrive "X:", "\\server1\projects",True

    Case "user2"
         WSHNetwork.MapNetworkDrive "L:", "\\server1\accounts",True ,
         WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True

End Select


notice the extra comma you may need to encapsulate the whsnetwork comand as it has a ,
this way all the commands are on the same line

Author

Commented:
Turns out the drive was being mapped twice so exited
Anuradha GoliSystems Development / Support Specialist

Commented:
Instructions for multiple Mapped Network Drives

Copy and paste the script below into notepad.
Change the server name from "\\alan to the name of your server.
Make sure that you have not one, but two network shares on your server.
Save the file with .vbs extension e.g. MNDMultiple Drives.vbs.
Double click the logon script and check in your Windows Explorer for two new drives called : drivers on 'alan' (M:) and download reports on 'alan' (P:).
' TwoMap.vbs - Map Network Drive to M: and P:
' Example of VBScript Mapping two drives in one script.
' ----------------------------------------------------' 
Option Explicit
Dim objNetwork, strRemotePath1, strRemotePath2
Dim strDriveLetter1, strDriveLetter2 

strDriveLetter1 = "M:" 
strDriveLetter2 = "P:" 
strRemotePath1 = "\\alan\drivers" 
strRemotePath2 = "\\alan\downloads reports" 

Set objNetwork = CreateObject("WScript.Network") 

' Section which maps two drives, M: and P:
objNetwork.MapNetworkDrive strDriveLetter1, strRemotePath1
objNetwork.MapNetworkDrive strDriveLetter2, strRemotePath2

' Extra code just to add a message box
WScript.Echo "Map drives " & strDriveLetter1 & " & " & strDriveLetter2

Wscript.Quit

' End of Windows Logon Script Example

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial