Lecture 3
Logon Scripts
Lecture 3
Logon Scripts
From language point of view, VBScript is not a difficult programming language. Indeed, it is much less complicated than those popular general purpose programming languages, such as C, C++, and Java. However, use of the language requires knowledge of the operating system.
From this lecture we start to learn Windows scripting techniques by performing a set of administrative tasks. The first task we are going to do today is to write a logon script to allocate network resources to users when they login.
Key words
WSH Object, WScript.Network, Network User, Network Drive, User Group, WMI
Reference to textbook chapters
The material covers the textbook chapters 3, 4 and part of chapters 11 and 27. (Don Jones, VBScript, WMI and ADSI unleashed : using VBSscript, WMI, and ADSI to automate Windows administration eBook: Chapter 3. The Components of a Script; Chapter 4. Designing a Script).
WSH objects
Download the script userName.vbs and run it.
The main part of the code is:
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
Dim strComputer, strDomain, strUser
strComputer = objNetwork.ComputerName
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName
The first statement declares a variable objNetwork. The second statement assigns an object to the variable.
Microsoft operating system, more accurately the WSH (Windows Script Host), provides a few objects for typical scripting purposes of performing administrative tasks. WScript.Network is one of most useful WSH Objects (also called WScript Objects). The following is the hierarchy of WSH objects:
Learn more about WSH Objects and Associated Tasks from Microsoft website.
The Network object
WScript.Network is an object that provides network information, such as computer name, domain name and username, as well as the functions of mapping drives and printers. We use the following statement to create an instance of the object:
WScript.CreateObject("WScript.Network")
WScript.Network has a set of properties and methods (See msdn online documents or textbook page 174-179):
•ComputerName Property
•UserDomain Property
•UserName Property
•AddPrinterConnection Method
•AddWindowsPrinterConnection Method
•EnumNetworkDrives Method
•EnumPrinterConnections Method
•MapNetworkDrive Method
•RemoveNetworkDrive Method
•RemovePrinterConnection Method
•SetDefaultPrinter Method
Using these properties or methods is not hard. For instance, suppose you assign the object WScript.Network to variable objNetwork, i.e.,
objNetwork = WScript.CreateObject("WScript.Network")
Please note that the real name which corresponds to the WScript.WshNetwork object is "WScript.Network". There is no a property of the WScript object that we can use to return the WScript.WshNetwork object, but using CreateObject("WScript.Network").
To get the computer name you are using, you can simply write
WScript.echo objNetwork.ComputerName
or assign objNetwork.ComputerName to a variable as we did in userName.vbs.
Map a network drive
Whenever you login to a lab computer, you automatically get a U: drive. Obviously it is not a local drive. In fact it is a network drive that is mapped to a file folder located on a remote computer to which you have access. As a network user, you might not know which file folder is available to you. However, if you are an administrator, you have control over your network storage.
Suppose that you are an administrator, and jamie is one of the network users. You have allocated a network folder to jamie as follows:
\\server\users\jamie
Now, you want to map X: drive of the machine to the network folder above so that whenever jamie logs on to one of the computers in the network he can access the network folder through X: drive. You can use the following script for the job:
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "X:", "\\server\user\jamie"
Of course you can't test the above code on your computer unless you have a network folder "\\server\user\jamie". If you want to test the program now on your computer, follow the instructions here to find a shared network folder provided that your computer is connected to a network (you can do this in a SCM lab).
To disconnect a network drive, use the following code:
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive "X:"
See msdn online documents for more details of the task.
To check the information about all connected network drives, use the following code:
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objDrives = objNetwork.EnumNetworkDrives
For i =0 to objDrives.count - 1 Step 2
   WScript.echo objDrives.Item(i) & ":" & objDrives.Item(i+1)
Next
Map a network printer
Assume that you have two networked computers at home but have only one network-ready printer. If you want to share the printer between the two computers, the following script can help you to do this job:
Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\server\\printerName"
Similarly, you can remove a network printer connection and list the information of all connected printers.
Get user's groups
Gaining group information is a bit harder because the same user can belong to different groups. The following code, GetGroup.vbs, can list all the groups a user belongs to:
Set oNetwork = CreateObject("WScript.Network")
sDomain = oNetwork.UserDomain
sUser = oNetwork.UserName
Set oUser = GetObject("WinNT://" & sDomain & "/" & sUser & ",user")
For Each oGroup In oUser.Groups
MsgBox oGroup.Name
Next
GetObject is a built-in method of VBScript, which returns user a reference to an object, given the user domain and user name. The property Groups of the object contains all the roles of the user.
Get IP address
The following script can get IP address:
Set oWMI = GetObject("winmgmts:" & "\\.\root\cimv2")
Set resultObj = oWMI.ExecQuery("select IPAddress from " & _
"Win32_NetworkAdapterConfiguration" & " where IPEnabled = TRUE")
For Each IPAddress in resultObj
If IPAddress.IPAddress(0) <> "0.0.0.0" Then
myIP = IPAddress.IPAddress(0)
Exit For
End If
Next
MsgBox myIP
Note that we have used a WMI query to get the IP address (see Chapter 18). We will learn more about it later. The information we have collected is now enough for writing a simple logon script. Try to write your logon script now. Don't be shy away from searching on the Web.