function Read-StreamAsString { <# .SYNOPSIS Reads a given stream as a string. This is just a wrapper on a streamreader .PARAMETER Stream [System.IO.Stream] Must be an open, readable stream, preferably at position 0 already. #> param ( [System.IO.Stream]$Stream ) $response = "[[Stream was not readable]]" # Reset to the beginning in case it isn't at the beginning # This is likely to be the largest source of errors # The other is if the stream is already closed if ($Stream.CanSeek) { $Stream.Position = 0 } if ($Stream.Position -eq 0) { $streamReader = [System.IO.StreamReader]::new($Stream) $response = $streamReader.ReadToEnd() $streamReader.Close() $Stream.Close() } return $response }