Skip to content

Write-Host: remote sessions support #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sim590
Copy link

@sim590 sim590 commented Aug 1, 2023

As mentioned in #5, using System.Console.Write(Line) functions doesn't work from inside remote sessions. Using the base Write-Host here is just simpler and should work in all situations.

@sim590 sim590 changed the title write-host: remote sessions support Write-Host: remote sessions support Aug 1, 2023
@sim590
Copy link
Author

sim590 commented Nov 21, 2023

@beatcracker : I updated following the suggestion you made in #5.

@@ -71,11 +70,14 @@ function Write-Host {
$item = $AnsiTemplate -f $AnsiColor[$ForegroundColor.value__], $item, 39
}

[System.Console]::$Method($item)
if ($Host.Name -eq "ServerRemoteHost") {
if ($NoNewLine) { Microsoft.PowerShell.Utility\Write-Host -NoNewLine $item } else { Microsoft.PowerShell.Utility\Write-Host $item }
Copy link
Owner

@beatcracker beatcracker Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Can be simplified even further by dropping if like this:

Microsoft.PowerShell.Utility\Write-Host -NoNewLine:$NoNewLine -Object $item

@@ -71,11 +70,14 @@ function Write-Host {
$item = $AnsiTemplate -f $AnsiColor[$ForegroundColor.value__], $item, 39
}

[System.Console]::$Method($item)
if ($Host.Name -eq "ServerRemoteHost") {
Copy link
Owner

@beatcracker beatcracker Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested comment to add:

# [console]:write* doesn't work in remote sessions
# https://github.com/PowerShell/PowerShell/issues/20054
# https://github.com/beatcracker/Powershell-Misc/issues/5

@@ -57,8 +57,7 @@ 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' }
if ($Host.UI.SupportsVirtualTerminal -or $Host.Name -eq "ServerRemoteHost") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to use single quotes here, since we're not doing variable expansion:

$Host.Name -eq 'ServerRemoteHost'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants