Azure Devops: Invoke-RestMethod : Response suppressed in console but stored in variable

Viewed 41

As part of Azure devops pipeline , I need hit the API to get the Oauth token and use it in the subsequent steps, I was able to achieve this, the only problem is the Oauth is displayed in the devops console, which I want to suppress it, is there a way I can suppress the output or pipe the Invoke-RestMethod to variable? I tried the | Out-Null but this also did not store the response in the variable.

Thanks in advance. cheers.

$response = Invoke-RestMethod 'https://login.microsoftonline.com/xyz/oauth2/v2.0/token' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json
$token=$response.access_token
1 Answers

Oops I missed the not, it didn't not store the variable. I have found another way to do this without writing to console.

Invoke-RestMethod 'https://login.microsoftonline.com/xyz/oauth2/v2.0/token' -Method 'POST' -Headers $headers -Body $body | ForEach-Object {
                      $token=$_.access_token
                      Write-Host "##vso[task.setvariable variable=bearerToken;issecret=true]$token"
}
Related