PoshCode Logo PowerShell Code Repository

Invoke-BinaryProcess.ps1 by Lee Holmes 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2175"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Invoke-BinaryProcess
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Invokes a process that emits or consumes binary data.
  15.  
  16. .EXAMPLE
  17.  
  18. Invoke-BinaryProcess binaryProcess.exe -RedirectOutput |
  19.        Invoke-BinaryProcess binaryProcess.exe -RedirectInput
  20.  
  21. #>
  22.  
  23. param(
  24.     ## The name of the process to invoke
  25.     [string] $ProcessName,
  26.  
  27.     ## Specifies that input to the process should be treated as
  28.     ## binary
  29.     [Alias("Input")]
  30.     [switch] $RedirectInput,
  31.  
  32.     ## Specifies that the output of the process should be treated
  33.     ## as binary
  34.     [Alias("Output")]
  35.     [switch] $RedirectOutput,
  36.  
  37.     ## Specifies the arguments for the process
  38.     [string] $ArgumentList
  39. )
  40.  
  41. Set-StrictMode -Version Latest
  42.  
  43. ## Prepare to invoke the process
  44. $processStartInfo = New-Object System.Diagnostics.ProcessStartInfo
  45. $processStartInfo.FileName = (Get-Command $processname).Definition
  46. $processStartInfo.WorkingDirectory = (Get-Location).Path
  47. if($argumentList) { $processStartInfo.Arguments = $argumentList }
  48. $processStartInfo.UseShellExecute = $false
  49.  
  50. ## Always redirect the input and output of the process.
  51. ## Sometimes we will capture it as binary, other times we will
  52. ## just treat it as strings.
  53. $processStartInfo.RedirectStandardOutput = $true
  54. $processStartInfo.RedirectStandardInput = $true
  55.  
  56. $process = [System.Diagnostics.Process]::Start($processStartInfo)
  57.  
  58. ## If we've been asked to redirect the input, treat it as bytes.
  59. ## Otherwise, write any input to the process as strings.
  60. if($redirectInput)
  61. {
  62.     $inputBytes = @($input)
  63.     $process.StandardInput.BaseStream.Write($inputBytes, 0, $inputBytes.Count)
  64.     $process.StandardInput.Close()
  65. }
  66. else
  67. {
  68.     $input | % { $process.StandardInput.WriteLine($_) }
  69.     $process.StandardInput.Close()
  70. }
  71.  
  72. ## If we've been asked to redirect the output, treat it as bytes.
  73. ## Otherwise, read any input from the process as strings.
  74. if($redirectOutput)
  75. {
  76.     $byteRead = -1
  77.     do
  78.     {
  79.         $byteRead = $process.StandardOutput.BaseStream.ReadByte()
  80.         if($byteRead -ge 0) { $byteRead }
  81.     } while($byteRead -ge 0)
  82. }
  83. else
  84. {
  85.     $process.StandardOutput.ReadToEnd()
  86. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me