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

It might depend on what file.exe is and the output it produces.

This example seems to work for me.

If you question is really "how do I output the contents of a variable into a file as utf8", just look at how I create the utfString.txt file.

  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


111914-capture.jpg



capture.jpg (95.8 KiB)
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.

47451047 avatar image
0 Votes"
47451047 answered

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



I got like this. Text in Russian language. I need got in UTF8, wihtout import\export in file.


снимок.png (85.2 KiB)
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

If you change my example to include Russian words, does it work? I changed this statement and it displays ok.

  $utfString = '蓝屏日志 请问怎么处理呢        крокодил  трактор     президент  термометр  базар '

If that works ok, then open a command prompt and run the program and redirect the output to files. Then open the files with notepad and see if they look ok.

 certmgr.exe -list -store uMY 1>c:\temp\stdout.txt  2>c:\temp\stderr.txt

I am running Powershell 5.1 on Win10 21H1.

One thing that I find strange, is that your example shows you calling the Execute-Command function before you define it. I get an error when I do that. This may be the result of running the script multiple times in ISE. It appears to call the function that you defined in the prior execution of the script. Move the statements that call the function below the definition of the function.


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.

47451047 avatar image
0 Votes"
47451047 answered 47451047 edited

$test = "крокодил трактор президент термометр базар"
$test | out-file -FilePath C:\temp\test.txt

I got a normal encoding with the Russian language.

If I save the script in ps1 and run it in a normal powershell, there is no problem with the encoding. The only problem is with ISE

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

The only problem is with ISE

So this script produces unreadable characters? Do you have another PC that you can test on?

 cls
 $test = "крокодил трактор президент термометр базар"
 'The $test variable contains: {0}' -f $test  
 $test | out-file -FilePath C:\temp\test.txt
 "Here is the contents of the test file."
 get-content  C:\temp\test.txt


It works for me in both ISE and a PS prompt.

112242-capture.jpg




capture.jpg (64.3 KiB)
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.

47451047 avatar image
0 Votes"
47451047 answered

Yes, i have problem only with function Execute-Command

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

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



capture.jpg (96.0 KiB)
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

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



capture.jpg (95.5 KiB)
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.

47451047 avatar image
0 Votes"
47451047 answered

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



снимок.png (187.6 KiB)
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

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?



capture.jpg (72.1 KiB)
capture1.jpg (135.6 KiB)
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.