This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using invoke-restmethod from a remote machine

I am trying to get invoke-restmethod working from a remote machine.

I started experimenting with a very simple command to return all the agents.

invoke-restmethod -uri 'https://[server ip]:8006/apprecovery/api/core/agents/' -method "Get" -Credential $cred

The credential is generated with Get-Credential and I have [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} to address the self-signed cert.

I am getting a generic error:

invoke-restmethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:1 char:1
+ invoke-restmethod -uri 'https://[server ip]:8006/apprecovery/api/cor ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Any ideas where to go from here or were I may find more logs on the RR server?

It does load just fine from the same machine using a browser after the authentication prompt.

  • Hi mark.clift:
    Looks that you have stumbled over a Microsoft issue related to the GET method implementation. Basically, the Invoke-RestMethod -Method Get works either once (after a reboot) or not at all.

    The PUT and POST methods work fine but the DELETE Method does not like 'ServerCertificateValidationCallback' at all.

    Returning to your issue, the GET Method is a simple 'read from the server' operation. As such it looks that it can be replaced with Invoke-WebRequest. In reality, even this commandlet works only now and then and it looks that it was not fixed even in PowerShell 5.1.

    As such the best approach is to go the old (and tried) System.Net.WebClient way.
    This is how it works:

    $agentsquery = New-Object System.Net.WebClient
    $agentsquery.Credentials = get-credential
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
    $agents = ($agentsquery.DownloadString("https://[server ip]:8006/apprecovery/api/core/agents/")).agents.agent

    Hope that this helps :)

  • Tudor: Thank you for your reply. It helped!

    Especially thank you for suggested work around as it saved me the time it would have taken to find it which is great after all the time I spent on a bug that was never going to work.

    One change in the your suggested lines is to type the $agents variable as it returns a string otherwise.

    [xml]$agents = ($agentsquery.DownloadString("10.16.110.36:8006/.../agen
    ts/")).agents.agent
  • You are most welcome :)
    Looks that you have found a new issue :)
    The returned object should have been an XML object already (otherwise navigating to the 'agent' node would not be possible).
    Happy to find out that casting it to xml fixes it :)
    Please keep us posted with your work progress, I believe that it is very interesting for all of us!