【问题标题】:HTTPWebRequest.GetResponse() - The SSL connection could not be established, see inner exception (Cannot index into a null array)HTTPWebRequest.GetResponse() - 无法建立 SSL 连接,请参阅内部异常(无法索引到空数组)
【发布时间】:2022-01-06 08:26:58
【问题描述】:

我尝试使用来自https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.1.0.0 的 HPECMDLets。

当我连接到 HPE 服务器时,我收到以下错误:

Line |
72   |  [System.Net.WebResponse] $resp = $httpWebRequest.GetResponse()
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Exception calling "GetResponse" with "0" argument(s): "The SSL connection could not be established, see inner exception."

InnerException ($Errors[0].Exception.InnerException | FL *) 是:

InvalidOperation: Cannot index into a null array.

我正在使用带有 PowerShell 7.2 的 Windows Server 2012R2。

我还尝试弄清楚 Connect-HPERedfish 函数的结构并创建自己的脚本:

$Credential = Import-Clixml -Path "<Path-to-Credential>"
$Address = '192.168.1.1'
$DisableCertificateAuthentication = $True
$DisableExpect100Continue = $True

$session = $null
$wr = $null
$httpWebRequest = $null
$OrigCertFlag = $false

$un = $Credential.UserName
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)
$pw = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr)


$unpw = @{'UserName'=$un; 'Password'=$pw}
$jsonStringData = $unpw | ConvertTo-Json
$session = $null


[IPAddress]$ipAddress = $null
if([IPAddress]::TryParse($Address, [ref]$ipAddress))
{
    if(([IPAddress]$Address).AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6 -and $Address.IndexOf('[') -eq -1)
    {
        $Address = '['+$Address+']'
    }
}
$baseUri = "https://$Address"
$odataid = "/redfish/v1/"
$uri = (New-Object System.Uri -ArgumentList @([URI]$baseUri, $Odataid)).ToString()
$method = "GET"

Start-Sleep -Milliseconds 300
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

$wr = [System.Net.WebRequest]::Create($uri)
$httpWebRequest = [System.Net.HttpWebRequest]$wr
$httpWebRequest.Method = $method
$httpWebRequest.AutomaticDecompression = [System.Net.DecompressionMethods]::GZip
$httpWebRequest.Headers.Add('Odata-version','4.0')
$httpWebRequest.ServicePoint.Expect100Continue = -not($DisableExpect100Continue)
$httpWebRequest.ServerCertificateValidationCallback = {$DisableCertificateAuthentication}
[System.Net.WebResponse] $resp = $httpWebRequest.GetResponse() << Error!

到目的地的 Invoke-Webrequest 成功:

Invoke-WebRequest https://192.168.1.1/ -SkipCertificateCheck

另外 - 使用 PowerShell 5,使用上述脚本连接到服务器是成功的! 那么,为什么我在使用 PowerShell 7 时遇到错误?

非常感谢您的帮助

PS:有时复制粘贴错误可能是致命的: 内部异常现在给了我正确的输出:

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.Runspace type. 
The script block you attempted to invoke was: $false

我找到了关注的帖子:https://github.com/PowerShell/PowerShell/issues/11658,但那里指向所谓的解决方案的链接不起作用..

PPS:关于https://www.agilepointnxblog.com/powershell-error-there-is-no-runspace-available-to-run-scripts-in-this-thread我删除了ServerCertificateValidationCallback属性:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null

但这会导致innerException:

System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch, RemoteCertificateChainErrors

所以我需要将 [System.Net.ServicePointManager]::ServerCertificateValidationCallback 设置为 $true 以跳过证书检查..

【问题讨论】:

    标签: https windows2012 powershell-7.2


    【解决方案1】:

    此问题的根本原因是 PowerShell Core+ 中 ServicePointManager 的 ServerCertificateValidationCallback 属性不兼容。

    该属性仅在 PowerShell 5.1 中受支持,在 PowerShell Core 及更高版本中不支持。

    链接: https://get-powershellblog.blogspot.com/2017/12/powershell-core-web-cmdlets-in-depth.html

    https://github.com/PowerShell/PowerShell/issues/13597

    https://ebookreading.net/view/book/EB9781789536669_423.html#

    Getting "Unable to find type [System.Net.ServicePointManager]." in PowerShell for OS X

    https://github.com/dotnet/runtime/issues/17154

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 1970-01-01
      • 2021-12-09
      • 2020-06-25
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 2022-08-31
      相关资源
      最近更新 更多