0

I try to use the HPECMDLets from https://www.powershellgallery.com/packages/HPERedfishCmdlets/1.1.0.0.

When I connect to an HPE Server i get the following error:

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."

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

InvalidOperation: Cannot index into a null array.

Im using Windows Server 2012R2 with PowerShell 7.2.

I also tried figuring out the structure of the Connect-HPERedfish function and creating my own script:

$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!

The Invoke-Webrequest to the destination is successfull:

Invoke-WebRequest https://192.168.1.1/ -SkipCertificateCheck

In addition - with PowerShell 5, the connectioon to the server with the above script is successfull! So, why i got the error with PowerShell 7?

I am very grateful for any help

PS: Sometimes a copy-paste mistake can be fatal: The inner exception gives me now the right output:

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

I found the followed thread: https://github.com/PowerShell/PowerShell/issues/11658, but the link there to the alleged solution does not work..

PPS: Regarding to https://www.agilepointnxblog.com/powershell-error-there-is-no-runspace-available-to-run-scripts-in-this-thread I deleted the ServerCertificateValidationCallback property:

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

But this results in the innerException:

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

So I need to set [System.Net.ServicePointManager]::ServerCertificateValidationCallback to $true to Skip Certificate checks..

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

0

The root cause for this problem are incompatibilities about the ServerCertificateValidationCallback property of ServicePointManager in PowerShell Core+.

The property is only supported in PowerShell 5.1, not in PowerShell Core and newer.

Links: 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

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135