Utility: PowerShell

Last Update: 5/5/2017

Team Services | TFS 2017 | TFS 2015 | Previous versions: XAML Build, Release

Run a PowerShell script

Demands

DotNetFramework

Arguments

Argument Description
Script filename Specify the path to the script to you want to run. The path must be a fully qualified path or a valid path relative to the default working directory. In Team Foundation Build, this directory is $(Build.SourcesDirectory).
Arguments Specify arguments to pass to the script. You can use ordinal or named parameters.
Advanced
Working folder Specify the working directory in which you want to run the script. If you leave it empty, the working directory is the folder where the script is located.
Control options

Examples

Hello World

Create test.ps1 at the root of your repo:

Write-Host "Hello World from $Env:AGENT_NAME."
Write-Host "My ID is $Env:AGENT_ID."
Write-Host "AGENT_WORKFOLDER contents:"
gci $Env:AGENT_WORKFOLDER
Write-Host "AGENT_BUILDDIRECTORY contents:"
gci $Env:AGENT_BUILDDIRECTORY
Write-Host "BUILD_SOURCESDIRECTORY contents:"
gci $Env:BUILD_SOURCESDIRECTORY
Write-Host "Over and out."

On the Build tab of a build definition, add this step:


Utility: PowerShell

Run test.ps1.

  • Script filename: test.ps1

Write a warning

icon Set warning message

  • Arguments

    "You've been warned by"
    
  • Script

    Write-Host "$("##vso[task.setvariable variable=WarningMessage]") $($args[0])"
    

icon Write warning using task.LogIssue

  • Script

    # Writes a warning to build summary and to log in yellow text
    Write-Host  "$("##vso[task.logissue type=warning;]") $($env:WarningMessage) $("the task.LogIssue Team Build logging command.")"
    

icon Write warning using PowerShell command

  • Script

    # Writes a warning to log preceded by "WARNING: "
    Write-Warning "$($env:WarningMessage) $("the Write-Warning PowerShell command.")"
    

Write an error

icon Set error message

  • Arguments

    "something went wrong."
    
  • Script

    Write-Host "$("##vso[task.setvariable variable=ErrorMessage]") $($args[0])"
    

icon Write error using task.LogIssue

  • Script

    # Writes an error to the build summary and to the log in red text
    Write-Host  "$("##vso[task.logissue type=error;]") $("the task.LogIssue Team Build logging command reported that") $($env:ErrorMessage)"
    
TIP

If you want this error to fail the build, then add this line:

exit 1

icon Write error using PowerShell command

  • Script

    # Writes an error to the build summary and the log with details about the error
    Write-Error "$("the Write-Error PowerShell command reported that") $($env:ErrorMessage)"
    
TIP

If you don't want this error to fail the build, then clear the Advanced: Fail on Standard Error check box.

ApplyVersionToAssemblies.ps1

Use a script to customize your build process

Q&A

Where can I learn about PowerShell scripts?

Scripting with Windows PowerShell

Microsoft Script Center (the Scripting Guys)

Windows PowerShell Tutorial

PowerShell.org

How do I set a variable so that it can be read by subsequent scripts and tasks?

Define and modify your build variables in a script

Define and modify your release variables in a script

Q: I'm having problems. How can I troubleshoot them?

A: Try this:

  1. On the variables tab, add system.debug and set it to true. Select to allow at queue time.

  2. In the explorer tab, view your completed build and click the build step to view its output.

The control options arguments described above can also be useful when you're trying to isolate a problem.

Q: How do variables work? What variables are available for me to use in the arguments?

A: $(Build.SourcesDirectory) and $(Agent.BuildDirectory) are just a few of the variables you can use. See Variables.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent.

I can't select a default agent queue and I can't queue my build or release. How do I fix this?

See queues.

I use Team Foundation Server on-premises and I don't see some of these features. Why not?

Some of these features are available only on Visual Studio Team Services and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.