0 out of 2 rated this helpful Rate this topic

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>
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
How the Exec task spawns cmd.exe

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
Exec seems to use "cmd /q"
From experiments, it appears that Exec calls cmd on its contents with the /q option which turns off echo. (And as far as I can tell, can't be overridden.)
Missing Property
EnvironmentVariables is missing.
Include paths with spaces
It applies not only to exec, but to all MSBuild actions in general: If you want to executables or files with whitespace in the file path, you must use quotes. In attributes, you will need to replace/escape the quotes by using "&quot". For example: $0<Copy DestinationFolder="$(MSBuildProjectDirectory)\bin\Deploy" ContinueOnError="false" OverwriteReadOnlyFiles="true" SkipUnchangedFiles="false" SourceFiles="@(DeploymentFiles)" /> $0 <Copy DestinationFolder="$(MSBuildProjectDirectory)\bin\Deploy\Audio\%(RecursiveDir)" ContinueOnError="false" OverwriteReadOnlyFiles="true" SkipUnchangedFiles="false" SourceFiles="@(MediaDeploymentFiles)" /> $0 <Exec Condition="Exists('C:\Program Files\7-Zip\7z.exe')" ContinueOnError="false" WorkingDirectory="$(MSBuildProjectDirectory)\bin" Timeout="300000" Command="&quot;C:\Program Files\7-Zip\7z.exe&quot; a App.7z .\Deploy\*" /> $0 <Exec Condition="Exists('C:\Program Files\7-Zip\7zSD.sfx')" ContinueOnError="false" WorkingDirectory="$(MSBuildProjectDirectory)\bin" Timeout="300000" Command="copy /b &quot;C:\Program Files\7-Zip\7zSD.sfx&quot; + &quot;$(MSBuildProjectDirectory)\DeployInstallerConfig.txt&quot; + &quot;$(MSBuildProjectDirectory)\bin\App.7z&quot; App.exe" /> $0 $0
More information about item groups

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

WorkingDirectory caution
When entering a value for the WorkingDirectory parameter, do not use a quoted string or include a property that is defined as a quoted string otherwise you will get an error
 error MSB6003: The specified task executable "cmd.exe" could not be run. The directory name is not valid
and the build will fail.