question

Joseph88-3047 avatar image
0 Votes"
Joseph88-3047 asked LimitlessTechnology-2700 answered

Issue powershell script in AzureDevops Pipeline

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-serverwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Monalla-MSFT avatar image
0 Votes"
Monalla-MSFT answered

@Joseph88-3047 - 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Joseph88-3047 avatar image
0 Votes"
Joseph88-3047 answered Monalla-MSFT commented

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Joseph88-3047 - Sure, thanks to clarify that. Let me look into this and will get back to you as soon as I have an update.

Thanks for understanding.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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".

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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--

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.