Skip to content

Write-Host function can't work in remote sessions #5

@sim590

Description

@sim590

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:

PowerShell/PowerShell#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions