Powershell 6+ (in this case v7) - GetResponseStream
5M7X opened this issue · 1 comments
5M7X commented
$resp = $_.Exception.Response.GetResponseStream()
this line causes a problem as GetResponseStream() seems to be deprecated since powershell v6+.
I ran into this error with the latest powershell v7.
A possible fix would be a wrapper like this one here: https://stackoverflow.com/a/48154663/16052146
if ($PSVersionTable.PSVersion.Major -lt 6) {
if ($Error.Exception.Response) {
$Reader = New-Object System.IO.StreamReader($Error.Exception.Response.GetResponseStream())
$Reader.BaseStream.Position = 0
$Reader.DiscardBufferedData()
$ResponseBody = $Reader.ReadToEnd()
if ($ResponseBody.StartsWith('{')) {
$ResponseBody = $ResponseBody | ConvertFrom-Json
}
return $ResponseBody
}
}
else {
return $Error.ErrorDetails.Message
}
}
try {
$result = Invoke-WebRequest ...
}
catch {
ParseErrorForResponseBody($_)
}
Tried it out but it then breaks
710 | $reader = New-Object System.IO.StreamReader($resp)
5M7X commented