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"