Invoke-RestMethod コマンドレットやInvoke-WebRequestコマンドレットなどでhttpsを使用して接続してみたら、
以下のようなエラーが発生した場合、TLS1.2のみのサポートなどでエラーなった可能性があります。
Invoke-WebRequest : 接続が切断されました: 送信時に、予期しないエラーが発生しました。。 発生場所 行:1 文字:13 + $response = Invoke-WebRequest -UseBasicParsing "https:// ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest]、WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
本資料では、TLSのバージョンを指定する方法を記します。
以下のURLでは、TLS1.2を使用したサンプルコードを公開しています。
http://cryptocurrency.just4fun.biz/?API/CoinExchange.io#ld8fda54
TLS1.2を指定する方法は以下の構文です。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
一番最後にTls12と指定しているので、わかると思います。
Invoke-RestMethod コマンドレットやInvoke-WebRequestコマンドレットでhttps接続する前に
本構文を実行してください。
以下の構文でサポートされているTLSを確認することができます。
[enum]::GetNames([Net.SecurityProtocolType])
以下、実際に上記構文を実行したときの出力です。
PS C:\> [enum]::GetNames([Net.SecurityProtocolType]) SystemDefault Ssl3 Tls Tls11 Tls12
以下の構文で設定されているTLSを確認することができます。
[Net.ServicePointManager]::SecurityProtocol
Windows10のデフォルトでは以下のように出力されました。
PS C:\> [Net.ServicePointManager]::SecurityProtocol Ssl3, Tls
設定すれば、以下のようになります。
PS C:\> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 PS C:\> [Net.ServicePointManager]::SecurityProtocol Tls12
列挙型で複数指定すれば、複数のTLSバージョンを設定することができます。
以下の構文では、TLS, TLS1.1, TLS1.2を指定しています。
[Net.ServicePointManager]::SecurityProtocol = @([Net.SecurityProtocolType]::Tls,[Net.SecurityProtocolType]::Tls11,[Net.SecurityProtocolType]::Tls12)
以下、実際に上記の構文を実行し、設定を確認しています。
PS C:\> [Net.ServicePointManager]::SecurityProtocol = @([Net.SecurityProtocolType]::Tls,[Net.SecurityProtocolType]::Tls11,[Net.SecurityProtocolType]::Tls12) PS C:\> [Net.ServicePointManager]::SecurityProtocol Tls, Tls11, Tls12
以上、PowerShellでTLSのバージョンを指定する方法でした。