Delen via


Exec task

Runs the specified program or command by using the specified arguments.

Parameters

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.
ConsoleOutput Optional ITaskItem[] output parameter.

Each item output is a line from the standard output or standard error stream emitted by the tool. This is only captured if ConsoleToMsBuild is set to true.
ConsoleToMsBuild Optional Boolean parameter.

If true, the task will capture the standard error and standard output of the tool and make them available in the ConsoleOutput output parameter.

Default: false.
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.

Default: null (no custom processing).
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.

Default: null (no custom processing).
EchoOff Optional Boolean parameter.

If true, the task will not emit the expanded form of Command to the MSBuild log.

Default: false.
ExitCode Optional Int32 output read-only parameter.

Specifies the exit code that is provided by the executed command, except that if the task logged any errors, but the process had an exit code of 0 (success), ExitCode is set to -1.
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.

Default: false.
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.

Default: false.
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.
UseUtf8Encoding Optional String parameter.

Specifies whether to use UTF8 code page when processing the command line for executed commands. Valid values are Always, Never, or Detect. The default is Detect, which means use the UTF8 code page only when non-ANSI characters are present.
WorkingDirectory Optional String parameter.

Specifies the directory in which the command will run.

Default: The project's current working directory.

ToolTaskExtension parameters

This task inherits from the ToolTaskExtension class, which inherits from the ToolTask class, which itself inherits from the Task class. This inheritance chain adds several parameters to the tasks that derive from them.

The following table describes the parameters of the base classes:

Parameter Description
EchoOff Optional bool parameter.

When set to true, this task passes /Q to the cmd.exe command line such that the command line does not get copied to stdout.
EnvironmentVariables Optional String array parameter.

Array of environment variable definitions, separated by semicolons. Each definition should specify an environment variable name and value separated by an equal sign. These variables are passed to the spawned executable in addition to, or selectively overriding, the regular environment block. For example, Variable1=Value1;Variable2=Value2.
ExitCode Optional Int32 output read-only parameter.

Specifies the exit code that is provided by the executed command. If the task logged any errors, but the process had an exit code of 0 (success), this is set to -1.
LogStandardErrorAsError Optional bool parameter.

If true, all messages received on the standard error stream are logged as errors.
StandardErrorImportance Optional String parameter.

Importance with which to log text from the standard out stream.
StandardOutputImportance Optional String parameter.

Importance with which to log text from the standard out stream.
Timeout Optional Int32 parameter.

Specifies the amount of time, in milliseconds, after which the task executable is terminated. The default value is Int.MaxValue, indicating that there is no time out period. Time-out is in milliseconds.
ToolExe Optional string parameter.

Projects may implement this to override a ToolName. Tasks may override this to preserve the ToolName.
ToolPath Optional string parameter.

Specifies the location from where the task loads the underlying executable file. If this parameter is not specified, the task uses the SDK installation path that corresponds to the version of the framework that is running MSBuild.
UseCommandProcessor Optional bool parameter.

When set to true, this task creates a batch file for the command line and executes it by using the command-processor instead of executing the command directly.
YieldDuringToolExecution Optional bool parameter.

When set to true, this task yields the node when its task is executing.

Remarks

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 do additional processing or conditional operations based on the result of the tool or command that it runs.

The Exec task calls cmd.exe instead of directly invoking a process.

The parameters IgnoreExitCode and IgnoreStandardErrorWarningFormat affect the conditions under which the task returns false, indicating an error. With the default settings (false for both), the Exec task indicates a failure (returns false) either if the executable has a non-zero exit code, or if a diagnostic message is found in the executable's standard error stream. If you only want Exec to indicate failure if the executable returns a non-zero exit code, then set IgnoreStandardErrorWarningFormat to true.

Example

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>

See also