Exec Task
Runs the specified program or command by using the specified arguments.
The following table describes the parameters for the Exec task.
|
Parameter |
Description |
|---|---|
|
Command |
Required String parameter. The command(s) to run. These can be system commands, such as attrib, or an executable, such as program.exe, runprogram.bat, or setup.msi. This parameter can contain multiple lines of commands. Alternatively, you can put multiple commands in a batch file and run it by using this parameter. |
|
CustomErrorRegularExpression |
Optional String parameter. Specifies a regular expression that is used to spot error lines in the tool output. This is useful for tools that produce unusually formatted output. |
|
CustomWarningRegularExpression |
Optional String parameter. Specifies a regular expression that is used to spot warning lines in the tool output. This is useful for tools that produce unusually formatted output. |
|
ExitCode |
Optional Int32 output read-only parameter. Specifies the exit code that is provided by the executed command. |
|
IgnoreExitCode |
Optional Boolean parameter. If true, the task ignores the exit code that is provided by the executed command. Otherwise, the task returns false if the executed command returns a non-zero exit code. |
|
IgnoreStandardErrorWarningFormat |
Optional Boolean parameter. If false, selects lines in the output that match the standard error/warning format, and logs them as errors/warnings. If true, disable this behavior. |
|
Outputs |
Optional ITaskItem[] output parameter. Contains the output items from the task. The Exec task does not set these itself. Instead, you can provide them as if it did set them, so that they can be used later in the project. |
|
StdErrEncoding |
Optional String output parameter. Specifies the encoding of the captured task standard error stream. The default is the current console output encoding. |
|
StdOutEncoding |
Optional String output parameter. Specifies the encoding of the captured task standard output stream. The default is the current console output encoding. |
|
WorkingDirectory |
Optional String parameter. Specifies the directory in which the command will run. |
This task is useful when a specific MSBuild task for the job that you want to perform is not available. However, the Exec task, unlike a more specific task, cannot gather output from the tool or command that it runs.
The Exec task calls cmd.exe instead of directly invoking a process.
In addition to the parameters listed in this document, this task inherits parameters from the ToolTaskExtension class, which itself inherits from the ToolTask class. For a list of these additional parameters and their descriptions, see ToolTaskExtension Base Class.
The following example uses the Exec task to run a command.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Binaries Include="*.dll;*.exe"/>
</ItemGroup>
<Target Name="SetACL">
<!-- set security on binaries-->
<Exec Command="echo y| cacls %(Binaries.Identity) /G everyone:R"/>
</Target>
</Project>
It creates a temporary batch file, to be invoked via
%windir%\system32\cmd.exe" /Q /C %temp%\cce126ad5cfd4efa889dad3ae5d4571f.exec.cmd
the batch files contains
setlocal
set errorlevel=dummy
set errorlevel=
<your command here>
exit %errorlevel%
If the command contains | or % characters (for example, it is an inline powershell script, like powershell -command "--literal-script-block--" ) the cmd.exe will eat/interpret them
- 1/10/2012
- IvanBrug
- 12/16/2011
- Jeff Youngstrom
- 4/5/2011
- Sebazzz
The example shows involving an item group in the command but doesn't explain what it does.
Here's how it works:
Given this item group:
<ItemGroup>
<MyFiles Include="folder1\file1.txt" />
<MyFiles Include="folder2\file2.txt" />
</ItemGroup>
This exec command
<Exec Command="runme.bat @(MyFiles)" />
will run the following command (inside of cmd.exe):
runme.bat folder1\file1.txt;folder2\file2.txt
This exec command
<Exec Command="runme.bat %(MyFiles.Identity)" />
will run the following commands in successfion (inside of cmd.exe):
runme.bat folder1\file1.txt
runme.bat folder2\file2.txt
- 2/18/2011
- CodeMuncher
error MSB6003: The specified task executable "cmd.exe" could not be run. The directory name is not valid
and the build will fail.
- 9/22/2010
- Richard Winks
- 9/22/2010
- Richard Winks
