Powershellを触ってみた。
でもまずは何をするにもProxyを通さないといけない環境なのでProxyを通すためのメモ。
PowerShell Version
PS > $PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
5 1 14393 4402
Proxyのやり方
を参考に。
PS > $secPasswd=ConvertTo-SecureString <"Password"> -AsPlainText -Force
PS > $myCreds=New-Object System.Management.Automation.PSCredential -ArgumentList <"Username"> ,$secPasswd
Proxyを追加してみたが、このコードの意味がわからない。
と思ったらこれか。
資格情報を作成してるのね。なるほど。
なんかセキュリティ的にはちょっと微妙ではあるんだが、公式サイトに記載されているくらいだから、そこまで変な話ではないな。
で、じゃあCurlを叩いてみると。
PS > curl https://www.yahoo.co.jp -Proxy <ProxyServer> -proxyCredential $myCreds
curl : 要求は中止されました: SSL/TLS のセキュリティで保護されているチャネルを作成できませんでした
発生場所 行:1 文字:1
+ curl https://www.yahoo.co.jp -Proxy <ProxyServer> -proxy ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest]、Web
ption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
こんなエラーが出る。
上記を見ただけなんだが、
PS > [Net.ServicePointManager]::SecurityProtocol
Ssl3, Tls
これは古いね…。なんでWindows10でデフォルトSSL3が有効なんだ…。というわけで上記に記載してあるTLS1.2を有効にしてみる。(yahooだとTLS1.1でも駄目だった)
Yahoo! JAPANが提供するWebAPIをご利用の環境につきましても、「TLS1.2」に対応している必要があります。
「TLS1.2」に対応していない環境からWebAPIを利用されている場合、「TLS1.0」「TLS1.1」のサポート終了後は接続が確立できなくなります。
弊社からのエラーコードも返されません。お使いのシステム側で接続エラーとなります。
PS > [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
PS > [Net.ServicePointManager]::SecurityProtocol
Tls12
で、Curlを叩いてみると…、
PS > curl https://www.yahoo.co.jp -Proxy <ProxyServer> -proxyCredential $myCreds
StatusCode : 200
StatusDescription : OK
Content : <!DOCTYPE html><html lang="ja"><head><meta charSet="utf-8"/><meta http-equiv="X-UA-Compatible" cont
ent="IE=edge,chrome=1"/><title>Yahoo! JAPAN</title><meta name="description" content="あなたの毎日を
アップデートする情...
RawContent : HTTP/1.1 200 OK
Pragma: no-cache
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Vcap-Request-Id: 162cbcd5-260c-4d90-6284-247e5ff5d500
X-Xss-Protection: 1; mo...
(中略)
ほうほう。取れましたね。
でもなんか他の言語と違ってPowerShellは癖があるなあ。
Windowsな感じともいうが、Unix系で生きてきた私にとってはどうにも違和感が(笑)
おまけ
PowerShellはTLS1.3にはまだ対応していないのか!
PS > [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls13
"SecurityProtocol" の設定中に例外が発生しました: "列挙値が無効なため、null を型 "System.Net.SecurityProtocolType" に変
換できません。次のいずれかの列挙値を指定し、再試行してください。有効な列挙値: "SystemDefault,Ssl3,Tls,Tls11,Tls12"。"
発生場所 行:1 文字:1
+ [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.Secu ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting