# Fonctions partagees par les scripts d'appel HTTP. # PowerShell 5.1 ne remplit pas ErrorDetails sur une erreur HTTP : sans lire le # flux de reponse on n'obtient qu'un « 400 Bad Request » opaque, alors que le # message du serveur est la seule information utile. function Get-HttpErrorBody($err) { if ($err.ErrorDetails.Message) { return $err.ErrorDetails.Message } try { $resp = $err.Exception.Response if ($resp) { $reader = New-Object IO.StreamReader($resp.GetResponseStream()) $body = $reader.ReadToEnd(); $reader.Close() if ($body) { return $body } } } catch { } return $err.Exception.Message } function Read-PlainPassword($prompt) { $secure = Read-Host $prompt -AsSecureString [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)) } # Set-Content -Encoding utf8 ecrit un BOM en PowerShell 5.1, et un BOM dans un # en-tete Authorization invalide le jeton. function Write-Utf8NoBom($path, $text) { [System.IO.File]::WriteAllText($path, $text, (New-Object System.Text.UTF8Encoding($false))) } function Get-Jwt($dir) { $p = Join-Path $dir "jwt.txt" if (-not (Test-Path $p)) { throw "jwt.txt absent. Lancer d'abord 4-get-jwt.ps1" } # [string] est indispensable : la surcharge Replace(char, char) refuse une # chaine vide. Sans le cast, on tombe dessus et l'appel echoue. (Get-Content $p -Raw).Replace([string][char]0xFEFF, '').Trim() }