Powershell Invoke-RestMethod utilisation de Certificats Auto-Signés et l'Authentification de Base - des Exemples?

Je suis en train de jouer avec une API RESTful et à l'aide de Firefox RESTClient plugin tout est bien. Je peut facilement effectuer une requête à l'API.

Cependant, quand j'ai essayer de brancher le même appel d'API en powershell il ne fonctionne pas!

J'ai essayé le code suivant à partir de divers autres postes qui devrait exclure les problèmes de certificat, mais je ne peux toujours pas obtenir que cela fonctionne:

    # This nugget should help me to get around any self-signed certificate issues I believe

    $netAssembly =[Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

    if($netAssembly)
    {
        $bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
        $settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")

        $instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())

        if($instance)
        {
            $bindingFlags = "NonPublic","Instance"
            $useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)

            if($useUnsafeHeaderParsingField)
            {
              $useUnsafeHeaderParsingField.SetValue($instance, $true)
            }
        }
    }

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

    $headers = @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="}

    # I exported this certificate from the web browser
    $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("HPCSA.crt")

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Certificate $cert -Headers $headers -Method Get`

Je ne suis pas capable de reproduire cela à l'aide de Powershell V3 est Invoke-RestMethod et je me demandais si quelqu'un pouvait partager des exemples de code pour l'accès à un HTTPS API restful qui a un certificat auto-signé et également l'utilisation de base de l'autorisation.

Message d'erreur que je reçois:

PS C:\Users\landg> C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1
Invoke-RestMethod : Unable to connect to the remote server
At C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1:31 char:1
+ Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

OriginalL'auteur Grazzer | 2012-08-29