Search code examples
powershellazure-devopsyamlazure-pipelines-yamlpowershell-7.0

how to pass mutiple parameters from YAML to Powershell Function


I have to invoke POST METHOD from powershell function which is having 8-9 parameters and these parameter may contain big strings.

I am calling powershell function from Azure DevOps Yaml based pipelines and We have a configuration YAML file which contains variables which need to be passed to POWERSHELL FUNCTION.

how to read these mulitple parameters from YAML file and pass it powershell script from YAML Based pipeline?Any relevant links or snippet will help.

I am very new to Powershell so I am sorry if question has not been put in correct way.


Solution

  • You need to install powershell-yaml module for that. Here is an example:

    if (-not (Get-Module -ListAvailable -Name powershell-yaml)) {
        Install-Module powershell-yaml -Force
    }
    
    $yamlData = ConvertFrom-Yaml (Get-Content -Path .\data.yaml -Raw)
    

    $yamlData is hashtable, so you can retrieve any object property from it.

    For instance, if you have such content in yaml file:

    name: DataObject
    traits:
      - Green
      - Good
      - Round
    
    value:
      dataArray:
        - FirstsPartOfData
        - SecondPartOfData
    

    After reading you can access each property and array like this:

    $yamlData.name
    $yamlData.traits
    $yamlData.value.dataArray