Using the CMD Definition Type
The Microsoft Windows Command CMD language is the Windows version of BAT (MS-DOS batch) files. It supports everything that BAT supported, and more. The CMD language is quite powerful nowadays; of particular interest when writing scripts are the SET, IF and FOR commands. Type HELP
followed by a command in a command prompt for more information.
note
You must assign at least one process server to run CMD process definitions in order to use the definition type.
For commands that interact with COM or WMI commands it is better to use the VBScript or PowerShell definition types.
Variables and Parameters
- Parameters to the process definition are manipulated in the CMD source simply as if they are variables, using the standard %PARAMETER% syntax.
- The CMD language does not have actual data types; all parameter values are stored as strings. Numbers are translated automatically. Dates are sent and retrieved using the Script Date Format.
- Out parameters are supported by setting the parameter using the
set PARAMETER=VALUE
syntax.
Exiting a Script
If processing of the CMD file ends by means of a hard EXIT [0]
command (without /b
switch), a warning will be printed in the output file and output variables will not be set. The correct ways to end the process without error and set the output variables are:
- Running to the end of the process definition source.
- Use
GOTO :EOF
(see HELP GOTO). - Use
EXIT /B [0]
Returning an Error
If processing of the CMD file ends by means of a hard EXIT [<n>]
command, where <n>
is an integer greater than 0
, (without /b
switch), the process will reach status Error and output variables will not be set. The correct way to return an error and still set the output variables is EXIT /B <n>
, where <n>
is an integer greater than 0
. The return code will be used as the Return Code of the process.
32/64-bit
By default, the 32-bit versions of the interpreters are used on Windows platforms, this can be changed using the following:
JCS_INTERPRETER_CMD
- parameter used to override default, can be set toC:/Windows/Sysnative/cmd.exe
for 64-bit; default when non-existent or empty and process server parameterLocalInterpreter_CMD
not set:C:/Windows/System32/cmd.exe
InterpreterWhitelist_CMD
- process server parameter that takes a regex expression matching all interpreters you want supported. Use.*
to allow any orC:/Windows/.*/cmd.exe}
to allow anycmd.exe
in a subfolder ofC:/Windows
. You can also explicitly set two separated by a comma without space (regex syntax):C:/Windows/Sysnative/cmd.exe,C:/Windows/System32/cmd.exe
LocalInterpreter_CMD
- process server parameter used to set default to 64-bit, can be set toC:/Windows/Sysnative/cmd.exe
for 64-bit; affects all processes for the process server that do not haveJCS_INTERPRETER_CMD
set, default or when not set:C:/Windows/System32/cmd.exe
- LocalInterpreterBits - Set this key to
64
and all Windows platform agents will use 64-Bit interpreters.
note
In Windows 64-bit, you have to explicitly call the 32-bit binaries to access C:/Windows/Sysnative
; the path does not exist for 64-bit applications. Also, in 32-bit applications, C:/Windows/System32
is linked to C:/Windows/SYSWOW64
which contains the 32-bit windows binaries. Remember that on 64-bit Windows platforms the System32
directory contains 64-bit binaries and SYSWOW64
is the directory containing 32-bit binaries. In other words, the number of bits is the opposite of that in the directory name.
note
Always use forward slashes /
as a Windows path separator in Redwood Server. Windows accepts them almost everywhere except in some legacy MS DOS-era executables that use the forward slash
Background and Foreground Processes
By default, commands are executed in the background; you will not see any dialog on the server console. If you want to display a process on-screen, you use the {session}
or {console}
keywords in the Run As User field followed by the credential or username and password.
{session}
- the process runs in a RDP, Windows Terminal Server, or console session running as the account specified in the credential. If no such windows user session exists, the process will fail with status Error.{console}
- Console session, only; console sessions are displayed on the physical monitor screen attached to the server. If the user specified by the credential is not the user that is logged on to the console, the process will fail with status Error.
These two options are useful but they require dedicating a system to a particular account (via {console}
) or that some other means is used to ensure that a RDP
session exists. It is usually better to let the platform agent manage RDP sessions via the built-in Windows Session support support.
note
Sessions are always for a user account; it is not possible to run session/console processes under NT Authority\LocalSystem
.
Run As User Examples
Specifying a Run As User field with {session}
and virtual user
{session}{virtual}:ops
Specifying a Run As User field with {console}
and credential
{console}jdoe@example.corp
Examples
The following shows how to pass numeric (N1), string (S1) and date (D1) parameters to and from CMD scripts:
set /A N1=%N1% + %N1%
rem Concatenate string
set S1=%S1% %S1%
set DTEMP=1999/12/31 23:59:59,000 GMT"
echo You said %D1%, I propose %DTEMP%
rem Set DateTime to new string value
set D1=%DTEMP%
The following example shows you how to return a non-zero exit code resulting in the processes going into error:
set N=1
echo Exiting with value %N%.
if %N% GEQ 1 exit/b %N%
echo Not reached
The following shows how to obtain the number of free bytes on drive C on the process server that the process runs on:
rem
rem Find out how many bytes are free on drive C:, and set the DIRSPACE variable
rem accordingly. This depends on "dir" writing the drive space on its last line
rem of output. We continuously overwrite the same variable until we automatically
rem leave the last value in place.
rem
for /F "usebackq tokens=3" %%x in (`dir /-C c:\`) do call :getline %%x
goto :EOF
:getline
set DIRSPACE=%1
goto :EOF
The following shows how to detect if the 32-bit executable is used (This requires a hotfix for Windows 2003 kb942589.
if exist c:\windows\sysnative\cmd.exe (
echo "32-bit"
) else (
echo "64-bit"
)