Executing Windows Commands

[ LiB ]

Executing Windows Commands

Okay, enough for shortcuts. Let's look at how to execute Windows commands from within your JScripts. To do this, you will have to use the WshShell object's Run() method. As you will see, using this method, you can create scripts that automate the execution of any Windows command or command-line utility.

Controlling Windows Services

Windows NT, XP, 2000, and 2003 provide much of their functionality in the form of services. A service is simply a software program that provides the operating system with a specific set of capabilities. For example, the Printer Spooler service handles all the work involved in printing print jobs. Likewise, the Plug and Play service is responsible for detecting and helping to install new hardware.

Windows automatically starts a large number of services, whether you use them or not. Each active service consumes a small portion of Windows CPU and memory resources. By stopping services that you do not need to use, you may be able to improve the performance of your computer just a bit.

NOTE

To see a list of services running on your computer, right-click on My Computer and select Manage.Then expand the Services and Applications node on the console tree that will be displayed and select Services. A list of installed services will be displayed on the right side of the console, along with their status and a brief explanation of their purposes.

You can control the execution of Windows services from the Windows command prompt using any of the following commands.

  • NET START . Starts a Windows service.

  • NET PAUSE . Pauses the execution of a Windows service.

  • NET CONTINUE . Resumes the execution of a paused Windows service.

  • NET STOP . Stops a Windows service.

The following JScript statements demonstrate how to use the WshShell object's Run() method to stop the Windows Alerter service:

 var wshObject = WScript.CreateObject("WScript.Shell"); wshObject.Run("net stop Alerter", 0, "True"); 

Once the service is stopped , the following statements can be used to start it again:

 var wshObject = WScript.CreateObject("WScript.Shell"); wshObject.Run("net start Alerter", 0, "True") 

Creating New User Accounts

Windows NT, XP, 2000, and 2003 operating systems also provide command-line support for the creation and configuration of user accounts via the following commands:

  • net user . Provides the capability to create and delete user accounts from the Window command line.

  • net group . Provides the capability to add user accounts to global group accounts.

  • net localgroup . Provides the capability to add user accounts to local groups.

The following JScript statements demonstrate how to create a new user account:

 var wshObject = WScript.CreateObject("WScript.Shell"); cmdResult = wshObject.Run("net user WilliamF qwerty /add", 0); 

In this example, an account named WilliamF is created with a temporary password of qwerty . The user account is established on the computer where the script runs. If the computer is part of a Windows-based local area network and you have administrative privileges over the Windows domain, you can modify the previous example by adding the /Domain switch, as demonstrated below, to create the user account as a domain-level account.

 var wshObject = WScript.CreateObject("WScript.Shell"); cmdResult = wshObject.Run("net user WilliamF qwerty /add /domain", 0); 

[ LiB ]


Learn JavaScript In a Weekend
Learn JavaScript In a Weekend, Second Edition
ISBN: 159200086X
EAN: 2147483647
Year: 2003
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net