TeamCity
 
You are viewing the documentation for an earlier version of TeamCity.

PowerShell

Last modified: 20 April 2023

The PowerShell build runner is specifically designed to run PowerShell scripts.

PowerShell Settings



Interaction with TeamCity



Attention must be paid, when using PowerShell to interact with TeamCity through service messages. PowerShell tends to wrap strings written to the console with commands like Write-Output, Write-Error and similar (see TW-15080). To avoid this behavior, either use the Write-Host command, or adjust the buffer length manually:

Error Handling



Due to this issue in PowerShell itself which causes zero exit code to be always returned to a caller, TeamCity cannot always detect whether the script has executed correctly or not. We recommend several approaches that can help in detecting script execution failures:

  • Manually catching exceptions and explicitly returning exit code Since TeamCity 9.0 the PowerShell plugin does not use the cmd wrapper around powershell.exe. It makes returning the explicit exit code possible.

    try {
      # your code here
    } Catch {
      $ErrorMessage = $_.Exception.Message
      Write-Output $ErrorMessage
      exit(1)
    }
  • Setting to and adding build failure condition In case syntax errors and exceptions are present, PowerShell writes them to stderr. To make TeamCity fail the build, set Error Output option to Error and add a build failure condition that will fail the build on any error output.

  • Failing build on certain message in build log Add a build failure condition that will fail the build on a certain message (say "POWERSHELL ERROR") in the build log.

    $ErrorMessage = "POWERSHELL ERROR"
    try {
      # your code here
    } Catch {
      Write-Output $ErrorMessage
      exit(1)
    }

Development Links



The PowerShell support is implemented as an open-source plugin. For development links refer to the plugin's page.