Giter Club home page Giter Club logo

Comments (3)

Guillem2607 avatar Guillem2607 commented on August 16, 2024

So 20 minutes after I posted this issue I have thrown a triple chasing some solution and I found it, I get the file content at the same time as I convert the variable from yaml to a hashtable.

solution

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 16, 2024

A bit of context to what happened in the first one. When you use Get-Content without the -Raw parameter, you get an array of objects. Essentially, an array of lines inside the yaml file.

When you use the -Raw flag, all the contents of the file is read at once as a raw string.

The powershell-yaml module uses "Advanced functions" which allows us to specify a BEGIN, PROCESS and END blocks. We use this to read a stream sent through a pipe and in the END block we process all input as a single yaml. This is why piping the contents of Get-Content works, but not sending it as a parameter.

To illustrate, we'll use the following yaml:

hi: there
goodbye: now

Using Get-Content without -Raw:

$contents = Get-Content /tmp/test.yaml
PS /tmp> $contents                             
hi: there
goodbye: now

Let's see what we got:

PS /tmp> $contents.GetType().FullName
System.Object[]

As we can see, this is an array of objects, not a string. If we try to pass it as a positional parameter, ConvertFrom-Yaml will try to convert from string to powershell objects, so we'll get an error:

PS /tmp> ConvertFrom-Yaml $contents
ConvertFrom-Yaml: Cannot process argument transformation on parameter 'Yaml'. Cannot convert
value to type System.String.

But piping the same object, will work due to how the BEGIN, PROCESS, END workflow works:

PS /tmp> $contents | ConvertFrom-yaml          

Name                           Value
----                           -----
hi                             there
goodbye                        now

To save yourself the headache, simply use the -Raw flag when you use Get-Content:

PS /tmp> $contents = Get-Content -Raw /tmp/test.yaml
PS /tmp> $contents.GetType().FullName
System.String
PS /tmp> ConvertFrom-Yaml $contents

Name                           Value
----                           -----
hi                             there
goodbye                        now

PS /tmp> $contents | ConvertFrom-Yaml

Name                           Value
----                           -----
hi                             there
goodbye                        now

from powershell-yaml.

gabriel-samfira avatar gabriel-samfira commented on August 16, 2024

Also, if ordering is important, use the -Ordered flag when using ConvertFrom-Yaml.

from powershell-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.