To Pass JSON File Arguments to PowerShell File and how to set the JSON File Configuration in YAML file in Azure Devops Pipeline.

Hitesh N 40 Reputation points
2025-05-30T02:46:44.4033333+00:00

How to Pass Arguments of the variables (storageAccountName,location,storageSKU) From the JSON File to the to powershell File , and how to set the JSON File Configuration in YAML file to fetch the json file object in the Azure Devops Pipeline.

Below Example of the JSON , PowerShell and YAML files.

  1. JSON File - test.json

{

storageAccountName = "xxxstoragetest123"

location = useast2

storageSKU = "xyz"
```}

2. PowerShell File - test.ps1 

$Parameters = @{

   storageAccountName = "value from-json-file"

```typescript
location = "value from-json-file"

storageSKU = "value from-json-file"
```}

3. YAML File-  test.yaml

trigger:

- master

pool:

  vmImage: ubuntu-latest

steps:

- task: AzurePowerShell@5

  inputs:

    azureSubscription: 'xxxx'

    ScriptType: 'FilePath'

    ScriptPath: 'test.ps1'

    #scriptArguments: 

    #  -Arg1 Hello `

    #  -Arg2 World

    azurePowerShellVersion: LatestVersion

    pwsh: true 
Azure DevOps
0 comments No comments
{count} votes

Accepted answer
  1. Durga Reshma Malthi 4,430 Reputation points Microsoft External Staff Moderator
    2025-06-19T08:34:25.1333333+00:00

    Hi Hitesh N

    You can absolutely pass $ResourceGroupName and $DeploymentName either from the parameters.json file or from an Azure DevOps variable group depending on how dynamic or reusable you want your pipeline to be.

    Define your variables in the library:

    Go to Azure DevOps -> Select your Project -> Pipelines -> Library -> +New variable group -> Add variables: resourceGroupName and deploymentName -> Save.

    In your YAML pipeline, reference the variable group above the steps section.

    variables:
      - group: <group name>
    

    and Update your PowerShell script to accept parameters in the place of $ResourceGroupName = "xxxx" and $DeploymentName = "Template"

    param (
        [string]$ResourceGroupName,
        [string]$DeploymentName
    )
    
    

    You can also pass from parameters.json but this is not recommended because PowerShell doesn’t auto-wire deployment-level variables.

    Hope this helps!

    Please Let me know if you have any queries.

    If you found the information helpful, please click "Upvote" on the post to let us know and consider accepting the answer as the token of appreciation. Thank You.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Durga Reshma Malthi 4,430 Reputation points Microsoft External Staff Moderator
    2025-05-30T09:10:55.5766667+00:00

    Hi Hitesh N

    To pass arguments from a JSON file to a PowerShell script in an Azure DevOps pipeline, follow these steps:

    1. Ensure your JSON file is correctly formatted:
         {
             "storageAccountName": "xxxstoragetest123",
             "location": "useast2",
             "storageSKU": "xyz"
         }
      
    2. Modify the PowerShell script to read values from the JSON file:
         # Load JSON file
         $jsonFilePath = "$(Build.SourcesDirectory)/test.json"
         $jsonContent = Get-Content -Path $jsonFilePath | ConvertFrom-Json
         # Assign values from JSON to parameters
         $Parameters = @{
             storageAccountName = $jsonContent.storageAccountName
             location = $jsonContent.location
             storageSKU = $jsonContent.storageSKU
         }
         # Output values for debugging
         Write-Host "Storage Account Name: $($Parameters.storageAccountName)"
         Write-Host "Location: $($Parameters.location)"
         Write-Host "Storage SKU: $($Parameters.storageSKU)"
      
      1. Modify your YAML file to include the JSON file and pass it to the PowerShell script:
         trigger:
           - master
         pool:
           vmImage: ubuntu-latest
         steps:
           - task: PowerShell@2
             inputs:
               targetType: 'filePath'
               filePath: '$(Build.SourcesDirectory)/test.ps1'
               arguments: '-jsonFilePath $(Build.SourcesDirectory)/test.json'
               pwsh: true
      
      Alternatively, you can try the below YAML file:
         trigger:
         - master
         pool:
           vmImage: ubuntu-latest
         steps:
         - script: |
             # Load the JSON file into a variable
             $jsonFilePath = "$(Build.SourcesDirectory)/test.json"
             $jsonContent = Get-Content $jsonFilePath -Raw | ConvertFrom-Json
             # Set the PowerShell parameters
             $Parameters = @{
                 storageAccountName = $jsonContent.storageAccountName
                 location = $jsonContent.location
                 storageSKU = $jsonContent.storageSKU
             }
             # Call the PowerShell script with parameters
             .\test.ps1
           displayName: 'Run PowerShell script'
           pwsh: true
      

    Hope this helps!

    Please Let me know if you have any queries.


  2. Durga Reshma Malthi 4,430 Reputation points Microsoft External Staff Moderator
    2025-06-18T08:20:04.8133333+00:00

    Hi Hitesh N

    To reference and pass the parameters from parameters.json in both your YAML and PowerShell files, you can follow the steps below:

    1. In your YAML file, you can reference both the ARM template and the parameters file by specifying the csmParametersFile input in the AzureResourceManagerTemplateDeployment task.
         trigger:
         - azure-pipelines
         pool:
           vmImage: ubuntu-latest
         variables:
           system.debug: true
         steps:
         - task: AzureResourceManagerTemplateDeployment@3
           inputs:
             deploymentScope: 'Resource Group'
             azureResourceManagerConnection: 'xxx'
             subscriptionId: 'xxxx'
             action: 'Create Or Update Resource Group'
             resourceGroupName: 'xxx'
             location: 'East US'
             templateLocation: 'Linked artifact' #or 'filePath'
             csmFile: 'arm/storage.json'
             csmParametersFile: 'arm/parameters.json'
             deploymentMode: 'Incremental'
             deploymentName: 'Template'
      
      Use templateLocation: 'Linked artifact' if the templates are part of your repo (not a URL).
    2. In your PowerShell script, you can read the parameters from the parameters.json file and pass them to the New-AzResourceGroupDeployment cmdlet.
         $ResourceGroupName = "xxx-dev"
         $Location = "eastus"
         $DeploymentName = "Template"
         $TemplateFilePath = "./arm/storage.json"
         $ParameterFilePath = "./arm/parameters.json"
         New-AzResourceGroupDeployment `
           -ResourceGroupName $ResourceGroupName `
           -TemplateFile $TemplateFilePath `
           -TemplateParameterFile $ParameterFilePath `
           -Mode Incremental `
           -Verbose `
           -DeploymentName $DeploymentName
      
      Instead of building a @{} hashtable manually, you can pass the parameters.json directly using the -TemplateParameterFile parameter.

    Hope this helps!

    Please Let me know if you have any queries.

    If you found the information helpful, please click "Upvote" on the post to let us know and consider accepting the answer as the token of appreciation. Thank You.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.