Guy’s Scripting Ezine 61 – Objects and Methods

Contents for Ezine 61 – Objects and Methods Explained

 ♣

This Week’s Secret

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

This Week’s Mission – to understand VBScript objects

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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 1 – To set a default printer.

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

  1. Copy and paste the script below into notepad. Alternatively, get a trial copy of OnScript.
  2. Save the file with .vbs extension e.g. defaultprn.vbs
  3. 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.
  4. Important:  if you have a local printer, for example Epson (rather than a network share), then simplify the printer name to:  strPrintServer =  "Epson".
  5. 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.

Example 2 – Adding error correcting code

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.

See more about VBScript techniques

VBScripts  • WMI  • Ezines  • Logon Scripts  • Tool Kit  •SLA Monitor  • Ezine 26 Msg Box

Ezine 41 VBS Select case  • Ezine 46 MsgBox  • Free Response Time Tool

Ezine 55 VBS Select case  • Ezine 61 Objects methods   •Ezine 70 GetEx Split