1

how is it possible to parse yaml file to extract appVersion from it ?

I can read file content with file content to variable to get all content but I can not parse yaml file to extract appVersion from it.

powershell script:

# get chart.yaml file content to yamlfilecontent variable.
write-host($env:yamlfilecontent)

# how to parse content ?
write-host($env:yamlfilecontent["appVersion"]) 

chart.yaml

apiVersion: v2
name: asset-api
description: Helm Chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.5.2

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.0.2"
Metin Bulak
  • 537
  • 4
  • 8
  • 30
  • As an aside: [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`, though that is rarely needed). See also: the bottom section of https://stackoverflow.com/a/50416448/45375 – mklement0 Nov 20 '21 at 23:09
  • As an aside: PowerShell functions, cmdlets, scripts, and external programs must be invoked _like shell commands_ - `foo arg1 arg2` - _not_ like C# methods - `foo('arg1', 'arg2')`. If you use `,` to separate arguments, you'll construct an _array_ that a command sees as a _single argument_. To prevent _accidental_ use of method syntax, use [`Set-StrictMode -Version 2`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-strictmode) or higher, but note its other effects. See [this answer](https://stackoverflow.com/a/65208621/45375) for more information. – mklement0 Nov 20 '21 at 23:09

1 Answers1

1

Unfortunately, as of version 7.2, PowerShell has no built-in support for parsing YAML - see the relevant feature request in GitHub issue #3607.

However, third-party modules are available, such as powershell-yaml, available from the PowerShell Gallery here, from where you can directly deploy it to Azure Automation. To install it locally, use Install-Module -Name powershell-yaml.

Assuming the module is installed, here's how to parse the YAML in your question, using a here-string to define the input text:

$yamlText = @'
apiVersion: v2
name: asset-api
description: Helm Chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
# ...
version: 1.5.2

# This is the version number of the application being deployed. This version number should be
# ...
appVersion: "1.0.2"
'@

($yamlText | ConvertFrom-Yaml).appVersion

The above yields 1.0.2, as intended.

ConvertFrom-Yaml outputs an ordered hashtable that represent the YAML input (technically, an instance of type [System.Collections.Specialized.OrderedDictionary]), whose entries PowerShell allows you to access as if they were properties (e.g. .appVersion) or using index syntax (e.g. ['appVersion']).

mklement0
  • 382,024
  • 64
  • 607
  • 775