We use PowerShell Remoting (PSRemoting) for various maintenance tasks on member servers and workstations in a bilingual environment (English and French).
Output from Microsoft utilities are perfectly displayed in either language when run on the local console but diacritical characters are garbled when displayed through the WinRM (Windows Remote Management) tunnel used by PSRemoting.
Taking vssadmin as an example, the string
List Providers`` ``- Liste les fournisseurs enregistrés de clichés instantanés de volume
is displayed as
List Providers`` ``- Liste les fournisseurs enregistr‚s de clich‚s instantan‚s de volume
when vssadmin is running remotely.
In our case, it seems the [Console]::OutputEncoding.CodePage is set to 1252 (the default "ANSI code page") in the remote session while the local script is using codepage 850 (DOS Latin-1). Fair enough.
However, the utility running in the remote session is still using codepage 850 despite the [Console]::OutputEncoding.CodePage set to 1252. Hence the gibberish.
To illustrate this condition, the script below will PSRemote on the local machine and display the utility output (with gibberish) and translation to the caller’s [Console]::OutputEncoding.CodePage. In other words, a single computer is being used to display this behavior.
In this example, the lowercase e acute is at code point 130 in code page 850 and at code point 233 in code page 1252: translation is required.
The test script demonstrate that with manual translation, the proper output is displayed on the source console. This is accomplished with:
$Encoding = [System.Text.Encoding]::Default # This is 1252 in this test
$Output = [System.Text.Encoding]::GetEncoding([int]$SourceEncoding) # This is 850 in this test
$Output.GetString( $Encoding.GetBytes($MojiBake[$i].Line) ) # This is black magic!
Is there a way to avoid this “manual” translation ?
Sample output from the test script:
Console Code Page: 850
Windows 10 Pro 26H2 (Build 26300.9711)
This is PowerShell 5.1.26100.9711
Caller's encoding: 850
Remote Console Code Page: 1252
The caller's encoding is used instead of the remote encoding: True
Test data:
List Providers - Liste les fournisseurs enregistr‚s de clich‚s instantan‚s de volume
List Shadows - Liste les clich‚s instantan‚s de volume existants
List ShadowStorage - Liste les associations de stockage de clich‚s instantan‚s de volume
List Volumes - Liste les volumes ‚ligibles pour les clich‚s instantan‚s
List Writers - Liste les r‚dacteurs enregistr‚s de clich‚s instantan‚s de volume
Correct data:
List Providers - Liste les fournisseurs enregistrés de clichés instantanés de volume
List Shadows - Liste les clichés instantanés de volume existants
List ShadowStorage - Liste les associations de stockage de clichés instantanés de volume
List Volumes - Liste les volumes éligibles pour les clichés instantanés
List Writers - Liste les rédacteurs enregistrés de clichés instantanés de volume
The test script:
$SomeScript = {
# Param([Parameter(ValueFromPipeline=$True)][String[]]$SourceEncoding)
Param([String]$SourceEncoding, [String]$SourceString)
### Display OS and PowerShell versions
$os = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
Write-Output ""
Write-Output "$($os.ProductName) $($os.DisplayVersion) (Build $($os.CurrentBuildNumber).$($os.UBR))"
Write-Output "This is PowerShell $($PSVersionTable.PSVersion -join ".")"
Write-Output ""
### Display the source Encoding
Write-Output "Caller's encoding: $SourceEncoding"
### Display Remote Console CodePage
Write-Output "Remote Console Code Page: $([Console]::OutputEncoding.CodePage)"
### Get some native output from this system
$VSSAdmin = $( Invoke-Expression "vssadmin" | Select-String -Pattern 'List' -SimpleMatch )
### Translate native output to ...
$MojiBake = $VSSAdmin
$Encoding = [System.Text.Encoding]::Default
$Output = [System.Text.Encoding]::GetEncoding([int]$SourceEncoding)
Write-Output ""
Write-Output "The caller's encoding is used instead of the remote encoding: $($Output.GetString( $Encoding.GetBytes($MojiBake[0].Line) ) -ceq $SourceString)"
# Note: Output already contains newline characters.
Write-Output ""
Write-Output "Test data:"
for ($i = 0; $i -lt $MojiBake.Count; $i++) { $MojiBake[$i].Line }
Write-Output ""
Write-Output "Correct data:"
for ($i = 0; $i -lt $MojiBake.Count; $i++) { $Output.GetString( $Encoding.GetBytes($MojiBake[$i].Line) ) }
}
### Target System and User credentials authorized via WinRM
$computer = "localhost"
$Credential = Get-Credential -Message "Enter the credentials required to run on the remote system"
### Display Console CodePage and reference string, presuming the first line contains diacritical characters
Write-Output "Console Code Page: $([Console]::OutputEncoding.CodePage)"
$Reference = $( Invoke-Expression "vssadmin" | Select-String -Pattern 'List' -SimpleMatch )[0]
### Connect User to Target and execute script
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Invoke-command -computername $computer -Credential $Credential -ScriptBlock $SomeScript -ArgumentList $([Console]::OutputEncoding.CodePage), "$Reference"