gpt4 book ai didi

rest - Powershell 3.0 Invoke-WebRequest HTTPS 在所有请求上都失败

转载 作者:行者123 更新时间:2023-12-03 21:01:11 25 4
gpt4 key购买 nike

我正在尝试通过 Powershell 3.0 和 REST API 使用我们的负载均衡器。但是,无论我尝试什么,如果它是 https 请求,无论是对我们的负载均衡器还是对任何其他 https 站点,我目前都遇到了失败。我觉得我错过了一些明显的东西。

这是 https 失败的代码

try
{
#fails
#$location='https://www.bing.com'
#fails
#$location='https://www.google.com'
#fails
#$location='https://www.facebook.com'
#fails
#$location='https://www.ebay.com'
#works
#$location='http://www.bing.com'
#works
#$location='http://www.google.com'
#fails (looks like Facebook does a redirect to https://)
$location='http://www.facebook.com'
#works
#$location='http://www.ebay.com'
$response=''
$response = Invoke-WebRequest -URI $location
$response.StatusCode
$response.Headers
}
catch
{
Write-Host StatusCode $response.StatusCode
Write-Host $_.Exception
}

我得到的错误是:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.Management.Automation.PSInvalidOperationException: 
There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspa
ce type. The script block you attempted to invoke was: $true
at System.Net.TlsStream.EndWrite(IAsyncResult asyncResult)
at System.Net.ConnectStream.WriteHeadersCallback(IAsyncResult ar)
--- End of inner exception stack trace ---
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()

我希望 this page以及对底部的建议,包括来自 Aaron D. 的建议)会有所作为,但没有一个有所作为。
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}


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
}


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

我曾尝试切换到 Invoke-RestCommand 但无济于事,因为我得到了相同的响应。

感觉这必须是环境因素,因为我无法相信上述内容对其他人不起作用,但我已经在工作站和服务器上尝试过,结果相同(不完全排除环境但我知道它们的设置不同)。

有什么想法吗?

最佳答案

这对我来说非常有效。该站点默认使用 TLS 1.0,显然 PS 无法使用。我使用了这一行:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

我的 PS 脚本(到目前为止我测试过的所有脚本)运行良好。

关于rest - Powershell 3.0 Invoke-WebRequest HTTPS 在所有请求上都失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25143946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com