Get output in UTF8

Андрей Михалевский 3,451 Reputation points
2021-07-05T14:47:00.513+00:00

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 for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

12 answers

Sort by: Most helpful
  1. Андрей Михалевский 3,451 Reputation points
    2021-07-06T14:39:27.997+00:00

    Yes, i have problem only with function Execute-Command

    0 comments No comments

  2. MotoX80 36,291 Reputation points
    2021-07-06T15:14:11+00:00

    Let's go back to my test script where I call cmd.exe. Paste this exact code into ISE and save it as a .PS1 file. Run it in both ISE and from a PS prompt. Does that work?

     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  
      }  
          
      $utfString = 'крокодил  трактор  президент  термометр  базар '  
      $utfString | out-file -FilePath c:\temp\utfString.txt -Encoding utf8       # create a file with utf8 contents   
          
      $DisableACMonitorTimeOut = Execute-Command -commandTitle "test" -commandPath "C:\windows\system32\cmd.exe" -commandArguments "/c type c:\temp\utfString.txt"  
              
      "Here is stdout:            {0}" -f $DisableACMonitorTimeOut.stdout  
      "Here is the file content:  {0}" -f $(get-content c:\temp\utfString.txt)  
      
    

    112215-capture.jpg

    0 comments No comments

  3. MotoX80 36,291 Reputation points
    2021-07-06T16:09:43.82+00:00

    It may be related to the fonts you are using or the system codepage.

    https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell

    For me, the Chinese characters display in ISE, but not in a console window.

    112210-capture.jpg

    0 comments No comments

  4. Андрей Михалевский 3,451 Reputation points
    2021-07-07T07:04:48.84+00:00

    Your code works perfectly. But how can I adapt it to myself?

    112348-%D1%81%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA.png

    0 comments No comments

  5. MotoX80 36,291 Reputation points
    2021-07-07T18:22:34.06+00:00

    But how can I adapt it to myself?

    I'm sorry, I don't understand the question.

    When I requested for you to "paste this exact code", my intention was to try to determine if the character display problem was related to certmgr.exe or if you had some other general problem that impacted every executable that Powershell ran. Since you changed cmd.exe to certmgr.exe, I still don't know the answer.

    I had also requested that you run certmgr.exe and redirect it's output to stdout.txt and stderr.txt. The intent there was to determine if there was a problem with Powershell capturing output or if there was a character problem that could be seen in notepad when running the program from a command prompt.

    Since I posted those, I discovered that my PC had a problem displaying the Chinese characters in a PS prompt. I traced that back to the font that was being used. That is one thing that you could could check. See if ISE and PS are using the same font.

    112665-capture.jpg

    112600-capture1.jpg

    If you don't get the correct output, then the question that I would have to ask is: do you really need to have ISE display the output correctly? ISE is normally used to develop scripts. Once a user gets their code working, they use Powershell.exe to run the .PS1 script. So if the output of your script looks ok in a PS window, is that good enough?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.