Downloads
7707.zip

Do you have a scripting-related question or problem? You can send your question or problem to bobwells@winntmag.com.

I'm trying to programmatically create a Windows NT workstation computer account using Windows Scripting Host (WSH), VBScript, and Active Directory Service Interfaces (ADSI) 2.5. The new computer account appears in Server Manager for Domains after I run my script. However, when I try to add the workstation to the domain with the Control Panel Network applet, Identification Changes dialog box, I receive an error that reads Unable to connect to the domain controller for this domain. Have your administrator check your computer account on the domain. What am I missing?

You can't add the computer to the domain after running your script because you never set the initial computer account password. The script in Listing 1 demonstrates the process of creating the computer account and setting the initial computer account password. In the script, you need to change the following values to match your environment:

  • DomainName—the name of the domain that will contain the new computer account
  • Administrator—the name of the user account that has sufficient domain privileges to create computer accounts
  • Password—the previous user account's password
  • ComputerName—the name of the new computer account

I need to update an existing desktop shortcut for several thousand users. I used WSH to create the shortcut. Can I use WSH to update the existing shortcut, or do I need to delete the existing shortcut and create a new one?

WSH's Shell object doesn't provide a method to update an existing shortcut. As a result, you need to delete the existing shortcut and create a new one. Unfortunately, the Shell object doesn't provide a method to delete shortcuts either, so you need to use the Microsoft Scripting Runtime library's FileSystemObject to delete the existing shortcut. The example code in Listing 2 demonstrates deleting a desktop shortcut to Notepad. You can modify the example to delete your existing shortcut and then create a new shortcut with the Shell object's CreateShortcut method.

I was reviewing a WSH script that first navigates to a Web site and then uses sinking event subroutines. In this script, the scriptwriter calls Microsoft Internet Explorer (IE) but not the sinking event subroutines. What is activating these subroutines if the scriptwriter never calls them? Is the browser automatically calling them, or are they always present after you load and execute a script?

Similar to the way Windows applications (e.g., Microsoft Office, IE, Visio) can expose a programmable object model, applications can also expose events that represent asynchronous actions the application performs. An application triggers events in response to some other event or circumstance. Sinking events is the process of connecting to a special interface that the application implements. Through this interface, you receive any events that the application fires.

You use the CreateObject's strPrefix parameter to connect to the application's outgoing interface. This optional second parameter identifies a string that the system couples with the event name to call custom event handlers. You subsequently use the string prefix appended with the event name to create a custom event handler (i.e., a subroutine). From that point forward, providing you've defined the custom event handler, the system invokes the subroutine whenever the application fires the corresponding event. If you don't define a custom event handler for an event, the application calls a default handler.

In the case of IE, the system invokes subroutines whenever the application fires an event such as DownloadBegin, DownloadComplete, or DocumentComplete. IE has many intrinsic events, and you can find information about them on the Microsoft Developer Network (MSDN) Online Web Workshop at http://msdn.microsoft.com/ workshop/default.asp. You can find more information about CreateObject's strPrefix parameter in the WSH 2.0 documentation.

I need to manipulate different files, depending on the day of the week. For example, on Monday, I need to delete X files, and on Tuesday, I need to delete Y files. How can I script these file-manipulation tasks?

As Listing 3, page 9, shows, you can use VBScript's Now and DatePart functions in conjunction with a Select Case statement. You first use the Now function to obtain the current date and the DatePart function to return only the day of the week of that date. You then compare the integer that the DatePart function returns to VBScript's seven weekday constants to determine the day of the week. The Select Case statement compares the return value against a list of Case expressions representing the seven weekday constants. When the Select Case statement finds a match, it carries out the file-manipulation task that the Case clause defines. You can use the optional Case Else clause to call an error handler if no match occurs. (For more information about Select Case statements, see Dino Esposito, "Understanding VBScript: Statements," September 1999.)