dafthack / MFASweep

A tool for checking if MFA is enabled on multiple Microsoft Services
MIT License
1.38k stars 188 forks source link

Powershell 6+ (in this case v7) - GetResponseStream #4

Open 5M7X opened 3 years ago

5M7X commented 3 years ago

$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 3 years ago

getresponsestatus_powershell7

getresponsestatus_powershell7_version

m-hanisch commented 2 weeks ago

@5M7X Have you retrieved any workaround in the meantime? Currently, facing the same issue.