question

47451047 avatar image
0 Votes"
47451047 asked DyakovAlexander answered

Get output in UTF8

Hi. I have code powershell:

 $DisableACMonitorTimeOut = Execute-Command -commandTitle "test" -commandPath "C:\Program Files\MyProgram\file.exe" -commandArguments "-help"
    
 $DisableACMonitorTimeOut.stdout
    
 Function Execute-Command ($commandTitle, $commandPath, $commandArguments)
 {
     Try {
         $pinfo = New-Object System.Diagnostics.ProcessStartInfo
         $pinfo.FileName = $commandPath
         $pinfo.RedirectStandardError = $true
         $pinfo.RedirectStandardOutput = $true
         $pinfo.UseShellExecute = $false
         $pinfo.WindowStyle = 'Hidden'
         $pinfo.CreateNoWindow = $True
         $pinfo.Arguments = $commandArguments
         $p = New-Object System.Diagnostics.Process
         $p.StartInfo = $pinfo
         $p.Start() | Out-Null
         $stdout = $p.StandardOutput.ReadToEnd()
         $stderr = $p.StandardError.ReadToEnd()
         $p.WaitForExit()
         $p | Add-Member "commandTitle" $commandTitle
         $p | Add-Member "stdout" $stdout
         $p | Add-Member "stderr" $stderr
     }
     Catch {
     }
     $p
 }

How i can got output in UTF8 ?

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

I found this... add one of these lines and see if you get different results.


 $pinfo.StandardOutputEncoding =  [System.Text.Encoding]::UTF8 

or


  $pinfo.StandardOutputEncoding =  [System.Text.Encoding]::Unicode 




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DyakovAlexander avatar image
0 Votes"
DyakovAlexander answered

UTF8 not work for me, instead, I use next:

 $pinfo.StandardOutputEncoding = [System.Text.Encoding]::GetEncoding(866)

https://stackoverflow.com/questions/16803748/how-to-decode-cmd-output-correctly/26015551#26015551

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.