# encoding: ascii
# api: powershell
# title: Receive-Stream
# description: A very simple stream-reader implementation (with no error handling) suitable for simple interactive script task …
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Receive-Stream
# x-poshcode-id: 1688
# x-archived: 2017-04-09T22:23:55
# x-published: 2010-03-11T07:24:00
#
#
function Receive-Stream {
#.Synopsis
# Read a stream to the end and close it
#.Description
# Will read from a byte stream and output a string or a file
#.Param reader
# The stream to read from
#.Param fileName
# A file to write to. If this parameter is provided, the function will output to file
#.Param encoding
# The text encoding, if you want to override the default.
param( [System.IO.Stream]$reader, $fileName, $encoding = [System.Text.Encoding]::GetEncoding( $null ) )
if($fileName) {
$writer = new-object System.IO.FileStream $fileName, "Create"
} else {
[string]$output = ""
}
[byte[]]$buffer = new-object byte[] 4096
[int]$total = [int]$count = 0
do
{
$count = $reader.Read($buffer, 0, $buffer.Length)
if($fileName) {
$writer.Write($buffer, 0, $count)
} else {
$output += $encoding.GetString($buffer, 0, $count)
}
} while ($count -gt 0)
$reader.Close()
if(!$fileName) { $output }
}