image Forbidden access to a socket on Windows and how to solve it image Running development tools and applications in Docker on Windows 10

Modify YML files with PowerShell

In this short post, I’ll share a simple script to modify the YML files. You can also use the script in the CI/CD pipelines, an example for Azure DevOps is below.

To read the YML file we will use powershell-yaml module, which you can install and active like this:

Here is our sample YML file that we will use in our little demo:

And here is a PowerShell script, that will modify the above YML file. It contains some comments, so you can read through.

Save both files to the same folder. Now when you run .\script.ps1 -Param2 "Param value" -FooterBody "Footer value" your sample YML file is modified:

Now let’s include the process above in the Azure DevOps pipeline. I want to set the Param2 and FooterBody as variables and then pass them to the script. I will not go into detail about how to set the whole pipeline, but I’ll show you the important bits. To continue, save the script above in the code repository so you will be able to reference it from PowerShell task.

Step 1: Set the pipeline variables

Figure 1: Set variables for a pipeline in Azure DevOps

Step 2: Add PowerShell task to the pipeline

Figure 2: Add PowerShell task to the pipeline in Azure DevOps

Step 3: Set PowerShell script task

Now click on the PowerShell task and set it up.

  • Set Type to File Path.
  • Set Script Path to the script in your code repository.
  • In the Arguments field type -Param2 $(Param2) -FooterBody $(FooterBody) which will inject the pipeline variables to the script.
  • Depending on where your YML files are in the code repository structure, you also need to specify the context or Working directory for the script.
Figure 3: PowerShell task example.

And that’s it.

I know, I know, the script can be more general and dynamic. For example, we could specify a YML file full path as the argument for the script, but for the sake of this demonstration that is fine. Leave your comments down below :).

TA-DA!

About Miha
I've always been fascinated by computers. I am happy that I can work in this creative field of software development, where I can solve problems. I am also an avid cyclist :) and I love to learn...a lot!
Related Posts
  • All
  • By Author
  • By Category
  • By Tag

Leave a Reply