Skip to content

Write-Host: remote sessions support#6

Open
sim590 wants to merge 2 commits intobeatcracker:masterfrom
sim590:write-host-in-remote-sessions
Open

Write-Host: remote sessions support#6
sim590 wants to merge 2 commits intobeatcracker:masterfrom
sim590:write-host-in-remote-sessions

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.


[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

}

[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

# 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