0

I'm trying to pass a JSON object from a file into a PS script inside an Azure Pipeline.

However, Pipeline kept coming back with the following message (bold formatting is mine). I have been banging my head against this brick wall for a while now with this issue. I have JSON-linted the JSON string and it all passed. Hopefully someone has seen this out there and can provide some pointers. Once the JSON variable was replaced with a String variable, all went well.

C:\Windows\system32\cmd.exe /D /S /C ""C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd" account set --subscription e7c5ee2d-c4a6-4f6c-86e6-09172501d1b3" C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a_temp\azureclitaskscript1661670809684.ps1'" D:\a\1\s\resources\TestScript.ps1 : A positional parameter cannot be found that accepts argument 'accounts: [ { userID: 111111111 } ] }'. At D:\a_temp\azureclitaskscript1661670809684.ps1:3 char:1

  • . 'D:\a\1\s\resources\TestScript.ps1' -usersJson "{ "acco ...
  • + CategoryInfo          : InvalidArgument: (:) [TestScript.ps1], > > > ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PositionalParameterNotFound,TestScript.ps1
    

Pipeline code - test.yml

name: Test
trigger: none

pool:
  vmImage: windows-latest

parameters:
- name: env
  type: string
  values:
  - dev
  default: dev
- name: sub
  type: string
  values:
  - MySC
  default: MySC

stages:
- stage: testing
  displayName: testing
  variables:
    - template: Variables/testingJson.yml
  jobs:
  - deployment: Build
    environment: Dev
    strategy:
      runOnce:
        deploy:
          steps:
            - checkout: self
            - script: |
                echo "userJson - " $(usersJson)
            - task: AzureCLI@2
              inputs:
                azureSubscription: ${{ parameters.sub }}
                scriptType: ps
                scriptLocation: scriptPath
                scriptPath: resources/TestScript.ps1
                arguments: >
                  -usersJson "$(usersJson)"
                failOnStderr: true

Variable file (testingJson.yml) as follows

variables:
  usersJson: '{
                "accounts": [
                  {
                    "userID": "111111111",
                    "UPN": "[email protected]"
                  }
                ]
              }'

While the PS script (TestScript.ps1) is just there to receive the JSON variable

param(
  [Parameter(Mandatory = $true)][string] $usersJson
)

Write-Host "Inside PS"

2 Answers 2

2

I think you call parameters with double quote like this TestScript.ps1 -usersJson "$json" You need to call with: TestScript.ps1 -usersJson '$json'

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, thank you! That works and it keeps the JSON input as JSON object.
0

The way to stop this is to do the following in the PS script - BUT see the accepted answer above for the correct resolution

param(
  [Parameter(Mandatory = $true)][string]$usersJson,
  [Parameter(Mandatory = $true)][string]$usersJson1
)

Write-Host "Inside PS " $usersJson
Write-Host "Inside PS1 " $usersJson1

This will take care of the 2 parameters passed in, and yes, it's one JSON object but somehow was treated as two 'objects'?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.