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.

The plugin responsible for PowerShell integration has been opensourced on GitHub: https://github.com/JetBrains/teamcity-powershell

Cross-Platform PowerShell



  • Cross-platform PowerShell (PowerShell Core) is supported on Windows, MacOS and Linux: download a PowerShell package for your platform and install it on the TeamCity agent.

  • Side-by-side installations of PowerShell Desktop and PowerShell Core is supported under Windows.

PowerShell Settings



Docker Settings



To enable support for Docker in PowerShell steps, run the TeamCity server with the -Dteamcity.docker.runners=jetbrains_powershell internal property.

In this section, you can specify a Docker image which will be used to run the build step.

Current limitations



  • Execution under Docker requires the PowerShell executable to be added to PATH

  • When using Docker to run the build step, only Docker-related build agent requirements are applied to the build

  • Selection of Edition in PowerShell build step affects the executable being used ( powershell.exe for Desktop , pwsh for Core ).

  • <Auto> defaults to pwsh (Core)

  • To specify a custom PowerShell executable, theteamcity.powershell.virtual.executable configuration parameter must be set to the full path of this executable inside the provided image

  • Current limitations of the Docker wrapper do not allow Linux containers running under Windows systems

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 that 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 codeThe 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 a 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 logAdd 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)
    }

Handling Output



To properly handle non-ASCII output from PowerShell, the correct encoding must be set both on the PowerShell side and on the TeamCity side.

  • To set the output encoding for PowerShell to UTF-8, add the following line to the beginning of your PowerShell script:

    [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  • To set the encoding on the TeamCity agent side, either set the java launch option -Dfile.encoding=UTF-8, or set the build configuration parameter teamcity.runner.commandline.stdstreams.encoding value toUTF-8

Temporary Files



The TeamCity PowerShell plugin uses temporary files as an entry point; these files are created in the build temporary folder and removed after the PowerShell build step is finished. To keep the files, set the powershell.keep.generated or teamcity.dont.delete.temp.files configuration parameter to true.

Development Links



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