Search
Desktop

Windows Server 2008 Server Core : Working with Scripts - Creating a Basic Script

Published 6 min readBy Programming4us Editorial Team
Scripts can make the command line significantly easier to automate and can improve the reliability of command line tasks by helping you perform tasks in the same sequence every time. This section shows how to create basic scripts in both VBScript and JavaScript so you can see the differences between the two languages. The following code shows a basic example in VBScript.
' Test1.VBS shows how to use functions and subprocedures
' within a WSH script.

WScript.Echo("The value returned was: " + CStr(MyFunction(1)))

function MyFunction(nSomeValue)
    WScript.Echo("Function received value of: " + CStr(nSomeValue))
    Call MySubprocedure(nSomeValue + 1)
    MyFunction = nSomeValue + 1
end function
sub MySubprocedure(nSomeValue)
    WScript.Echo("Subprocedure received value of: " + CStr(nSomeValue))
end sub

As you can see, the sample code uses the WScript object to send information to the screen. The WScript object is always available at the command line, even though you have probably never used it as part of a browser application. As shown in the example, it's important to know how to use both functions and subs, the two building blocks of VBScript. The following code shows a similar example for JavaScript.

// Test1.JS shows how to use functions within a WSH script.

WScript.Echo("The value returned was: " + MyFunction(1));

function MyFunction(nSomeValue)
{
   WScript.Echo("The value received was: " + nSomeValue);
   return nSomeValue + 1;
}

JavaScript only provides functions, so that's all this example demonstrates. It's also important to notice that VBScript requires you to convert numeric values to a string, while JavaScript performs the conversion automatically. The following sections show how to perform certain command line-oriented tasks using scripting.

1. Scripting the Command Line and System Environment

Many of your scripts require access to the command line. The command line is where you type switches to modify the behavior of the script. The system environment contains user, application, and operating system values, such as the user's name or the version of the operating system. The JavaScript code in Listing 1 retrieves information from the command line. It also retrieves information about the application environment. You can obtain this example on the Sybex Web site at http://www.sybex.com/WileyCDA/.

Example 1. Working with the Command Line and System Environment

When you run this script, you'll see a dialog box containing all of the information about the script engine. When you click OK, the program asks if you want to display the command line arguments. If you say yes, then you'll see anything you typed at the command line. Otherwise, the script displays a Goodbye message.

You should notice a few things about this example. First, I created an object in this code. You need access to the WshShell object for many of the tasks you'll perform with scripts. The code also shows how to use the Popup() method to obtain information from the user. Finally, the code uses the Arguments object to access the command line information. Notice the object hierarchy used in this example.

2. Scripting the Registry

Knowing how to access the registry from your script is important because you also need to access these values in order to discover how a particular utility will react or how the user had configured the system. You can also use the registry to store and retrieve values for your script. The example in Listing 2 shows how to use VBScript to access information in the registry. You don't want to change information unless you have to, but seeing what's available in the registry is a good way to build your knowledge of both scripting and the registry. Note that this example uses the command line argument to determine which file extension to look for in the registry. The example uses the TXT file extension when you don't supply one. You can obtain this example on the Sybex Web site at http://www.sybex.com/WileyCDA/.

Example 2. Working with the Registry

When you run this script, it reads the command line. If you haven't supplied a value, the script assigns a default extension of TXT. The script uses the extension to locate information in the registry such as the file open and print commands. Finally, the script uses the Popup() method to display the output.

You should notice several differences between this example and the JavaScript example in Listing 6.1. First, the method for creating an object requires the use of a set—you can't simply assign the object to a variable. You'll also notice that VBScript has access to all of the standard Visual Basic constants such as vbTab and vbCrLf. Finally, VBScript handles many of the method calls as subs, not as functions. You need to exercise care when working in a mixed environment.