?
Solved

Beginning to learn VB scripting...some help

Posted on 2011-02-20
3
Medium Priority
?
984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

777 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