Powershell v3의 Invoke-WebRequest 및 Invoke-RestMethod를 사용하여 성공적으로 POST 메서드를 사용하여 json 파일을 https 웹 사이트에 게시했습니다.
내가 사용하는 명령은
$cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("cert.crt")
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Body $json -ContentType application/json -Method POST
그러나 다음과 같은 GET 메서드를 사용하려고 할 때 :
Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert -Method GET
다음 오류가 반환됩니다.
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At line:8 char:11
+ $output = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
SSL 인증서를 무시하기 위해 다음 코드를 사용하려고 시도했지만 실제로 어떤 일을하고 있는지 확실하지 않습니다.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
누군가 여기서 무엇이 잘못 될 수 있고 어떻게 고칠 수 있는지에 대한 지침을 제공 할 수 있습니까?
감사
답변
이 해결 방법은 저에게 효과적이었습니다.
http://connect.microsoft.com/PowerShell/feedback/details/419466/new-webserviceproxy-needs-force-parameter-to-ignore-ssl-errors
기본적으로 PowerShell 스크립트에서 :
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$result = Invoke-WebRequest -Uri "https://IpAddress/resource"
답변
Lee의 대답은 훌륭하지만 웹 서버가 지원하는 프로토콜에 문제가있었습니다.
다음 줄도 추가 한 후 https 요청을받을 수 있습니다. 이 답변에서 지적했듯이 https://stackoverflow.com/a/36266735
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Lee의 코드로 내 완전한 솔루션.
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
답변
사용해 보셨나요 System.Net.WebClient
?
$url = 'https://IPADDRESS/resource'
$wc = New-Object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential("username","password")
$wc.DownloadString($url)
답변
순수한 대안 구현 파워 쉘 (없이 Add-Type
의씨# 출처):
#requires -Version 5
#requires -PSEdition Desktop
class TrustAllCertsPolicy : System.Net.ICertificatePolicy {
[bool] CheckValidationResult([System.Net.ServicePoint] $a,
[System.Security.Cryptography.X509Certificates.X509Certificate] $b,
[System.Net.WebRequest] $c,
[int] $d) {
return $true
}
}
[System.Net.ServicePointManager]::CertificatePolicy = [TrustAllCertsPolicy]::new()
답변
다음은 저에게 효과적이며 SSL 인증서 / 콜백 기능과 상호 작용하기 위해 더 이상 사용되지 않는 최신 수단을 사용하며 동일한 powershell 세션 내에서 동일한 코드를 여러 번로드하려고 시도하지 않습니다.
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback=@"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore();
이것은 다음 기사 https://d-fens.ch/2013/12/20/nobrainer-ssl-connection-error-when-using-powershell/ 에서 수정되었습니다.
답변
이 콜백 함수를 사용하여 SSL 인증서를 무시할 때 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
나는 항상 Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
당신이 가진 결과처럼 들리는 오류 메시지 를 받았습니다 .
아래 기능으로 이끄는 이 포럼 게시물 을 찾았 습니다 . 나는 이것을 다른 코드의 범위 내에서 한 번 실행하면 나를 위해 작동합니다.
function Ignore-SSLCertificates
{
$Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler = $Provider.CreateCompiler()
$Params = New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add("System.DLL") > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
{
public class TrustAll : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We create an instance of TrustAll and attach it to the ServicePointManager
$TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}
답변
EM7 OpenSource REST API에 대한 문서를 검색해 보았습니다. 지금까지 운이 없습니다.
http://blog.sciencelogic.com/sciencelogic-em7-the-next-generation/05/2011
OpenSource REST API에 대한 많은 이야기가 있지만 실제 API 또는 문서에 대한 링크는 없습니다. 나는 참을성이 없었을 것입니다.
다음은 시도해 볼 수있는 몇 가지 사항입니다.
$a = Invoke-RestMethod -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert
$a.Results | ConvertFrom-Json
API에서 가져온 열을 필터링 할 수 있는지 확인하려면 이것을 시도하십시오.
$a.Results | ft
또는 이것도 사용해 볼 수 있습니다
$b = Invoke-WebRequest -Uri https://IPADDRESS/resource -Credential $cred -certificate $cert
$b.Content | ConvertFrom-Json
컬 스타일 헤더
$b.Headers
Twitter JSON API로 IRM / IWR을 테스트했습니다.
$a = Invoke-RestMethod http://search.twitter.com/search.json?q=PowerShell
도움이 되었기를 바랍니다.
