DESKTOP

Windows Server 2008 Server Core : Working with Scripts - Using the Scripting Objects

10/23/2012 3:03:45 AM
Some developers require months to learn everything there is to know about the scripting language and the objects the language controls. This section helps you understand the various objects that WSH supports. You won't become a guru overnight. As you learn more, you'll be able to create scripts of increasing complexity. Scripting isn't hard to learn, but you need to take your time and learn it a bit at a time.

WSH depends on objects that Microsoft supplies as part of Windows to perform tasks such as outputting text to the display.

NOTE

An object consists of three elements: properties, methods, and events. A property describes the object and determines its functionality. For example, you can say an apple is red. In this case, red is a property of the apple. However, you can also paint the apple blue. In this case, you changed the color property of the apple to another value. Methods are actions you can perform with an object. For example, looking at an apple again, you can say that it has a grow method. As the tree applies the grow method, the apple becomes larger. Events are responses to specific object actions. For example, when the apple becomes mature, it raises the "color" event to tell you that it's ripe.

Writing scripts in Windows means knowing the object you want to work with, the properties that object provides, and the methods you can use with that object. You don't have to know about every object. In fact, you'll find it easier to learn about one object at a time. The following sections tell you about the main scripting object, WScript, and some of the supporting objects it contains.


1. Working with the WScript Object

The WScript object is the main object for WSH. You'll access every other object through this one. The following list tells you about the properties that the WScript object supports.


Application

Provides you with access to a low level interface for WScript. An interface is a pointer to a list of functions that you can call for a particular object. Only advanced programmers need this property because WSH exposes all of the basic functions for you.


Arguments

Provides a complete list of the arguments for this script. Applications pass arguments on the command line. WSH passes the argument list as an array. You create a variable to hold the argument list, and then access the individual arguments as you would any array. The Arguments.Count property contains the total number of array elements.


FullName

Contains the full name of the scripting engine along with the fully qualified path to it. For example, if you were using CScript, you might get C:\WINDOWS\SYSTEM32\CSCRIPT.EXE as a return value.


Interactive

Returns true if the script is in interactive mode.


Name

Returns the friendly name for WScript. In most cases, this is Windows Script Host.


Path

Provides just the path information for the host executable. For example, if you were using CScript, you may get a return value of: C:\WINDOWS\SYSTEM32\.


ScriptFullName

Contains the full name and path of the script that's running.


ScriptName

Provides just the script name.


Version

Returns the WSH Version number.

Remember that all of these properties tell you about the WScript object. You can also use methods to perform tasks with the WScript object. The following list provides a brief overview of the more important methods you'll use with the WScript object. Note that most of these methods require you pass one or more parameters as input. A parameter is a piece of data the method uses to perform a task.


CreateObject
(strProgID)

Create the object specified by strProgID. This object could be WSH specific like "WScript.Network" or application specific like "Excel.Application".


GetObject
(strPathname[,strProgID])

Retrieves the requested object. strPathname contains the filename for the object you want to retrieve. In most cases, this is going to be a data file, but you can retrieve other kinds of objects as well. As soon as you execute this command, WSH starts the application associated with that object. For example, if you specified C:\MyText.TXT as the strPathname, then WSH may open Notepad to display it. The optional strProgID argument allows you to override the default processing of the object. For example, you may want to open the text file with Word instead of Notepad.


Echo
(AnyArg)

Displays text in a window (WScript) or to the console screen (CScript). AnyArg can contain any type of valid output value. This can include both strings and numbers. Using Echo without any arguments displays a blank line.


GetScriptEngine
(strEngineID)

Registers an alternative script engine such as PerlScript . strEngineID contains the identifier for the script engine that you want to retrieve. You'll need to register the engine using the GetScriptEngine.Register() method before you can actually use it. A script engine also requires you to provide a default extension.


Quit
(intErrorCode)

