Issue powershell script in AzureDevops Pipeline

Joseph88 1 Reputation point
2021-10-19T15:58:54.073+00:00

Hello,

I am getting this error when running a powershell task into a pipeline written in JSON format:

The assignment expression is not valid. The input to an assignment operator
must be an object that is able to accept assignments, such as a variable or a
property.

  • CategoryInfo : ParserError: (:) [], ParseException
  • FullyQualifiedErrorId : InvalidLeftHandSide

This is the powershell task:

´´´
{
"task": "PowerShell@2",
"inputs": {
"targetType": 'inline',
"script": '$token = [System.Text.Encoding]::UTF8.GetBytes($env:SonarTokenVar + ":"),
$base64 = [System.Convert]::ToBase64String($token),
$basicAuth = [string]::Format("Basic {0}", $base64),
$headers = @{Authorization = $basicAuth},
$result = Invoke-RestMethod -Method Get -Uri "http://xxx/api/project_badges/measure?project=myrepo&metric=alert_status" -Headers $headers
$result | ConvertTo-Json | Write-Host
if ($result.projectStatus.status -eq "OK")
Write-Host "Quality Gate Succeeded"
else
throw "Quality gate failed"',
}
},

´´´

Any idea how can i fix the code?

thanks in advance.

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,935 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,322 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Monalla-MSFT 10,541 Reputation points
    2021-10-19T18:06:07.03+00:00

    @Joseph88 - Thanks for reaching out.

    Devops is not currently supported here on Microsoft QnA. The Community Members and Engineers are actively answering questions in dedicated forums here. Please post your question in that forum :
    https://developercommunity.visualstudio.com/spaces/21/index.html
    https://azure.microsoft.com/en-in/support/devops/

    Please don't forget to Accept as answer if the reply is helpful.

    0 comments No comments

  2. Joseph88 1 Reputation point
    2021-10-19T18:33:12.473+00:00

    The question was more related to Powershell than AzureDevops.

    I do not think this error is an AzureDevops related issue...

    The assignment expression is not valid. The input to an assignment operator
    must be an object that is able to accept assignments, such as a variable or a
    property.

    • CategoryInfo : ParserError: (:) [], ParseException
    • FullyQualifiedErrorId : InvalidLeftHandSide

  3. Rich Matheisen 44,416 Reputation points
    2021-10-19T19:03:50.65+00:00

    Off the top of my head, I'd guess that the quoting is wrong in your JSON.

    You're using things like "$token, $base64, etc. and those are being seen as variable names in the JSON and are being interpolated. I'd guess that the intention is to pass the "$token" (for example) as a string and not the value of the unassigned variable named $token.

    If I remove the trailing comma from the line '"throw Quality gate failed"' and then feed the JSON into ConvertFrom-JSON I get this:

    PS C:\Users\richm> $y.inputs.script
    
     = [System.Text.Encoding]::UTF8.GetBytes( + ":"),
     = [System.Convert]::ToBase64String(),
     = [string]::Format("Basic {0}", ),
     = @{Authorization = },
     = Invoke-RestMethod -Method Get -Uri "http://xxx/api/project_badges/measure?project=myrepo&metric=alert_status" -Headers
     | ConvertTo-Json | Write-Host
    if (.projectStatus.status -eq "OK")
    Write-Host "Quality Gate Succeeded"
    else
    throw "Quality gate failed"
    

    There are no "left side" to the assignments!

    Try escaping the "$" characters like this "`$token".

    0 comments No comments

  4. Limitless Technology 39,301 Reputation points
    2021-10-20T09:04:49.74+00:00

    Hello Joseph88,

    There is no line and char numbers that could lead you to the point of failure?

    Also in this error normally there are 3 main suspects:

    • A param() is called if a Function is called in the middle of the script. Solution: move the param() it to the top
    • Non-UTF8 character in the script

    Hope this helps with your query,


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments