Solved

Beginning to learn VB scripting...some help

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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