Here is a pretty simple use case: there's a trivial PowerShell script that calls Write-Host cmdlet. It works just fine with a plain "Run PowerShell" step, but fails in "PowerShell on Target Machines".
I understand in the latter case it runs in a remote session, and this fact adds some constraints on its own. However, it is possible to Write-Host in a remote PowerShell session if I trigger it not from build vNext, but in some other way.
What is the decision behind this? Why is it not allowed to Write-Host in that step?
Any thoughts on this one, guys?
Write-Host is not a preferred choice for automation. We will evaluate your feedback.
We haven't added this support for remote PowerShell. We do understand it is possible to support it. However, we would recommend using Write-Verbose, OutPut in lieu of Write-Host.
I would like to quote, Windows PowerShell inventor, Jeffrey Snover, who says that Write-Host is harmful http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/
Thanks for your reply. I see the point with Write-Host considered harmful, and it's quite possible to fix the scripts and tools to avoid using it.
However, it might be a stopper with third-party. Consider the following scenario: I'd like to run a PowerShell script on a remote VM, and the script install a Chocolatey package, which uses Write-Host in its install.ps1. In this case it's a showstopper.
Besides, the plain "Run PowerShell" step still allows using Write-Host. I don't think that it is more harmful in a remote session, isn't it?
Appreciate your inputs, your argument is valid. As mentioned, we will look into it.
Great! Thanks a lot!
Added to product backlog
@yansklyarenko as a workaround you might be able to wrap the command and do something like this:
$verbosepreference = 'continue' ; write-host 'foo' 6>&1 | % { if ($_ -is [System.Management.Automation.InformationRecord]) { write-verbose "$_" } else { $_ } }
Unfortunately the above will only work in ps 5.
@KrishnaAdityaB some feedback to consider: I've read the guidance over and over and had people express to me numerous times: "don't write host". I think the fact that there is so much discussion over the whole issue is missing the point: people _want_ to write-host. In the end I think that's the overriding concern. And people seem to find write-host to be natural and fit their solutions.
Furthermore, since in powershell 5 write-host is a wrapper for write-information (pipeline 6), i think this is revealing of the fact that the mark was originally missed and finally getting some resolution. Anyway that's my 2 cents.
I'm still unclear after reading this issue. What should I use instead of Write-Host? We're on v4. Write-Output does not seem to show up in the build logs.
If you want to log you can user write-verbose with -verbose option . e.g
write-verbose "Sample log" -verbose
Most helpful comment
If you want to log you can user write-verbose with -verbose option . e.g
write-verbose "Sample log" -verbose