?
Solved

Beginning to learn VB scripting...some help

Posted on 2011-02-20
3
Medium Priority
?
987 Views
Last Modified: 2012-06-27
I am just learning how to vbscript, I needed a script that maps and disconnects network drives for users that logon on the domain. I found the following scripts and they work, I just need help understanding the actual code...if anyone can please help me

To map a network drive

Set objNetwork = CreateObject("WScript.Network")  ...What is Wscript.Network??? Where does VBscript get this obeject from and what does it do?
Set objShell = CreateObject("WScript.Shell")...Same as above...What is Wscript.Shell?
DriveLetter1 = "P:"
RemotePath1 = "\\alan\Drivers"

objNetwork.MapNetworkDrive DriveLetter1, RemotePath1
objShell.PopUp "Drive " & DriveLetter1 & " connected successfully."

Wscript.Quit

' End of example VBScript

To disconnect a network drive

Option Explicit       .... What does this mean?
Dim objShell, objNetwork, DriveLetter1   ...What is dim?
DriveLetter1 = "P:"

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

objNetwork.RemoveNetworkDrive DriveLetter1
objShell.PopUp "Drive " & DriveLetter1 & " disconnected."

Wscript.Quit

' End of example VBScript

Thanks guys
0
Comment
Question by:Network_Padawan
3 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34939303
WScript.Network is a scripting object that provides methods and properties that allow for manipulation of network objects see here :
http://msdn.microsoft.com/en-us/library/s6wt333f(v=vs.85).aspx

WScript.shell allows you to manipulate environment variables and the like see here :
http://msdn.microsoft.com/en-us/library/aew9yb99(v=vs.85).aspx

Option Explicit means that all variables have to be declared see here :
http://msdn.microsoft.com/en-us/library/t7zd6etz(v=vs.85).aspx

DIM is used to dimension or declare variables see here :
http://msdn.microsoft.com/en-us/library/t7zd6etz(v=vs.85).aspx
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 34939320
Hi,

WScript.Network and WScript.Shell are ActiveX Automation objects that reside in the system file wshom.ocx

They exists to allow automation tasks, like VBScript, to use system objects to operate with.

When you create an object like this, with the CreateObject method, you must use the Set keyword before the variable you assign it to.

DriveLetter1 and RemotePath1 are user defined variable names that are used, in this case, to hold string values.

MapNetworkDrive is a method of the WScript.Network object that allows you to map drives:
http://msdn.microsoft.com/en-us/library/8kst88h6(v=vs.85).aspx

PopUp is a method of the WScript.Shell object that allows you to present a message to the user with a specificed timeout:
http://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.85).aspx

WScript.Quit instructs the script to terminate immediately.

Option Explicit:
"When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs."
http://msdn.microsoft.com/en-us/library/bw9t3484(v=vs.85).aspx

Dim is used to declare names of variables that are to be used in the code, and Dim statements must exist when you use Option Explicit:
http://msdn.microsoft.com/en-us/library/t7zd6etz(v=vs.85).aspx

RemoveNetworkDrive is a method of the WScript.Network object that allows you to remove the mapping of network drives:
http://msdn.microsoft.com/en-us/library/d16d7wbf(v=vs.85).aspx

Also, use a proper editor when possible, such as VBSEdit: www.vbsedit.com

Regards,

Rob.
0
 

Author Closing Comment

by:Network_Padawan
ID: 34939511
Awesome! Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question