Exits the script prematurely. The optional intErrorCode argument returns an error code if necessary. You can test for this value using the ErrorLevel clause in batch files.

2. Working with the WScript.WshArguments Object

Whenever you start a script, you have the option of passing one or more arguments to it on the command line. That's where the WshArguments object comes into play. It helps you determine the number of arguments, and then retrieve them as needed. You'll always use the WScript.Arguments property to access this object; it's not directly accessible. The following list describes the properties for this object.


Item
(intIndex)

Retrieves a specific command line argument. intIndex contains the index of the argument that you want to retrieve. The array used to hold the arguments is 0 based, so the first argument number is 0.


Count()

Returns the number of command line arguments.


Length()

Returns the number of command line arguments. WSH provides this property for JScript compatibility purposes.

3. Working with the WScript.WshShell Object

You'll use the WScript.WshShell object to access the Windows shell (the part of Windows that interacts with applications and creates the user interface) in a variety of ways. For example, you can use this object to read the registry or to create a new shortcut on the desktop. This is an exposed WSH object, which means you can access it directly. However, you need to access it through the WScript object like this: WScript.WshShell. The following list describes the WshShell methods.


CreateShortcut
(strPathname)

Creates a WSH shortcut object. strPathname contains the location of the shortcut, which will be the Desktop in most cases.


DeleteEnvironmentVariable
(strName[,strType])

Deletes the environment variable specified by strName. The optional strType argument defines the type of environment variable to delete. Typical values for strType include System, User, Volatile, and Process. The default environment variable type is System.


GetEnvironmentVariable
(strName [, strType])

Retrieves the environment variable specified by strName. Default environment variables include NUMBER_OF_PROCESSORS, OS, COMSPEC, HOMEDRIVE, HOMEPATH, PATH, PATHEXT, PROMPT, SYSTEMDRIVE, SYSTEMROOT, WINDIR, TEMP, and TMP. The optional strType argument defines the type of environment variable to delete. Typical values for strType include System, User, Volatile, and Process. The default environment variable type is System.


Popup
(strText [,intSeconds] [,strTitle] [,intType])

Displays a message dialog box. The return value is an integer defining which button the user selected including the following values: OK (1), Cancel (2), Abort (3), Retry (4), Ignore (5), Yes (6), No (7), Close (8), and Help (9). strText contains the text that you want to display in the dialog box. intSeconds determines how long WSH displays the dialog box before it closes the dialog box and returns a value of −1. strTitle contains the title bar text. The intType argument can contain values that determine the type of dialog box you'll create. The first intType argument determines button type. You have a choice of OK (0), OK and Cancel (1), Abort, Retry, and Ignore (2), Yes, No, and Cancel (3), Yes and No (4), and Retry and Cancel (5). The second intType argument determines which icon Windows displays in the dialog box. You have a choice of the following values: Stop (16), Question (32), Exclamation (48), and Information (64). Combine the intType argument values to obtain different dialog box effects.


RegDelete
(strName)

Removes the value or key specified by strName from the registry. If strName ends in a backslash, then RegDelete removes a key. You must provide a fully qualified path to the key or value that you want to delete. In addition, strName must begin with one of these values: HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG, or HKEY_DYN_DATA.


RegRead
(strName)

Reads the value or key specified by strName from the registry. If strName ends in a backslash, then RegRead reads a key. You must provide a fully qualified path to the key or value that you want to read. In addition, strName must begin with one of these values: HKEY_ CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG, or HKEY_DYN_DATA. RegRead can only read specific data types including REG_SZ, REG_EXPAND_SZ, REG_DWORD, REG_BINARY, and REG_MULTI_SZ. Any other data types will return an error.


RegWrite
(strName, anyValue [, strType])

