Home > Enterprise >  Azure DevOps pipelines adds whitespace into powershell variables that I cannot remove
Azure DevOps pipelines adds whitespace into powershell variables that I cannot remove

Time:10-29

I have a powershell script I run in our CI/CD pipeline that checks some information through a Invoke-WebRequest call to a service. I receive a JSON back from that service and it parses information from that json.

This is a pseudo representation of that call

$response = Invoke-WebRequest -Uri $uri -Headers @{ Authorization = "Basic "  
[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$accessToken")) } - 
UseBasicParsing

$items= $response | Select-Object -Property Content | Format-Table -HideTableHeaders - 
Wrap | Out-String | ConvertFrom-Json

$items= $items.value

Then later I use a foreach to loop through $items and grab a column for each one. When I run this locally everything happens as expected. When I run this through a pipeline though the column I'm looping through in $items has a random new line in the middle of one of the items causing an error in my script.

Here is my pipeline yaml call to this script

- job: validation
  condition: and(succeeded(), eq({conditional that is expected to pass}))
  steps:
    - powershell: .\script.ps1
      displayName: Validation script
      env:
        SYSTEM_ACCESSTOKEN: $(System.AccessToken)

CodePudding user response:

By using Format-* cmdlets, you ruin the data. These cmdlets are for display purposes only.

Just do

$items = $response.Content | ConvertFrom-Json

CodePudding user response:

The azure agent simply was running Powershell in a smaller prompt than on my laptop. The same issue happened however because my laptop was larger it was less likely to happen. I added the following lines to increase the width of the window on the pipeline so that it did not wrap like it did.

$pshost = Get-Host
$pswindow = $pshost.UI.RawUI

$newSize = $pswindow.BufferSize
$newSize.Width = 5000
$pswindow.BufferSize = $newSize
  • Related