Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

August 01, 1999 12:00 AM

Scripting 101, Part 3

Windows IT Pro
InstantDoc ID #5683
Rating: (1)
Downloads
5683.zip

Working with objects

Last month in my Scripting 101 series, I explained how to declare and initialize variables in Windows Scripting Host (WSH) scripts that you create using VBScript. This month, I discuss the most important job you perform in the script body: working with objects.

Objects
Regardless of the tasks your scripts perform, at some point you'll create and interact with objects. Whether implicitly or explicitly, all WSH scripts create objects. Understanding what an object is, what an object provides, and how to create and interact with objects is key to successfully using WSH. Although working with objects might be old hat to those of you with a Visual Basic (VB) background, quite the opposite is true for individuals with only a batch file background.

Objects vs. Utilities
WSH scripts and batch files have numerous significant differences, but none more important than the transition away from utilities and toward objects. Historically, developers used batch files to glue together system, resource kit, and third-party command-line utilities. Provided that you found utilities to support all the tasks that you wanted to perform, this approach worked fine. Unfortunately, developers often hit roadblocks along the way. If the utility the batch file called failed to provide some required option, the developer had to scour the Internet for another utility to fill the void. Sound familiar?

Figure 1, page 164, shows how Windows NT's graphical applications and utilities call Win32 APIs to run the task that the application or utility performs. Unfortunately, Win32 APIs aren't suitable for ad hoc scripts, and calling Win32 APIs from scripts is almost impossible. Therefore, NT limited scripting to the functionality that the utilities provided. This limitation led to a perception that Windows was scripting-challenged (especially compared to UNIX). In fact, the limited functionality led to a proliferation of third-party and open-source scripting tools to address the lack of Win32 scriptability.

So what has changed? As I've mentioned, part of the solution is WSH. WSH interacts with objects, specifically COM automation objects, to eliminate the need to find utilities that support all the tasks you want to perform. WSH provides a lightweight scripting environment capable of calling the same set of system services that the native NT graphical applications call.

The more significant piece of the solution pertains to the API. As Figure 2, page 164, shows, Microsoft is using COM to expose almost all system services in such a way that scripts and more traditional system programming languages (e.g., C++) call the same set of interfaces to perform a desired task. How is this standardization possible? With COM, system and application developers can develop their applications so that COM-aware scripting environments, such as WSH, can interact with and control COM-enabled services and applications.

For example, in addition to abstracting multiple and disparate directory services, Active Directory Service Interfaces (ADSI) lets scripts call the same ADSI services that the native Windows 2000 (Win2K) Active Directory (AD) management tools use. Similarly, Windows Management Instrumentation (WMI) provides COM-savvy scripts with the ability to fetch, read, and set the same set of Win32 system and performance data that Systems Management Server (SMS) 2.0 and other Web-Based Enterprise Management (WBEM)-enabled systems management applications access. You leverage these services by utilizing these COM objects in your scripts. The first step is to understand what an object is.

What Is an Object?
An object is simply a thing. Each object has a set of characteristics or attributes that describe the object and the actions it performs. Network OSs, messaging systems, applications, and programming languages generally classify objects in terms of a user, a group, a computer, a printer, a network, or an application. These entities are all objects in the IT world. Each of these objects has a set of properties that uniquely describe it. Each object might also perform an action using its methods. So in the computer world, properties describe an object, and objects use methods to perform actions.

Let's consider a user object. Properties of a user object might include username, full name, and password. A user object might use methods to create, delete, or set passwords. You must have a user object before you can interact with it, so let's take a look at how to create objects.

Creating Objects
You might be interested to learn that WSH is a set of objects. WSH includes three primary objects: WScript, Network, and Shell. The WScript object provides the script execution environment. The Network object provides properties and methods typically associated with network and printer connections. The Shell object provides methods and properties associated with the desktop shell, environment variables, and the Registry.

Before you can access an object's methods and properties, you must first create an instance of the object. As part of the script execution environment, WScript includes two methods, CreateObject and GetObject, that you can use to create objects. Using these methods is a concept that represents a radical departure from calling utilities inside of batch files.

VBScript also provides CreateObject and GetObject as functions, which you'll generally want to use rather than the WScript methods. Using WScript's CreateObject or GetObject methods adds an additional layer of work and is slightly less efficient than using the analogous VBScript functions. WScript includes these methods to support third-party script engines that might not provide a mechanism to create objects.

Before we look at some examples, you need to be aware of a couple of exceptions to the requirement that you create the object before you use it. The first exception concerns WScript. Recall that I said every WSH script creates objects. Running a WSH script implicitly creates a WScript object. Therefore, you can call any WScript method or read any WScript property without an explicit call to CreateObject.

The VBScript Err object is the second object that WSH scripts automatically create. For more information about the VBScript Err object, see "Scripting Solutions: Scripting 101, Part 2," July 1999. Now, let's create some objects. A good place to start is with syntax and definitions.

Related Content:

ARTICLE TOOLS

Comments
  • Anonymous User
    8 years ago
    Dec 20, 2004

    You can use the SOAP to call the COM object remotely

  • rohit
    9 years ago
    Dec 06, 2003

    Hi bobby,
    this article is great, what is was looking for
    now after reading your article i have more questions to ask if u can tell me i am specifying here it will be great
    Does CreateObject function creates the object from the remote machine where Com is installed as in process(dll) or it works for only exe(out process)?
    and if Getobject can be used for creating the objects from remote machines then how?

  • John Toohey
    12 years ago
    Apr 10, 2000

    The 4 Scripting articles where great. Information at just the right time for us. We are just starting to get into WSH.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.