Microsoft Windows 2. Scripting Guide. Microsoft. Running Programs Microsoft. Did you know you can install an application with VBScript using the 'Run' method.Here's a sample script illustrating how we do it. You can run scripts with the Windows-based script host in the following ways: By browsing in Windows Browse to the folder containing the script you want to. Launching multiple applications with a single command. As a system engineer or as a developer sometimes you may need to run multiple programs at once as a. VBScript ignores spaces, tabs, and newlines that appear within VBScript programs. One can use spaces, tabs. VBScript 5 Multiple Line Syntax. Guy's weekly Scripting Ezine 26. Example of VBScript SendKeys for run and sleep. Method WScript Shell. Hi all, I am trying to create a VBScript that will uninstall multiple (13) applications silently (preferably uninstalling one after another continuously as. For example, you can use scripts to create shared folders. But what if you want to set the offline folder options for that share? This procedure cannot be scripted using WSH or WMI. Instead, you must configure offline folder options using either the GUI or the Net. For another, there is no reason to write a script if a tool already exists that fills your needs. Suppose you want to see the list of files that are stored in a folder on the local computer. You can spend the time creating a script to return this information. Alternatively, you can simply type dir at the command prompt and be done with it. Using WSH scripts to help automate tasks does not require that you abandon the command- line tools or batch scripts that you currently use. If you have a batch script that performs well at a given task, there is no reason to create a WSH script that performs the same task. On the other hand, you might want to augment the capabilities of a batch script with capabilities available only in the WSH scripting environment. For example, you might have a disk cleanup utility that you want to run only if free disk space falls below a specified level. It is possible to create a batch file that can determine free disk space and then decide whether to run the cleanup utility; however, the procedure for creating such a batch file is not particularly straightforward. By contrast, this same task can be accomplished quite easily in WSH: You can use the File. System. Object or WMI to determine the free disk space and then either run or not run the disk cleanup utility. One of the advantages of using WSH is that you do not have to choose WSH or choose batch files and command- line tools. The two approaches are not at odds with one another; instead, they complement one another. For example, you can use the Wsh. Shell Run and Exec methods in scripts to incorporate existing batch files or command- line tools. Although running command- line tools and running batch scripts are important uses of the Run and Exec methods, these methods are not restricted to running a particular type of program. The Run and Exec methods enable your scripts to run any Windows program, including GUI applications. In addition, you can also run other scripts from within a script. Comparing Run and Exec. The fact that there are two ways to run programs from a script leads to an obvious question: which method should you use in your scripts? The answer to that question depends on the script and what it needs to accomplish. A script can use either the Run method or the Exec method to run a program in a manner similar to using the Run dialog box from the Start menu. Regardless of the method used, the program starts, and runs in a new process. However, when you use the Run method, your script will not have access to the standard input, output, and error streams generated by the program being run. A script cannot use the Run method to run a command- line tool and retrieve its output. For example, suppose you want to run Ping. This cannot be done using the Run command. Instead, you would need to ping the computer, save the results of the ping command to a text file, open the text file, read the results, and then parse those results to determine the success or failure of the command. The following script uses the Run method to call Ping. The script opens and reads the text file, checks to see whether the command succeeded (by determining whether any of the lines of output begin with the word Reply), and then closes and deletes the temporary file. Set obj. FSO = Wscript. Create. Object(. If you need access to command- line output, you should use the Exec method instead. The following script also parses the output generated by Ping. However, it does so by using the Exec method and by directly reading the output. There is no need to create, open, read, and delete a temporary file, and the script is only 9 lines long, compared with the 1. Run method. Set obj. Shell = WScript. Create. Object(. However, the Run method is still useful in a number of situations: You might want to run the application in a specified window type, such as a minimized window. Exec offers no control over window style; Run offers the options listed in Table 3. You might need to run a script on computers that do not have WSH 5. Exec is supported only on WSH 5. You might want to wait for the application being called to finish running before the script resumes. This can be done with either Run or Exec but requires less coding with Run. Running Programs. The Run method accepts three parameters. The first and only required parameter is the name of the program you want to run. If the program is in the same folder as the script, or if it is located within the computer path, you need enter only the name (for example, Calc. Otherwise, enter the full path to the program (C: \Admin\Monitoring\Disk. Space. exe). The second parameter is an integer that indicates the window style with which the program should begin (assuming the program has a window). The window style determines such things as whether a window will be the active window or be maximized. Table 3. 9 lists the integers that Run accepts as a second parameter as well as the corresponding the window styles. Table 3. 9 Integers Accepted by the Run Method for the Window Style. Integer. Window Style Description. Hides the window and activates another window. Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time. Activates the window and displays it as a minimized window. Activates the window and displays it as a maximized window. Displays a window in its most recent size and position. The active window remains active. Activates the window and displays it in its current size and position. Minimizes the specified window and activates the next top- level window in the Z order. The Z order is nothing more than the list detailing the order in which windows are to be activated. If you press ALT+TAB, you will see a graphical representation of the Z list. Displays the window as a minimized window. The active window remains active. Displays the window in its current state. The active window remains active. Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window. Sets the show- state based on the state of the program that started the application. For example, the script in Listing 3. Notepad. In line 1, the script sets the MAXIMIZE. In line 3, the script uses the Wsh. Shell Run method to start Notepad, passing it the MAXIMIZE. For example, Control Panel (Control. The Run method also accepts a Boolean value as a third parameter that determines whether the script pauses until the called program is finished running or instead continues with the next command in the script. If this value is set to False (the default), the Run method simply issues the command to run the program but does not check to ensure that the program actually ran. If the third parameter is set to True, the script will wait for the program to finish running, return the integer exit code provided by the program, and then continue with the next line of the script. If you set this value to False, you can run multiple programs at the same time; the script will start program A and then immediately start program B, even though program A is still running. This can enable your scripts to complete faster. However, it can also lead to possible problems: For example, what if program B cannot be run until program A has finished? If you are worried about possible . In that case, program B will not start until program A has concluded. For example, this script runs Calculator and then waits until Calculator has been closed before proceeding. If Calculator is never closed, line 3 of this script will never execute. Set obj. Shell = WScript. Create. Object(. For example, suppose you run the preceding script and then manually start a second instance of Calculator. If you close this second instance, the script will not respond. Instead, it will continue to wait until the initial instance of Calculator, the one started using Run, has been closed. Running Command- Line Tools. Although both Run and Exec are well suited for running GUI programs from scripts, you are unlikely to call many GUI programs from within a script. After all, the basic idea behind most system administration scripts is to carry out a task without the need for any human intervention. Because GUI applications typically require human intervention, about the best you can do within a script is open the application; you are very limited in what you can do with the application after it has been opened. This is not true for command- line tools, however. Most command- line tools are designed to run in automated fashion; after they have been started, there is no need for any human intervention. The tools start, perform their appointed task, and then terminate. Both the Run method and the Exec method can be used to run command- line tools, although in either case you should use a slightly different syntax from the one used to run GUI tools. When you run a command- line tool using either of these methods, you should always preface the tool name with one of the following: The %comspec% variable is an environment variable that specifies the command- line processor. By using %comspec%, you can create scripts that run on both Windows 9. Command. exe) and on Windows 2. Cmd. exe). The %comspec% variable is not required; however, it does provide a way for a the command window in which a tool runs to remain on the screen. This means that you might not have time to view the output generated by that tool.)Including %comspec% is also the only way to run command- line commands such as dir.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2016
Categories |