2

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dech
  • 1,284
  • 3
  • 13
  • 28
  • Side note: I just read that $VerbosePreference is by default set to SilentlyContinue, to show Verbose logging, you need to set this variable to at least Continue. Calling -Verbose on the cmdlet was insufficient, however setting this in the parent PS script may help: $Global:VerbosePreference="Continue" *Test in progress* – Dech Jul 03 '18 at 13:24

1 Answers1

1

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.

Dech
  • 1,284
  • 3
  • 13
  • 28