PowerShell Ezine, Logon Scripts

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:  A Free Trial of the Orion Network Configuration Monitor (NCM) v6Review of Orion NCM v6

Config management of routers, switches and firewalls is fun with NCM (Network Configuration Manager.  Furthermore, it can help to achieve your compliance policy, for example, pinpoint devices not backed up and discover access infringements or even weak passwords.  This Solarwinds NCM suite can not only detect violations, but also upload scripts to correct the problem.

Most computer problems arise from configuration changes.  Thus it makes sense to get a proper monitoring system so that you can double-check that that all the settings confirm to your security policy.

Download your free trial of Orion's Network Configuration Monitor.

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.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

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.


 *


Google

Web  This website

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved.

Please report a broken link, or an error.