Contents for Ezine 61 - Objects and Methods Explained
I'll let you into a secret, when I start a new scripting job, I look for the nearest script in my script folder and amend the existing code.
While the finished product is nothing like the first copy, nevertheless that first script was a useful launch pad. My point is that by understanding how the script is put together, you can take a basic script and extend its capabilities to
tackle a wide range of new tasks.
This week's mission is to explain the structure and keywords of VBScript. The central plank of all modern scripting is the object. Whilst I call the script language VBScript,
the executable is called WScript.exe. WScript corresponds to the Windows Scripting Host (WSH). WSH is what interprets each line in our .vbs file. In turn, this knowledge explains why we use
the syntax "WScript.Network", to create the object for our scripts.
WScript / VBScript objects
- Network (As in network drive or printer)
- Shell (As in Windows Shell - Run a program)
- File (File System Object - FSO)
- Active Directory, User, Group or Computer (LDAP//:domainame)
- WMI (Disk, memory, event viewer)
If we humans are determined by our genes, then each scripting objects has its own personality as defined by its methods. For example, file objects have read, write and save methods, whereas network objects have MapNetworkDrive and SetDefaultPrinter
methods.
How do we manipulate these objects? We obtain these object with the following verbs:
- GetObject
- CreateObject
- Enum
What do we do with the objects?
Once we have ' Gotten ' the object, then we put the object to work with methods. I do love VBscript methods and I exhort you to collect, savour and understand methods.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
As a vehicle to demonstrate VBScript objects, I could have chosen WMI or Active Directory objects. However, I have chosen to feature a
network printer script from the ebook. Instructions - Copy and paste the script below into notepad. Alternatively, get a trial copy of OnScript.
- Save the file with .vbs extension e.g. defaultprn.vbs
- Change the server \\lucy4 to a server on your network. Similarly, make sure that you alter the printer name to a printer on that server.
- Important: if you have a local printer, for example Epson (rather than a network share), then simplify the printer name to: strPrintServer = "Epson".
- Double click and then open explorer and check the printer folder.
' VBScript to set the local Default Printer ' Guy Thomas January 2005. ' ---------------------------------------- Option Explicit Dim objPrinter, strPrintServer
strPrintServer =" \\ lucy4\HP Laser 4L"
Set objPrinter = CreateObject("WScript.Network") objPrinter.SetDefaultPrinter strPrintServer
WScript.Quit
Learning Points
Note 1: Observe how we generate a network object with the command: Set objPrinter = CreateObject("WScript.Network"). In particular, check how the VBScript uses the 'Set'
command when you create a new object.
Note 2: Once we have objPrinter then we can manipulate it with the SetDefaultPrinter method.
If there is no such printer as "\\ lucy4\HP Laser 4L", then the script fails. We can trap this error message by adding the error correcting code.
' VBScript to set the local Default Printer ' Guy Thomas January 2005. ' ---------------------------------------- Option Explicit Dim objPrinter, strPrintServer
strPrintServer ="\\lucy4\HP Laser 4L"
On Error Resume next Set objPrinter = CreateObject("WScript.Network") objPrinter.SetDefaultPrinter strPrintServer
' Error
correcting Section If err.number = vbEmpty Then WScript.Echo
"Check the printer folder " ElseIf err.number = -2147352567 Then Wscript.Echo "Problem with name of printer or server" Else Wscript.Echo "Unknown Problem" End If WScript.Quit
Learning Points
Note 1: My error number was -2147352567, meaning that the printer does not exist. If you get a different error message number, then add additional elseif lines with suitable .echo commands.
Note 2: See how the script uses the err.number property; normally this value should be blank or vbEmpty. Summary - Objects and Methods
Study how VBScript creates objects. Appreciate the variety of objects, for example, network, file and Active Directory. Once you have a handle on the object then you can manipulate it with
methods such as SetDefaultPrinter.
My not-so-hidden agenda is to show you how to alter scripts to suit your own servers.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|