param([Parameter(Mandatory=$true)][string]$HostLabel, [Parameter(Mandatory=$true)][string]$OutFile, [int]$WatchSeconds = 3) $o = New-Object System.Collections.Specialized.OrderedDictionary $src = @" using System; using System.Runtime.InteropServices; public static class K32 { [DllImport("kernel32.dll")] public static extern IntPtr GetStdHandle(int n); [DllImport("kernel32.dll", SetLastError=true)] public static extern bool GetConsoleMode(IntPtr h, out uint m); [DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("kernel32.dll")] public static extern uint GetConsoleOutputCP(); } "@ Add-Type -TypeDefinition $src -ErrorAction Stop $mOut = 0; $ok = [K32]::GetConsoleMode([K32]::GetStdHandle(-11), [ref]$mOut) $o['HostLabel'] = $HostLabel $o['PSVersion'] = $PSVersionTable.PSVersion.ToString() $o['IsOutputRedirected'] = [Console]::IsOutputRedirected $o['HWND'] = ('0x{0:X}' -f ([K32]::GetConsoleWindow().ToInt64())) $o['ConsoleTitle'] = [Console]::Title $o['GetConsoleOutputCP'] = [K32]::GetConsoleOutputCP() $o['OutputEncoding'] = [Console]::OutputEncoding.WebName $o['STDOUT_mode_hex'] = ('0x{0:X}' -f $mOut) $o['STDOUT_VT_PROCESSING_0x4'] = (($mOut -band 4) -eq 4) $o['Host_UI_SupportsVirtualTerminal'] = [string]$Host.UI.SupportsVirtualTerminal $o['RawUI_SupportsVirtualTerminal_member'] = [bool]($Host.UI.RawUI | Get-Member -Name SupportsVirtualTerminal -ErrorAction SilentlyContinue) $o['WT_SESSION'] = [string]$env:WT_SESSION $o['WT_PROFILE_ID'] = [string]$env:WT_PROFILE_ID $o['TERM_PROGRAM'] = [string]$env:TERM_PROGRAM $o['TERM'] = [string]$env:TERM $o['ConEmuANSI'] = [string]$env:ConEmuANSI $o['PSEdition'] = $PSVersionTable.PSEdition try { $o['RawUI_KeyAvailable'] = 'ok:' + $Host.UI.RawUI.KeyAvailable } catch { $o['RawUI_KeyAvailable'] = 'THREW: ' + $_.Exception.Message } try { $o['Console_KeyAvailable'] = 'ok:' + [Console]::KeyAvailable } catch { $o['Console_KeyAvailable'] = 'THREW: ' + $_.Exception.Message } try { [Console]::SetCursorPosition(5, 5) $o['Console_SetCursorPosition_5_5'] = 'ok -> CursorLeft=' + [Console]::CursorLeft + ' CursorTop=' + [Console]::CursorTop } catch { $o['Console_SetCursorPosition_5_5'] = 'THREW: ' + $_.Exception.Message } try { $c = New-Object System.Management.Automation.Host.Coordinates 7, 7 $Host.UI.RawUI.CursorPosition = $c $o['RawUI_SetCursorPosition'] = 'ok -> ' + $Host.UI.RawUI.CursorPosition.X + ',' + $Host.UI.RawUI.CursorPosition.Y } catch { $o['RawUI_SetCursorPosition'] = 'THREW: ' + $_.Exception.Message } try { $o['RawUI_WindowSize'] = "$($Host.UI.RawUI.WindowSize.Width)x$($Host.UI.RawUI.WindowSize.Height)" } catch { $o['RawUI_WindowSize'] = 'THREW: ' + $_.Exception.Message } try { $o['RawUI_BufferSize'] = "$($Host.UI.RawUI.BufferSize.Width)x$($Host.UI.RawUI.BufferSize.Height)" } catch { $o['RawUI_BufferSize'] = 'THREW: ' + $_.Exception.Message } try { $o['RawUI_MaxWindowSize'] = "$($Host.UI.RawUI.MaxWindowSize.Width)x$($Host.UI.RawUI.MaxWindowSize.Height)" } catch { $o['RawUI_MaxWindowSize'] = 'THREW: ' + $_.Exception.Message } try { $o['RawUI_MaxPhysicalWindowSize'] = "$($Host.UI.RawUI.MaxPhysicalWindowSize.Width)x$($Host.UI.RawUI.MaxPhysicalWindowSize.Height)" } catch { $o['RawUI_MaxPhysicalWindowSize'] = 'THREW: ' + $_.Exception.Message } try { $o['CursorSize'] = [string]$Host.UI.RawUI.CursorSize } catch { $o['CursorSize'] = 'THREW' } try { $o['ForegroundColor'] = [string]$Host.UI.RawUI.ForegroundColor } catch { $o['ForegroundColor'] = 'THREW' } try { $o['WindowTitle'] = [string]$Host.UI.RawUI.WindowTitle } catch { $o['WindowTitle'] = 'THREW' } $o['ProgressPreference'] = [string]$ProgressPreference $o['HostName'] = $Host.Name $json = $o | ConvertTo-Json -Compress [System.IO.File]::WriteAllText($OutFile, $json, (New-Object System.Text.UTF8Encoding($false))) Write-Host ("[{0}] PS {1} VT={2} redirected={3} WT_SESSION={4}" -f $HostLabel, $PSVersionTable.PSVersion, $Host.UI.SupportsVirtualTerminal, [Console]::IsOutputRedirected, $env:WT_SESSION) -ForegroundColor Yellow Write-Host (" ANSI: " + [char]27 + "[31mRED" + [char]27 + "[0m " + [char]27 + "[104mBG-BRIGHT-BLUE" + [char]27 + "[0m " + [char]27 + "[38;2;255;128;0mTRUECOLOR" + [char]27 + "[0m") Write-Host (" Positioned: " + [char]27 + "[2;10HAT-ROW2-COL10") if ($WatchSeconds -gt 0) { Start-Sleep -Seconds $WatchSeconds }