These two little commands have very short write-ups in the manual but generate a lot of questions.

RUN

Action: Runs a command.
Syntax: RUN "Command"

Remarks: Command can be any 16-bit or 32-bit application.  To run command interpreter commands, specify the correct command interpreter as part of the command.

RUN does not wait for the program to complete.  Script execution continues immediately.
----------

SHELL

Action: Loads and runs a program.

Syntax: SHELL "Command"

Remarks: Command can be any 16-bit or 32-bit application.  To run command interpreter commands, specify the correct command interpreter as part of the command.

Script execution is stopped until the program exits.

SHELL sets the value of @ERROR to the exit code of the program that is run.
----------

If you find you need to include quotes on the command line, there are several ways to go.

1. Use single quotes outside and double quotes inside.
SHELL '%comspec% /c "C:\Program Files\Internet Explorer\iexplore.exe" about:blank'

2. Use Chr(34) in place of double quotes.

3. Assemble the shell command as a string.
$ShellCMD = Chr(34) + $WorkDir + "\symcdefsx86.exe" + Chr(34) + " /q /extract /vdb " + $TempDir
Shell $ShellCMD

----------

%comspec%
When using the SHELL command, don't launch the command interpreter (%comspec% /c) unless you need it.  The environment variable %comspec% points to the command interpreter needed to run certain DOS programs and internal DOS commands which include batch commands.  On NT/2K/XP the command interpreter is called CMD.EXE, evidenced by typing SET at a DOS prompt and looking at the comspec variable (likely "C:\WINNT\system32\cmd.exe").  On Wintendo it is COMMAND.COM, but by using the variable, %comspec% is common to both.  You can do a "CMD /?" from a DOS box for more info.
----------

If you want to use PIPE "|" or redirect ">" or ">>" then you must include the command interpreter.  The difference between a single ">" and double">>" redirect is that a single redirect will create/overwrite the file and a double redirect will create/append to it.  When redirecting, 1>NUL redirects STDOUT and 2>NUL redirects STDERR.  If you want to redirect both to the same file, use 1> output.txt 2>&1 (see Technet artice Q110930 for more on this).


The difference between SHELL and RUN is that RUN returns to the script immediately and allows it to continue.  SHELL normally will hold the script until the shelled program terminates.  In some cases, the shelled program calls another and terminates, returning control to the script prematurely.  Using Start /W could mitigate that.  You can do a "Start /?" from a DOS box for more info.
----------
Q. Why do I get the following when I use the command interpreter?
 Quote:
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.

A. The problem here is that CMD.EXE requires your current drive to be a "real" drive, i.e. associated with a Windows drive letter. You are running the script from a mapped drive during login or via group policy so the current drive is effectively the share on which the script is stored - this is the "UNC path" that CMD.EXE is complaining about.
To fix the problem just make sure that the current directory is on a real drive, the following should work in most situations:
 Code:
GO Left(%WINDIR%,2)
CD %WINDIR%
SHELL "<your shell command here>"

Note: Portions of this were taken from the HTML help file created and compiled by ScriptLogic Corporation.

[ 08. December 2002, 01:52: Message edited by: LLigetfa ]


Edited by Richard H. (10/05/18 03:05 PM)
Edit Reason: Added commentary on UNC paths