Comments (3)
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.
from powershell-yaml.
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.
Also, if ordering is important, use the -Ordered
flag when using ConvertFrom-Yaml
.
from powershell-yaml.
Related Issues (20)
- Add `Test-Yaml` cmdlet
- Class with PSCustomObject ConvertTo-Yaml fail HOT 1
- Copyright is out of date HOT 2
- This Module conflicts with Az.Aks HOT 3
- Fails when used from an Azure Function HOT 4
- an object with property name "keys" HOT 3
- `SerializationOptions` should be defined with `[Flags()]`
- Add `-Ordered` parameter info to documentation HOT 1
- Import-Module error HOT 4
- Yaml output is not respected when exporting Powershell nested classes.
- Could not find file '/usr/lib/powershell/ref/System.Private.CoreLib.dll' version 0.4.7 HOT 1
- Convetto-Yaml: Value starting with double quote is being enclosed by single quotes. HOT 5
- mscorlib is not referenced HOT 2
- Cannot find type [YamlDotNet.Core.Parser]
- powershell-yaml module is down HOT 1
- Disable string to date auto conversion HOT 18
- ConvertTo-Yaml multi-line scalar output HOT 2
- Supporting a different schema HOT 3
- Support to PowerShell in Constrained Language Mode HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from powershell-yaml.