Powershellã®Invoke-RestMethodãhttpsã«å®æ½ããã¨å¤±æãã
Powershellã®Invoke-RestMethodã§htppsã®uriã«RESTãããã¨ãããã¯ã¾ã£ãã®ã§ã¡ã¢ãInvoke-RestMethodèªä½ã¯ããªã便å©ã§ã¡ãã£ã¨æåããã®ã§ãååã¯ãã®ç´¹ä»ã§ãã
Invoke-RestMethod
Powershell3.0ããInvoke-RestMethodã追å ãããWindowsãããLinuxç³»ã§ããCurlã®ãããªãã¨ãã§ããããã«ãªãã¾ãããæè¿Powershellã§Invoke-RestMethodã使ã£ãã¹ã¯ãªãããæ¸ããã®ã§ãããjsonå½¢å¼ã®ãã¼ã¿ãRESTã§åå¾ãã¦ã´ãã§ã´ãã§ããã®ã¯LinuxããCurlãããã¯ããã«ç°¡åã ã¨æãã¾ãã
ä¾ãã°ãRESTã§ä»¥ä¸ã®ãããªjsonå½¢å¼ã®ãã¼ã¿ãåå¾ããå ´åã¯ã
{"name":"John","age":"25"}
ãããªæãã§æ¸ããã¨ãã§ãã¾ãã
>$var = Invoke-RestMethod -Uri http://xxxxxx/api/userlist -Method Get >echo $var.name John
jsonã®ãã¼ã¿ã¯Powershellå´ã§åæã«ãã¼ã¹ãã¦ããã¾ããLinuxããbashã§ãããã¨æãã¨curlã§ã¨ã£ã¦ãããã®ãawkãã¦â¦ã¨ããããªãã¨ã ãã§ãããPowershellãªãä¸è¦ã§ãã
httpsã®URLã«Invoke-RestMethodãããå ´åã®æ³¨æç¹
èªåã¯ããã§çµæ§ã¯ã¾ã£ã¦ãã¾ãã¾ãããåé¡ã®åãåããã§ããªãã¦åæ¥ãããããã£ãã¨æãã¾ãâ¦
å
·ä½çã«ã¯Invoke-RestMethodã®uriãhttpsã§ããã¤ç¸æã®SSLãµã¼ã証ææ¸ãèªå·±è¨¼ææ¸ã®å ´åã«ã¯ä»¥ä¸ã®ãããªã¨ã©ã¼ãèµ·ããã¾ããç§ã®å ´åã¯å
é¨ã®ç®¡çãµã¼ã(èªå·±è¨¼ææ¸)ã«åãã¦Invoke-RestMethodãã¦ãã®ã§ã¨ã©ã¼ã«ãªãã¾ããã
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At line:8 char:11
ã°ã°ã£ã¦ã¿ãã¨ä»¥ä¸ã®ããã«ããã°OK!ã¨çµæ§ãããããªã¨ããã«æ¸ãã¦ããã¾ãããããã ãã§ã¯ãã¡ã§ããã
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
å®éã«ãã®ç¶æ ã§å®è¡ããã¨ä»¥ä¸ã®ããã«ã¨ã©ã¼ã®è¡¨ç¤ºãå¤ããã ãã
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
ããã«ã°ã°ãã¨ãããªãã¼ã¸ãï¼ãã®äººãèªåã¨ã¾ã£ããåãç¶æ³ãããï¼
.net - Powershell v3 Invoke-WebRequest HTTPS error - Stack Overflow
çµå±ã解ã¨ãã¦ã¯ä»¥ä¸ã®ããã§ãã
add-type @" using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; } } "@ [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
httpsã®å ´åã«ã¯SSLãµã¼ã証ææ¸ã®ãã§ãã¯ããã¦ãã¦èªå·±è¨¼ææ¸ã®å ´åã¯å¼¾ãã®ãããã©ã«ãã®ããã§ãã
ä¸ã®ã³ã¼ãã§ã¯CertificatePolicyã®ãCheckValidationResultãã¡ã½ãããä¸æ¸ããã¦ç¡æ¡ä»¶ã«Trueãè¿ãããã«ãã¦ããâ¦ã¨ãã£ãæãã§ãããããä¸è¨ãã¹ã¯ãªããã«è¿½å ããã¨ããhttpsã§ãåé¡ãªãåä½ããããã«ãªãã¾ããã