Solved

Beginning to learn VB scripting...some help

Posted on 2011-02-20
3
980 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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