param([Parameter(Mandatory=$true)][string]$HostLabel, [Parameter(Mandatory=$true)][string]$OutFile) $o = New-Object System.Collections.Specialized.OrderedDictionary $src = @" using System; using System.Runtime.InteropServices; public static class K32 { [DllImport("kernel32.dll", SetLastError=true)] 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(); [DllImport("kernel32.dll")] public static extern uint GetACP(); } "@ Add-Type -TypeDefinition $src -ErrorAction Stop $hOut = [K32]::GetStdHandle(-11); $hIn = [K32]::GetStdHandle(-10) $mOut = 0; $mIn = 0 $okOut = [K32]::GetConsoleMode($hOut, [ref]$mOut) $okIn = [K32]::GetConsoleMode($hIn, [ref]$mIn) $o['HostLabel'] = $HostLabel $o['PSVersion'] = $PSVersionTable.PSVersion.ToString() $o['CLR'] = [System.Environment]::Version.ToString() $o['IsOutputRedirected'] = [Console]::IsOutputRedirected $o['IsInputRedirected'] = [Console]::IsInputRedirected $o['IsErrorRedirected'] = [Console]::IsErrorRedirected $o['GetConsoleWindow'] = ('0x{0:X}' -f ([K32]::GetConsoleWindow().ToInt64())) $o['GetConsoleOutputCP'] = [K32]::GetConsoleOutputCP() $o['GetACP'] = [K32]::GetACP() $o['OutputEncoding'] = [Console]::OutputEncoding.WebName $o['GetConsoleMode_STDOUT_ok'] = $okOut $o['STDOUT_mode_hex'] = ('0x{0:X}' -f $mOut) $o['STDOUT_VT_PROCESSING_0x4'] = (($mOut -band 4) -eq 4) $o['STDOUT_PROCESSED_OUTPUT_0x1'] = (($mOut -band 1) -eq 1) $o['STDOUT_WRAP_EOL_0x2'] = (($mOut -band 2) -eq 2) $o['STDIN_GetConsoleMode_ok'] = $okIn $o['STDIN_mode_hex'] = ('0x{0:X}' -f $mIn) $o['STDIN_VT_INPUT_0x200'] = (($mIn -band 0x200) -eq 0x200) $raw = $Host.UI.RawUI $o['RawUI_TypeName'] = $raw.GetType().FullName $o['RawUI_members'] = (($raw | Get-Member -MemberType Property | Select-Object -ExpandProperty Name) -join ',') $uiProp = $Host.UI | Get-Member -Name SupportsVirtualTerminal -MemberType Property -ErrorAction SilentlyContinue $o['HostUI_has_SupportsVirtualTerminal_member'] = [bool]$uiProp $o['Value_Host_UI_SupportsVirtualTerminal'] = [string]$Host.UI.SupportsVirtualTerminal $o['RawUI_has_SupportsVirtualTerminal'] = [bool]($raw | Get-Member -Name SupportsVirtualTerminal -ErrorAction SilentlyContinue) try { $o['Value_RawUI_SupportsVirtualTerminal'] = [string]$raw.SupportsVirtualTerminal } catch { $o['Value_RawUI_SupportsVirtualTerminal'] = 'THREW: ' + $_.Exception.Message } try { $w = $raw.WindowSize; $o['RawUI_WindowSize'] = "$($w.Width)x$($w.Height)" } catch { $o['RawUI_WindowSize'] = 'THREW: ' + $_.Exception.Message } try { $b = $raw.BufferSize; $o['RawUI_BufferSize'] = "$($b.Width)x$($b.Height)" } catch { $o['RawUI_BufferSize'] = 'THREW: ' + $_.Exception.Message } try { $o['RawUI_MaxPhysicalWindowSize'] = (($raw.MaxPhysicalWindowSize).Width.ToString() + 'x' + ($raw.MaxPhysicalWindowSize).Height.ToString()) } catch { $o['RawUI_MaxPhysicalWindowSize'] = 'THREW: ' + $_.Exception.Message } try { $raw.CursorPosition = (New-Object System.Management.Automation.Host.Coordinates 3,3); $o['SetCursorPosition_ok'] = $true; $o['CursorPosition_readback'] = "$($raw.CursorPosition.X),$($raw.CursorPosition.Y)" } catch { $o['SetCursorPosition_ok'] = 'THREW: ' + $_.Exception.Message } try { $o['KeyAvailable_read_ok'] = 'ok: ' + $raw.KeyAvailable } catch { $o['KeyAvailable_read_ok'] = 'THREW: ' + $_.Exception.Message } # Chinese width probe, ASCII-source via code points $zh = [string]::Concat([char]0x4E2D, [char]0x6587) # Zhong Wen $ascii = 'AB' $o['zh_string'] = $zh $o['zh_Length'] = $zh.Length $o['ascii_Length'] = $ascii.Length $o['GetConsoleWindowTitle'] = [Console]::Title $json = $o | ConvertTo-Json -Compress [System.IO.File]::WriteAllText($OutFile, $json, (New-Object System.Text.UTF8Encoding($false))) $raw.CursorPosition = (New-Object System.Management.Automation.Host.Coordinates 0,0) Write-Host '' Write-Host ("--- console probe [{0}] PS {1} VT={2} mode={3} ---" -f $HostLabel, $PSVersionTable.PSVersion, $Host.UI.SupportsVirtualTerminal, ('0x{0:X}' -f $mOut)) -ForegroundColor Yellow Write-Host ("ANSI test: " + [char]27 + "[31mRED-ANSI" + [char]27 + "[0m " + [char]27 + "[42mBG-GREEN" + [char]27 + "[0m " + [char]27 + "[1;36mBOLD-CYAN" + [char]27 + "[0m") Write-Host ("Unicode box: " + [char]0x250C + [char]0x2500 + [char]0x2510) Write-Host ("Chinese: " + $zh + " Length=" + $zh.Length) Write-Host '--- end ---' Start-Sleep -Seconds 4