Subscribe to Windows IT Pro
February 13, 2006 12:00 AM

WSH, Part 3: Windows Script Components

Create reusable code by writing your own COM objects in script
Windows IT Pro
InstantDoc ID #49092
Rating: (0)
Downloads
49092.zip

A Real-World Example
"WSH, Part 2: .wsf Files" presents a WSF script that reports the members of a group. To continue this theme, I decided to present a script component that provides the same functionality. The Penton.GetMembers component provides two write-only properties, GroupName and Domain-Name, and one method, Members. The Members method returns a sorted array containing the members of the group. Listing 2 shows the VBScript implementation of the GroupName and DomainName properties and the Members method. Listing 2 is only an excerpt from the Penton.GetMembers component. You can download the entire component from the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 49092 in the InstantDoc ID text box, then click the 49092.zip hotlink.

At callout A in Listing 2, note how the Members method calls the SortVBArray JScript function, which I created earlier in the component. This function returns a sorted copy of the specified VB safe array. (A safe array, also known as a VB array, is the array type used by VBScript.)

To use the Penton.GetMembers component in a script, create an object instance of it. Next, set the GroupName property to the name of the group (it defaults to Domain Admins). If the group exists in a domain other than the current logon domain, set the DomainName property as well. Finally, call the Members method to retrieve the array containing the members of the group. UseGetMembers.vbs, shown in Listing 3, uses the object to list the members of the Domain Users group in the current domain. (Be sure to execute this script with the CScript host because it uses WScript.Echo to output each member.) Note that the GetMembers.wsc file must be registered on the system before the script in Listing 3 will work.

Creating Your Own Components
This article provides only a brief overview of script components and doesn't cover some of their more advanced features. My goal was to explain the basics and why they might be useful in your own scripting tasks. Use the examples in this article to start creating your own components.

ADDITIONAL RESOURCES

Windows Script Component Wizard The Windows Script Component Wizard generates a prototype WSC file based on your inputs to the wizard. Even if you don't click the wizard's Finish button, you can still copy the GUID it creates and paste it into your component. http://www.microsoft.com/downloads/details.aspx?familyid=408024ed-faad-4835-8e68-773ccc951a6b

Windows Script 5.6 Documentation This download provides documentation for VBScript, JScript, WSH, the scripting runtime, and script components in one searchable Help file. http://www.microsoft.com/downloads/details.aspx?familyid=01592c48-207d-4be1-8a76-1c4099d7bbb9

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

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