Using PowerShell in post/pre build action in Visual Studio. 15 Jul 2015 PowerShell, Visual Studio Let’s say it. Batch files are plain simple for any real developer to use. PowerShell rocks. Sadly in Visual Studio in fairly useful feature of post/pre build actions you can by default use only batch files. Did the proj file build ok? Did it tell you 'Build succeeded' at the end? If your proj file can be build successfully, perhaps it execute the bat file but you didn't see the result. If the bat file was executed, what we can get or what we can see. If the proj file can't be run fine, I'm afraid that you need to modify it to make it run ok.
-->Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015
Use this task in a build or release pipeline to run a program from the command prompt.
Note
In Microsoft Team Foundation Server (TFS) 2018 and previous versions,run and release pipelines are called definitions,runs are called builds,service connections are called service endpoints,stages are called environments,and jobs are called phases.
Demands
None
YAML snippet
The CmdLine task also has a shortcut syntax in YAML:
Running batch and .CMD files
Azure Pipelines puts your inline script contents into a temporary batch file (.cmd) in order to run it.When you want to run a batch file from another batch file in Windows CMD, you must use the
call
command, otherwise the first batch file is terminated.This will result in Azure Pipelines running your intended script up until the first batch file, then running the batch file, then ending the step.Additional lines in the first script wouldn't be run.You should always prepend call
before executing a batch file in an Azure Pipelines script step.Important
You may not realize you're running a batch file.For example,
npm
on Windows, along with any tools that you install using npm install -g
, are actually batch files.Always use call npm <command>
to run NPM commands in a Command Line task on Windows.Arguments
Argument | Description |
---|---|
Script | Contents of the script you want to run |
Optional | |
Working directory | Specify the working directory in which you want to run the command. If you leave it empty, the working directory is $(Build.SourcesDirectory). |
Fail on standard error | If this is true , this task will fail if any errors are written to stderr . |
Environment variables | A list of additional items to map into the process's environment. For example, secret variables are not automatically mapped. If you have a secret variable called Foo , you can map it in like this: |
Control options |
Example
On the Build tab of a build pipeline, add these tasks:
Utility: Command Line | Get the date.
|
Utility: Command Line | Display the operating system version.
|
Utility: Command Line | Display the environment variables.
|
Utility: Command Line | Display all files in all the folders created by the build pipeline.
|
Open source
This task is open source on GitHub. Feedback and contributions are welcome.
Q & A
Where can I learn Windows commands?
Do I need an agent?
You need at least one agent to run your build or release.
I'm having problems. How can I troubleshoot them?
See Troubleshoot Build and Release.
I can't select a default agent pool and I can't queue my build or release. How do I fix this?
See Agent pools.
I use TFS on-premises and I don't see some of these features. Why not?
Some of these features are available only onAzure Pipelinesand not yet available on-premises. Some features are available on-premises if you haveupgraded to the latest version of TFS.