internal/functions/Invoke-TlsRestMethod.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Invoke-TlsRestMethod {
    <#
    Internal utility that mimics invoke-RestMethod
    but enables all tls available version
    rather than the default, which on a lot
    of standard installations is just TLS 1.0
    #>

    $currentVersionTls = [Net.ServicePointManager]::SecurityProtocol
    $currentSupportableTls = [Math]::Max($currentVersionTls.value__, [Net.SecurityProtocolType]::Tls.value__)
    $availableTls = [enum]::GetValues('Net.SecurityProtocolType') | Where-Object { $_ -gt $currentSupportableTls }
    $availableTls | ForEach-Object {
        [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor $_
    }

    Invoke-RestMethod @Args

    [Net.ServicePointManager]::SecurityProtocol = $currentVersionTls
}