1
Vote

Error from webservice is outputtet via Write-Host

description

If you try to run a runbook that does not exist etc.

the error is outputtet through Write-Host

This makes it very hard to catch the error message from the webservice.
To make this work together with my internal SCSM workflows, that triggers runbooks, i have changed the module.

i also experienced that it tried to close the $response in the finally block without checking if it was null. this caused that error to be displayed instead of the real web service error.

the catch/finally now looks like this:
catch [System.Net.WebException]
{
    $errmsg =  "Exception occurred in $($MyInvocation.MyCommand): `n$($_.Exception.Message)"
    $errmsg +=  "Response = "
    $response = $($_.Exception.Response)

    # Show the response with the error information
    if ($response.ContentLength -gt 0)
    {

        $reader = [IO.StreamReader] $response.GetResponseStream()  
        $output = $reader.ReadToEnd()  
        $reader.Close()
        $errmsg += $output
       #Write-Host ""
    }

    $response = $null
    throw $errmsg
}
finally
{
    $output = $null
    if (($response -ne $null) -and ($response.ContentLength -gt 0))
    {
        $reader = [IO.StreamReader] $response.GetResponseStream()  
        $output = $reader.ReadToEnd()  
        [xml]$output = $output
        $reader.Close()  
    }

   if($response -ne $null) { $response.Close() }

    Write-Output $output
}

Do you have any comments about why it is sent to write-host, or how to get the error message? (maybe i overlooked the method you are using).

P.s. if you want, i would love to join this project and help out fixing bugs and improving the module. (I have 6 years of powershell and 2 years of Orchestrator trainer and consultant experience)

comments

JGS wrote Apr 17, 2013 at 9:35 AM

(i am now sure yet if this quickfix i did today is best practice. but i tried to change as little as possible)