ps/Modules/Cole.PowerShell.Developer/Public/Read-StreamAsString.ps1

29 lines
798 B
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
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
}