Скачать файл из Интернет
Method 1a, by default synchronous**
Invoke-WebRequest $url -OutFile $path_to_file
Если возникнет ошибка "…Could not create SSL/TLS secure channel." То вести комманду
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Или один раз выполнить и больше ошибки не будет
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Method 1b (for JSON/XML data), by default synchronous**
Invoke-RestMethod $url -OutFile $path_to_file
This method is a wrapper around Invoke-WebRequest, but provides some additional formatting of JSON results. (See documentation here and here.)
Method 2, by default synchronous**
(New-Object System.Net.WebClient).DownloadFile($url, $path_to_file)
Method 3, asynchronous and may be much slower than the other two but is very gentle on bandwidth usage (it uses the BITS service).
Import-Module BitsTransfer Start-BitsTransfer -Source $url -Destination $path_to_file
Скачать сертификат с сервера в Интернет
$webRequest = [Net.WebRequest]::Create("https://www.myserver.ru/") [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11 [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $webRequest.Proxy = $null $webRequest.GetResponse() $cert = $webRequest.ServicePoint.Certificate $bytes = $cert.Export([Security.Cryptography.X509Certificates.X509ContentType]::Cert) set-content -value $bytes -encoding byte -path "$pwd\mycert.cer"
Ошибка Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
При использовании Invoke-RestMethod или Invoke-WebRequest с https на URL сервера, где установлен самоподписанный сертификат, возникали ошибки:
- The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
- The underlying connection was closed: An unexpected error occurred on a send.
Получилась аж вот такая конструкция, но я нашёл свойство, отключающее проверку сертификата (выделил ):
$URi = "https://<URL>" $request = [System.Net.WebRequest]::Create($URi) $data = (New-Object PSObject | Add-Member -PassThru NoteProperty user "<USER>" | Add-Member -PassThru NoteProperty password "<PASSWORD>" ) | ConvertTo-JSON $request.KeepAlive = $false $request.Headers.Add($HeadersCollection) $request.ContentType = "application/json" $request.Accept = "application/json" $request.Method = "POST" $request.ServerCertificateValidationCallback = {$true} $bytes = [System.Text.Encoding]::ASCII.GetBytes($data) $request.ContentLength = $bytes.Length $requestStream = [System.IO.Stream]$request.GetRequestStream() $requestStream.write($bytes, 0, $bytes.Length) $requestStream.Close() $response = $request.GetResponse() $response.GetResponseStream()
И так как свойство было найдено, то решил попробовать пойти по классике, и получилось это:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$false} $data = @{"user" = "<USER>";"password" = "<PASSWORD>"} | ConvertTo-JSON $URi = "https://<URL>" Invoke-RestMethod -Method Post -Uri $URi -Body $data -ContentType "application/json"