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.

Parents
  • 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 :)

Reply
  • 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 :)

Children
No Data