Search code examples
c#powershellteamcitypowershell-cmdlet

Writing a C# PowerShell cmdlet - how to output debug logging to TeamCity caller


I am writing a PowerShell cmdlet in C#, and I need to add some diagnostic logging to the script so that I can evaluate the behavior through TeamCity. Having experimented with WriteVerbose with the -Verbose flag enabled for the function, nothing was logged to TeamCity.

What is the equivalent of Write-Host for a cmdlet?

What is the difference between the following cmdlets:

  • WriteCommandDetail
  • WriteProgress
  • WriteVerbose
  • WriteDebug
  • WriteWarning
  • WriteError
  • WriteObject

Solution

  • In case anyone else encounters this issue, this is what I learnt: PowerShell supports many different output streams.

    The one that TeamCity is after however is none of the above, [C#] Console.WriteLine is sufficient to log to the TeamCity trace log.

    To answer my previous question:

    WriteError is used for returning exceptions properly (this works well, albeit a little convoluted, just using C# throw will crash PowerShell when debugging a cmdlet).

    WriteObject is used for returning custom or existing C# objects from the cmdlet to the variable assignment (e.g. [PowerShell] $x = Do-Something, where [C#] WriteObject(Output) would be returned to [PowerShell] $x).

    WriteInformation (new in PowerShell 5) is the new way to write to the output stream, Write-Output now redirects to the Information stream.