Write-Host function can't work in remote sessions
Hi,
So I tried to use your Write-Host function in remote sessions and it didn't output any text. That is due to the usage of System.Console.Write(Line) which are not usable in remote sessions. See the issue I opened on the Powershell repo about this:
https://github.com/PowerShell/PowerShell/issues/20054
So, I would suggest that you make the same change I did in order to make this work:
@@ -83,8 +83,6 @@ function Write-Host {
Process {
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_special_characters#escape-e
# https://docs.microsoft.com/en-us/powershell/scripting/windows-powershell/wmf/whats-new/console-improvements#vt100-support
- if ($Host.UI.SupportsVirtualTerminal) {
- $Method = if ($NoNewline) { 'Write' } else { 'WriteLine' }
$Output = if ($Separator) { $Object -join $Separator } else { "$Object" }
# Splitting by regex ensures that this will work on files from Windows/Linux/macOS
@@ -97,11 +95,7 @@ function Write-Host {
$item = $AnsiTemplate -f $AnsiColor[$ForegroundColor.value__], $item, 39
}
- [System.Console]::$Method($item)
- }
- }
- else {
- Microsoft.PowerShell.Utility\Write-Host @PSBoundParameters
+ if ($NoNewLine) { Microsoft.PowerShell.Utility\Write-Host -NoNewLine $item } else { Microsoft.PowerShell.Utility\Write-Host $item }
}
}
}
So, instead of using System.Console.Write or System.Console.WriteLine, simply use the default Write-Host function. Therefore, it makes calls inside remote sessions output colored text in Azure DevOps pipeline just as calls outside of remote session.
Thanks! Will give this a look. The reason for [console]:Write* is performance - the Write-Host can be abominably slow when you have a lot of text to dump. I don't have any concrete numbers right now, but I'll see if I can repro this.
It would be great if we could detect remote session somehow ($host.Name -ne 'ConsoleHost' ?) and use Write-Host only in this case.
- https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations#avoid-write-host