Created
April 17, 2013 14:18
-
-
Save CharlesB2/5404653 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Using Start-Process you can get the correct encoding of your program output. | |
#However you have to use a temp. filename because it's the only thing Start-Process allows to. | |
#I wrote this function, taking program path and arguments, and returns stdout decoded from | |
#program output as UTF8 Function | |
Start-ProcessClean | |
{ | |
Param( [Parameter(Mandatory=$True, Position=1)][string]$FilePath, | |
[Parameter(Mandatory=$False,Position=2)][string[]]$ArgumentList ) | |
$stdOutTempFile = [IO.Path]::GetTempFileName() | |
$stdErrTempFile = [IO.Path]::GetTempFileName() | |
Start-Process $FilePath -ArgumentList $ArgumentList -NoNewWindow -Wait -RedirectStandardOutput $stdOutTempFile -RedirectStandardError $stdErrTempFile | |
$stdout = Get-Content $stdOutTempFile -Encoding UTF8 | |
rm $stdOutTempFile; rm $stdErrTempFile | |
return $stdout | |
} | |
#Call it like | |
$out = Start-ProcessClean "program.exe" 'first_argument "argument with spaces"' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment