0

enter image description here

Above is variables.stg.yml

I am trying to read it in my Powershell code. I used https://www.powershellgallery.com/packages/powershell-yaml/0.4.2 for this

$os_list = (Get-Content -Raw -Path ..\variables.stg.yml| ConvertFrom-Yaml) 
$json = $os_list | ConvertTo-Json
Write-Host $json 
#Convert JSON file to an object
$JsonParameters = ConvertFrom-Json -InputObject $json
Write-Host $JsonParameters
#Create new PSObject with no properties
$oData = New-Object PSObject
#Loop through properties of the $JsonParameters.parameters object, and add them to the new blank object
$JsonParameters.parameters.psobject.Properties.Name | 
ForEach{ 
    Add-Member -InputObject $oData -NotePropertyName $_ -NotePropertyValue 
$JsonParameters.parameters.$_.Value 
}

Write-Host $oData

However what i see is :

enter image description here

knowdotnet
  • 839
  • 1
  • 15
  • 29
  • Why are you converting the data to json and back again? FWIW, the error seems to be cause by referencing `$JsonParameters.parameters` instead of `$JsonParameters.variables` – Mathias R. Jessen May 12 '22 at 15:42
  • Write-Host $JsonParameters is printing @{variables=} – knowdotnet May 12 '22 at 15:56
  • 1
    Exactly, so you want to do `$JsonParameters.variables.psobject.Properties.Name | ForEach-Object { ...` - not `$JsonParameters.parameters.psobject.Properties.Name | ForEach-Object { ...` (or perhaps just `$oData = $JsonParameters.parameters` if you're not going to use/modify `$JsonParameters` any further anyway) – Mathias R. Jessen May 12 '22 at 16:07
  • Its slightly unclear what you are aiming to do. The idea proposed by Mathias is what you're looking for - assuming you know that "variables" is the parent level item in the yaml, always. I see that the initial yaml import is coming in as a hashtable - I assume that's why you're converting to json - but you can also just create your psobject there: [PSCustomObject]$os_list – omrsafetyo May 12 '22 at 16:12
  • 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, or 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`); see [this answer](https://stackoverflow.com/a/60534138/45375). To explicitly print only to the display _but with rich formatting_, use `Out-Host`. – mklement0 May 12 '22 at 17:00
  • Have you tried making `$oData = New-Object PSObject` a `PSCustomObject` ? – jack_skellington May 12 '22 at 16:08

2 Answers2

2

The immediate problem with your code is that you're referencing $JsonParameter.parameters when you really want $JsonParameters.variables - the property name in the yaml file is variables, not parameters.

A less cumbersome way to obtain an object with the ABC and Test entries from the yaml file as properties would be to simply cast the hashtable generated by ConvertTo-Yaml to a [PSCustomObject]:

$documentWithVariables = Get-Content -Path ..\variables.stg.yml -Raw |ConvertFrom-Yaml 
$oData = [PSCustomObject]$documentWithVariables.variables

Much simpler :)

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

It seems like you're trying to use Convertto-YAML to converted to serialized JSON.

Accordding to their documentation you need to use the jsoncompatible flag to do this.

Converting from YAML to JSON

The awesome YamlDotNet assembly allows us to serialize an object in a JSON compatible way. Unfortunately it does not support indentation. Here is a simple example:

Import-Module powershell-yaml

PS C:\> $yaml = @"
anArray:
- 1
- 2
- 3
nested:
  array:
  - this
  - is
  - an
  - array
hello: world
"@

PS C:\> $obj = ConvertFrom-Yaml $yaml
PS C:\> $obj

Name                           Value
----                           -----
anArray                        {1, 2, 3}
nested                         {array}
hello                          world

PS C:\> ConvertTo-Yaml -JsonCompatible $obj
{"anArray": [1, 2, 3], "nested": {"array": ["this", "is", "an", "array"]}, "hello": "world"}

# Or you could do it in one line.
PS C:\> ConvertFrom-Yaml $yaml | ConvertTo-Yaml -JsonCompatible
{"anArray": [1, 2, 3], "nested": {"array": ["this", "is", "an", "array"]}, "hello": "world"}

Your array is also not formatted correctly to be imported as a nested. Below is correct syntax:

variables:
 ABC: 
  XYz
 Test: 
  preprod01

Finally:

[pscustomobject]$os_list = (ConvertFrom-Yaml -yaml (get-content -Raw C:\Powershell\TestCSVs\variables.stg.yml))
[pscustomobject]$os_list = ConvertTo-Yaml $os_list -JsonCompatible
$os_list

$oData = $os_list.variables
$oData


PS:> {"variables": {"ABC": "XYz", "Test": "preprod01"}}