Solved

Beginning to learn VB scripting...some help

Posted on 2011-02-20
3
979 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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
Awesome! Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now