0

我尝试使用来自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.

我将 Windows Server 2012R2 与 PowerShell 7.2 一起使用。

我还尝试弄清楚 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 以跳过证书检查..

4

1 回答 1

0

此问题的根本原因是 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#

获取“无法找到类型 [System.Net.ServicePointManager]。” 在 OS X 的 PowerShell 中

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

于 2022-01-07T10:12:42.777 回答