Gram z RESTful API i za pomocą wtyczki RESTClient RESTClient wszystko jest dobrze. Mogę łatwo zapytać o API.Powershell Invoke-RestMethod przy użyciu certyfikatów z własnym podpisem i podstawowego uwierzytelniania - dowolne przykłady?
Jednak, gdy próbuję podłączyć to samo wywołanie API do powłoki powershell, to nie działa!
Próbowałem następujący kod z różnych innych stanowisk, które powinny wykluczać kwestii certyfikatów, ale nadal nie można uzyskać to do pracy:
# 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`
nie jestem w stanie powtórzyć to przy użyciu PowerShell V3 powołują -RestMethod i zastanawiał się, czy ktoś mógłby udostępnić przykładowy kod dostępu do interfejsu API HTTPS, który ma samopodpisany certyfikat, a także korzystać z podstawowej autoryzacji.
Komunikat o błędzie pojawia się:
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
Tylko uwaga dla innych użytkowników, należy pamiętać, że to wszystko wpływa IDontCarePolicy ruch sieciowy z sesji PowerShell, więc gdy ustawisz wartość CertificatePolicy na taką, ** nic **, które robisz od tego momentu, sprawdzi ważność certyfikatów SSL. – Jaykul