Writes the data specified by anyValue to a value or key specified by strName to the registry. If strName ends in a backslash, then RegWrite writes a key. You must provide a fully qualified path to the key or value that you want to write. In addition, strName must begin with one of these values: HKEY_CURRENT_USER, HKEY_LOCAL_ MACHINE, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_CURRENT_CONFIG, or HKEY_DYN_DATA. RegRead can only write specific data types including REG_SZ, REG_EXPAND_SZ, REG_DWORD, and REG_BINARY. Any other data types will return an error.


Run
(strCommand [, intWinType] [lWait])

Runs the command or application specified by strCommand. You can include command line arguments and switches with the command string. intWinType determines the type of window that the application starts in. You can force the script to wait for the application to complete by setting lWait to True; otherwise, the script begins the next line of execution immediately.


SetEnvironmentVariable
(strName, strValue [, strType])

Sets the environment variable named strName to the value specified by strValue. The optional strType argument defines the type of environment variable to create. Typical values for strType include System, User, Volatile, and Process. The default environment variable type is System.

4. Working with the WScript.WshNetwork Object

The WshNetwork object works with network objects such as drives and printers that the client machine can access. This is an exposed WSH object, which means you can access it directly using the WScript.WshNetwork object. The following list describes properties associated with this object.


ComputerName

Returns a string containing the client computer name.


UserDomain

Returns a string containing the user's domain name.


UserName

Returns a string containing the name that the user used to log on to the network.

As with any other WSH object, the WshNetwork object uses methods to work with network resources. The following list describes the methods associated with this object.


AddPrinterConnection
(strLocal, strRemote [, lUpdate] [, strUser] [, strPassword])

Creates a new printer connection for the local machine. strLocal contains the local name for the printer specified by strRemote. The strRemote value must contain a locatable resource and usually uses a UNC format such as \\Remote\Printer. Setting lUpdate to True adds the new connection to the user profile, which means Windows makes the connection available each time the user boots their machine. strUser and strPassword contain optional username and password values required to log onto the remote machine and create the connection.


EnumNetworkDrives()

Returns a WshCollection object containing the list of local and remote drives currently mapped from the client machine. A WshCollection object is essentially a 0-based array of strings.


EnumPrinterConnections()

Returns a WshCollection object containing the list of local and remote printers currently mapped from the client machine. A WshCollection object is essentially a 0-based array of strings.


MapNetworkDrive
(strLocal, strRemote [, lUpdate] [, strUser] [, strPassword])

Creates a new drive connection for the local machine. strLocal contains the local name for the drive specified by strRemote. The strRemote value must contain a locatable resource and usually uses a UNC format such as \\Remote\Drive_C. Setting lUpdate to True adds the new connection to the user profile, which means Windows makes the connection available each time the user boots their machine. strUser and strPassword contain optional username and password values required to log onto the remote machine and create the connection.


RemoveNetworkDrive
(strName [, lForce] [, lUpdate])

Deletes a previous network drive mapping. If strName contains a local name, Windows only cancels that connection. If strName contains a remote name, then Windows cancels all resources associated with that remote name. Set lForce to True if you want to disconnect from a resource whether that resource is in use or not. Setting lUpdate to True removes the connection from the user profile so that it doesn't appear the next time that the user logs onto the machine.


RemovePrinterConnection
(strName [, lForce] [, lUpdate])

Deletes a previous network printer connection. If strName contains a local name, Windows only cancels that connection. If strName contains a remote name, then Windows cancels all resources associated with that remote name. Set lForce to True if you want to disconnect from a resource whether that resource is in use or not. Setting lUpdate to True removes the connection from the user profile so that it doesn't appear the next time that the user logs onto the machine.

Other  
 
Top 10
Review : Sigma 24mm f/1.4 DG HSM Art
Review : Canon EF11-24mm f/4L USM
Review : Creative Sound Blaster Roar 2
Review : Philips Fidelio M2L
Review : Alienware 17 - Dell's Alienware laptops
Review Smartwatch : Wellograph
Review : Xiaomi Redmi 2
Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8
Visit movie_stars's profile on Pinterest.