Powershell Invoke-RestMethod使用自签名证书和基本身份验证 - 任何示例?

Gra*_*zer 8 rest powershell-3.0

我正在使用RESTful API并使用Firefox RESTClient插件一切都很好.我可以轻松查询API.

但是,当我尝试将相同的API调用插入powershell时,它不起作用!

我已经尝试了以下代码来自其他各种帖子,这些帖子应该排除证书问题,但我仍然无法使其工作:

    # 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`
Run Code Online (Sandbox Code Playgroud)

我无法使用Powershell V3的Invoke-RestMethod复制这个,并且想知道是否有人可以共享示例代码以访问具有自签名证书并且还使用基本授权的HTTPS restful API.

错误消息我得到:

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
Run Code Online (Sandbox Code Playgroud)

Gra*_*zer 10

我的解决方案的来源

使用这个让我进入实际的服务器....现在有一个HTTP 500错误,但这个错误是另一天.

这是我的"工作"片段:

     add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;

            public class IDontCarePolicy : ICertificatePolicy {
            public IDontCarePolicy() {}
            public bool CheckValidationResult(
                ServicePoint sPoint, X509Certificate cert,
                WebRequest wRequest, int certProb) {
                return true;
            }
        }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Headers @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="} -Method Get
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助其他人从几个令人沮丧的时间:)

  • 只是其他用户的注意事项,请注意此IDontCarePolicy会影响PowerShell会话中的所有网络流量,因此,一旦将CertificatePolicy设置为该值,**从那时起您将执行任何操作都将检查SSL证书的有效性. (3认同)

maj*_*tor 7

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Run Code Online (Sandbox Code Playgroud)