ps/Modules/Cole.PowerShell.Developer/Public/Write-TeamCity.ps1
2023-05-30 22:51:22 -07:00

64 lines
2.1 KiB
PowerShell

function Write-TeamCity {
<#
.SYNOPSIS
Used to write messages for Team-City
.DESCRIPTION
Will sanitize (escape) the inputs for TeamCity messages if running on TeamCity
.PARAMETER Message
The special message to write to the TeamCity logs
.PARAMETER Status
Attach a status to a message. Warning and Error have special significance.
Error is set automatically when you supply an ErrorDetails message.
Default is Normal.
.PARAMETER ErrorDetails
Can be a StackTrace or more specific details about where/how the error occurred.
.EXAMPLE
Write-TeamCity -Message "This is a warning message" -Status Warning
.EXAMPLE
Write-TeamCity -Message "oh no! [this is a bug]" -ErrorDetails "this path: [$path] failed"
#>
[CmdletBinding(DefaultParameterSetName = 'Message')]
[OutputType([void])]
param(
[Parameter(Mandatory = $true, ParameterSetName = "Message")]
[Parameter(Mandatory = $true, ParameterSetName = "Error")]
[string]$Message,
[Parameter(Mandatory = $false, ParameterSetName = "Message")]
[ValidateSet('Normal','Warning')]
[string]$Status = 'Normal',
[Parameter(Mandatory = $true, ParameterSetName = "Error")]
[string]$ErrorDetails
)
if (Test-IsTeamCityProcess) {
$sanitizedErrorDetails = ""
if ($PSCmdlet.ParameterSetName -eq 'Error') {
$Status = 'Error'
$sanitizedErrorDetails = ConvertTo-SafeTeamCityMessage -InputText $ErrorDetails
$sanitizedErrorDetails = "errorDetails='$sanitizedErrorDetails'"
}
$sanitizedMessage = ConvertTo-SafeTeamCityMessage -InputText $Message
Write-Host "##teamcity[message text='$sanitizedMessage' $sanitizedErrorDetails status='$($Status.ToUpper())']"
} else {
if ($PSCmdlet.ParameterSetName -eq 'Error') {
Write-Error -Message $Message -ErrorAction Continue
Write-Error -Message $ErrorDetails
} else {
if ($Status -eq 'Warning') {
Write-Warning -Message $Message
} else {
Write-Host $Message
}
}
}
}