Giter Club home page Giter Club logo

powershell-yaml's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

powershell-yaml's Issues

Different behavior as ConvertFrom-Json with Get-Content

When I run the command Get-Content test.json | ConvertFrom-Json | ConvertTo-Yaml I got the representing Yaml which works.

When I run the command Get-Content -Raw test.yaml | ConvertFrom-Yaml | ConvertTo-Yaml everything works as expected.

When I run the command Get-Content test.yaml | ConvertFrom-Yaml | ConvertTo-Yaml I got an array with each property as object.

Example:

test.yaml

id: m1
isRoot: true
delay: 0
text: Test

Output:

- id: m1
- isRoot: true
- delay: 0
- text: Test

I think this is confusing because I expect that all converters should work the same way
I know that Get-Content parse each line, but this should recognized automatically like on ConvertFrom-Json.

Tested with Powershell 5.1 on Windows and Powershell Core 6.2 on Windows and MacOS

ConvertTo-Yaml is converting `|` blocks into `>`

When doing something like Get-Content example.yaml -Raw -Encoding UTF8 | ConvertFrom-Yaml -Ordered | ConvertTo-Yaml you will see that | blocks are converted into > with blank spaces

Could I somehow force / set options to keep | type?

Input file:

version: 2.4.37
description: Apache web server.
homepage: https://www.apachelounge.com
persist:
  - htdocs
  - conf
bin:
  - bin\ab.exe
pre_install: |
  Write-Host 'Installing'
  Write-Host 'Installing'
post_install: |
  # set directory in httpd.conf
  $conf = "$dir\conf\httpd.conf"
  $root = (scoop which httpd | Split-Path -Resolve -Parent | Split-Path -Parent) -replace '\\', '/'
  (Get-Content $conf) -replace 'c:/Apache\d+', "$root" | Set-Content $conf
checkver:
  url: https://www.apachelounge.com/download/
  regex: Apache ([\d\.]+) Win64

Output:

....
pre_install: >
  Write-Host 'Installing'

  Write-Host 'Installing'
post_install: >
  # set directory in httpd.conf

  $conf = "$dir\conf\httpd.conf"

  $root = (scoop which httpd | Split-Path -Resolve -Parent | Split-Path -Parent) -replace '\\', '/'

  (Get-Content $conf) -replace 'c:/Apache\d+', "$root" | Set-Content $conf

Only Hashtables supported?

It looks like in the help that the input object and/or the Data parameter take System.Object type but I can only seem to get ConvertTo-Yaml to work with basic hashtables.

$srv = Get-Service -Name BITS
ConvertTo-Yaml -Data $srv

Fails

$srv = Get-Service -Name BITS | Select-Object Name,Status,DisplayName
ConvertTo-Yaml -Data $srv

Fails, even as simpler object

$custom = [PSCustomObject]@{'Color'='Blue'; 'Day'='Monday'; Number=4}
ConvertTo-Yaml -Data $custom 

Fails

Only the following succeeds:

$hashtable = @{'Color'='Blue'; 'Day'='Monday'; Number=4}
ConvertTo-Yaml -Data $hashtable

Am I misunderstanding a limitation or is there any known best practices for coercing PowerShell objects into a type compatible with powershell-yaml?

Thanks!

Replace Confirm-Equality with a tested solution

Confirm-Equality is not a good basis for your tests. This is not your fault, it's just that comparing artbitrary values in PowerShell is complicated. ๐Ÿ™‚ There are no tests for it and it's easy to trick it into being wrong. Here is one example that returns $true:

Confirm-Equality -got 1 -expected 1,2,3

PowerShell -eq does not work the same way == in C# does, and so the first if in Compare-Equality gives you unexpected result.

I wrote a module that compares objects by structure and that has 600+ of tests. By default it does equivalency checks (less strict), but can be configured to do equality (more strict) as well.

Would you accept PR that would move the tests to using Assert?

ConvertTo-Yaml - PSCustomObject with $null properties

Steps to reproduce

$CustomObject = [pscustomobject]@{firstname="John"; url=$null; comment="foo bar"}
ConvertTo-Yaml $CustomObject

Outcome

Convert-PSObjectToGenericObject : Cannot bind argument to parameter 'Data' because it is null.

Workaround

I'm currently using a modified version of Convert-PSCustomObjectToDictionary to accommodate members with a value of $null:

function Convert-PSCustomObjectToDictionary {
    Param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [PSCustomObject]$Data
    )
    $ret = [System.Collections.Generic.Dictionary[string,object]](New-Object 'System.Collections.Generic.Dictionary[string,object]')
    foreach ($i in $Data.psobject.properties) {
		If ($i.Value) {
			$ret[$i.Name] = Convert-PSObjectToGenericObject $i.Value
		}
		Else {
			$ret[$i.Name] = ""
		}
    }
    return $ret
}

Environment details

  • powershell-yaml version: 0.3.1
  • PowerShell version:
$psversiontable

Name                           Value
----                           -----
PSVersion                      5.1.16299.98
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.98
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Regional settings influence conversion

> 'pi: 3.14' | ConvertFrom-Yaml

Name                           Value
----                           -----
pi                             314

> 'pi: 3,14' | ConvertFrom-Yaml

Name                           Value
----                           -----
pi                             3,14


> [System.Globalization.CultureInfo]::CurrentCulture.NumberFormat | % NumberDecimalSeparator    
, 

ConvertTo/From-YAML: Type is not preserved

  • PSVersion 5.1.16299.251
  • PSEdition Desktop
  • PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
  • BuildVersion 10.0.16299.251
  • CLRVersion 4.0.30319.42000
  • WSManStackVersion 3.0
  • PSRemotingProtocolVersion 2.3
  • SerializationVersion 1.1.0.1
  • powershell-yaml v0.3.2

Converting to YAML and back to an object doesn't return the original object:

PS > ConvertTo-Yaml @{a=1;b=2}
a: 1
b: 2
PS > $foo = ConvertTo-Yaml @{a=1;b=2} | ConvertFrom-Yaml
PS > $foo.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Error importing powershell-yaml in strict mode on Windows 2012

I get this error when importing powershell-yaml on Windows 2012:

The property 'NanoServer' cannot be found on this object. Verify that the property exists.
At D:\Build\Arc\powershell-yaml\Load-Assemblies.ps1:41 char:16
+         return ($serverLevels.NanoServer -eq 1)
+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at IsNanoServer<Process>, D:\Build\********\powershell-yaml\Load-Assemblies.ps1: line 41
at Initialize-Assemblies, D:\Build\********\powershell-yaml\Load-Assemblies.ps1: line 46
at <ScriptBlock>, D:\Build\********\powershell-yaml\Load-Assemblies.ps1: line 66
at <ScriptBlock>, <No file>: line 1
    + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

I think this happens because all our code runs in strict mode and on Windows 2012 the HKLM:Software\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels registry key doesn't have the NanoServer value.

My fix is to check for the property. I changed this:

        return ($serverLevels.NanoServer -eq 1)

to

        return (($serverLevels | Get-Member 'NanoServer') -and $serverLevels.NanoServer -eq 1)

Publish new version?

Hiyo!

Any chance you could publish a new version to the gallery? This line in particular would help avoid a bug in a tool I'm using (i.e. previous behavior using psd1 ScriptsToProcess runs in a useless scope given the bug - loading in psm1 will load them in the right scope)

Cheers!

any plan to support multiline string value?

example:

      cosmosdb:
        dbName: "product-catalog"
        collectionName: "products"
        query: -|
          select
          c.id,
          c.documentType,
          c.artifactType,
          c.artifactId,
          c.nodeId,
          c.createdWhen,
          c.createdBy,
          c.modifiedWhen,
          c.modifiedBy from c where c.documentType="NodeMapping"

Breaking change from 0.3.1 to 0.3.2 - can't serialize collection created via pipeline/write-output

Hi @gabriel-samfira,

I love this module, although I found a breaking change since 0.3.2 which can't serialise the collection returned from the pipeline (unless it's streamed to Convertto-Yaml).

Example:
This works in 0.3.1 but not in 0.3.2

Convertto-yaml -Data (Write-output 'a','b','c')

This works on both:

Convertto-yaml -Data (Write-output -NoEnumerate 'a','b','c')
(Write-output -NoEnumerate 'a','b','c') | Convertto-yaml

Any chance for a fix?

ConvertFrom-Yaml fails when reading yaml containing multiple documents

when I execute below
ConvertFrom-Yaml -Yaml $(Get-Content "E:\file.yaml") -AllDocuments

I get below error:

ConvertFrom-Yaml : Cannot process argument transformation on parameter 'Yaml'. Cannot convert value to type System.String.
At line:1 char:29
+ ... tFrom-Yaml -Yaml $(Get-Content "E:\file.yaml") -AllDoc ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [ConvertFrom-Yaml], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,ConvertFrom-Yaml

Here is my yaml file.

---
# Source: A/templates/A-poddisruption.yaml
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: A
  namespace: A
  labels:
    app: A
    tier: A
spec:
  maxUnavailable: 20%
  selector:
    matchLabels: 
      app: A
---
# Source: A/templates/B-loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: B
  namespace:A
  labels:
    app: A
    tier: A
spec:
  type: LoadBalancer
  externalTrafficPolicy: Local
  selector:
    app: A
    tier: A
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  - name: https
    protocol: TCP
    port: 443
    targetPort: 443``

$Error is populated with useless error, when importing module

Importing the module generates a useless error, because the can serialize and deserialize YAML just fine, regardless of this error.
This makes it difficult to detect whether my powershell script generated any errors, because this error will always appear, making it seem that there is always at least 1 error:

C:\Git> $Error                                                                                                                    
C:\Git> Import-Module powershell-yaml                                                                                             
C:\Git> $Error                                                                                                                    
Unable to find type [YamlDotNet.Serialization.Serializer].                                                                        
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.4.0\Load-Assemblies.ps1:27 char:9                                 
+         [YamlDotNet.Serialization.Serializer] | Out-Null                                                                        
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                   
    + CategoryInfo          : InvalidOperation: (YamlDotNet.Serialization.Serializer:TypeName) [], RuntimeException               
    + FullyQualifiedErrorId : TypeNotFound                                                                                        ```

Block scalars are not parsed as array

Scalar blocks should be parsed as array.

# test yaml for desription
installer_block: |
  Get-Content 'Alfa.txt'
  Write-Host 'Test' -f Yellow
installer_arr:
  - Get-Content 'Alfa.txt'
  - Write-Host 'Test' -f Yellow
$yaml = Get-Content -Raw .\test.yml | ConvertFrom-Yaml
$yaml.installer_arr.Count
2
$yaml.installer_block.Count
1 # Should be 2

Single Value Array

Somehow the ConvertTo-Yaml converts an array attribute into string if the array only contains one value. Conversion to yaml array works fine for multiple values.

Sorting imported YAML breaks ConvertTo-YAML

I want to import from an existing YAML file, modify, and then write a new/replacement YAML file, ideally in the same order / layout as it was imported. My use/test case is on PowerShell 7 (Core), running on MacOS. So far, all other expected functionality of powershell-yaml is excellent.

Currently, I can import (ConvertFrom-YAML), but it doesn't seem the -Ordered parameter works to alphabetically order the Keys/Names.

If I sort the imported object, and then use ConvertTo-Yaml, the results are NOT valid YAML.
Example as PowerShell object:

----                           -----
Name                           Value
autoscaling                    {maxReplicas, enable, minReplicas}
deployment                     {replicaCount}
inboundTraffic                 {cname}
initContainers                 {rectify}

Example after ConvertTo-Yaml -Data ($values | sort):

- ""
- ""
- '----                           -----'
- Name                           Value
- autoscaling                    {maxReplicas, enable, minReplicas}
- deployment                     {replicaCount}
- inboundTraffic                 {cname}
- initContainers                 {rectify}

Aliases?

What about adding aliases for exported commands? Something like cfy, cty?

New Version

Hey!

I would really like to see the latest changes in a new version of powershell-yaml on PowerShellGallery and GitHub releases.
When do you think are you going to release the next version?

Assemblies are not loaded when imported with Import-Module -Version

Because of PowerShell/PowerShell#3738, ScriptsToProcess in the PSD1 are not run when imported via Import-Module powershell-yaml -Version '0.3.1. This means Load-Assemblies.ps1 is not run. Even though this is a powershell bug, it might be worthwhile to work around it by dot-sourcing Load-Assemblies.ps1 from powershell-yaml.psm1.

Environment Info

Powershell Version Table:

Name                           Value
----                           -----
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

powershell-yaml Version: 0.3.1
OS version: Windows 8.1

Steps to reproduce

PS C:\Windows\system32> Import-Module -Version 0.3.1 powershell-yaml
PS C:\Windows\system32> ConvertFrom-Yaml 'hi: hi'
New-Object : Cannot find type [YamlDotNet.RepresentationModel.YamlStream]: verify that the assembly containing this type is loaded.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:24 char:23
+ ...  $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:25 char:9
+         $yamlStream.Load([System.IO.TextReader] $stringReader)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Nested Hashtable Error, but not on JSONCompatible

I have a hash table who's value is another hash table. The deserializer seems to produce different results if you serialize it normally, vs JSON compatible serialization. JSON compatibility seems to be the accurate mode.

get-installedmodule *yaml* |select *|fl
$hash = @{x=@{y=1;z=2}}
write-output "input hash inside hash"
$hash

$hash|convertto-yaml -OutFile '.\yaml.yaml' -Force
$yaml = gc .\yaml.yaml 
write-output "converting to yaml in file, getting valid(?) yaml"
$yaml

$results = $yaml|convertFrom-Yaml
write-output "The deserializer doesn't produce the intended output"
 $results

 $hash|convertto-yaml -OutFile '.\yamljson.yaml' -Force -JsonCompatible
 $yamljson = gc .\yamljson.yaml 
 write-output "converting to yaml json in file, getting valid(?) yaml json"
 $yamljson
 write-output "Deserializer seems ok, so probably, yeah"
 $resultsjson = $yamljson|convertFrom-Yaml
 $resultsjson

Fun sidenote, if you use anything to prettify yamljason.yaml, you'll get invalid yaml, which I suppose is consistent with the white-space requirements of yaml, but ruins the human-editability of the JSON file.

Problem with string example formatted as timestamp even in quotes

I have an API schema with a property in an OpenApiSpec YAML file formatted as:

        workRegisteredDate:
          type: string
          format: date
          example: 1991-01-07

When I use "ConvertFrom-Yaml" and "ConvertTo-Yaml" in my own script to update the API Spec Title and Server Url the output file then has this:

        workRegisteredDate:
          type: string
          format: date
          example: 1991-01-07T00:00:00.0000000

Obviously I don't want this example to be re-written as a date time stamp. I cloned @gabriel-samfira fork to test out the -Raw option, but this didn't help me. I then looked at powershell-yaml.psm1 file and found I could avoid this even without adding single or double quotes by altering the regex in the function "Convert-ValueToProperType" at around line 106 from:

        $regex = @'
[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] # (ymd)
|[0-9][0-9][0-9][0-9] # (year)
 -[0-9][0-9]? # (month)
 -[0-9][0-9]? # (day)
 ([Tt]|[ \t]+)[0-9][0-9]? # (hour)
 :[0-9][0-9] # (minute)
 :[0-9][0-9] # (second)
 (\.[0-9]*)? # (fraction)
 (([ \t]*)Z|[-+][0-9][0-9]?(:[0-9][0-9])?)? # (time zone)
'@

to:

        $regex = @'
[^"][0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] # (ymd)
|[0-9][0-9][0-9][0-9] # (year)
 -[0-9][0-9]? # (month)
 -[0-9][0-9]? # (day)
 ([Tt]|[ \t]+)[0-9][0-9]? # (hour)
 :[0-9][0-9] # (minute)
 :[0-9][0-9] # (second)
 (\.[0-9]*)? # (fraction)
 (([ \t]*)Z|[-+][0-9][0-9]?(:[0-9][0-9])?)? # (time zone)
'@

Adding [^"] seems to stop the date string from being converted to a date time stamp, however I'm not sure if this would break anything for anyone else.

Is there another way to avoid my example from being clobbered without altering the code?

Failed to serialize a string obtained with Get-Date -Format

[1] Running

ConvertTo-Yaml -Data ([PSCustomObject]@{timestamp = [String](Get-Date -Format 'yyyy-MM-dd')})

Outputs
timestamp: 2020-02-10

[2] Running

ConvertTo-Yaml -Data ([PSCustomObject]@{timestamp = (Get-Date -Format 'yyyy-MM-dd').Clone()})

Outputs
timestamp: 2020-02-10

[3] Running

ConvertTo-Yaml -Data ([PSCustomObject]@{timestamp = (Get-Date -Format 'yyyy-MM-dd')})

Outputs (the message is truncated)

Exception calling "Serialize" with "2" argument(s): "Too much recursion when traversing the object graph"
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.4.1\powershell-yaml.psm1:383 char:13
+             $serializer.Serialize($wrt, $norm)
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : InvalidOperationException

timestamp: &o0
  BaseObject: *o0
  Members:
  - IsGettable: true
    OverloadDefinitions:
    - char Chars(int index) {get;}
    TypeNameOfValue: System.Char
    MemberType: ParameterizedProperty
    Value:
      IsGettable: true
      OverloadDefinitions:
      - char Chars(int index) {get;}
      TypeNameOfValue: System.Char
      MemberType: ParameterizedProperty

Update YamlDotNet.dll

There have been quite some releases of YamlDotNet. Can you update the DLL, please? There are no specific issues with the current one but there has been quite some bug fixing in the repo and also some interesting new features like .net Core support.

Why casting version number to a date

Hi there,
Could you please help me solve a problem with automatic casting version number to a date format?

I have yaml file and I read it using your library.

Now here is example of yaml file (properly formatted):
#Software Config
service:
version: 7.9.67-1
developer:
licence: test

And now after loading - this file version is cast to a date format.
7.9.67-1 is now 09/07/1967 02:00:00
(Also hour is strange, should be 01 but is 02 --> when the second number is bigger than 3 then hour is +1)

When number doesn't match to a date format then version is fine eg: 57.23.3-1

Did you face this problem?

How to avid casting?
I tried with "", '' and it doesn't help.

Merge Key Language-Independent Type for YAML Support

Does this project support the Merge Key Language-Independent Type for YAML Version 1.1?

I'm encountering the same behavior in powershell-yaml as the one reported and fixed in the lyaml library described here: YAML merge key not working

Within the learnxinyminutes.yaml example, access to the bar.name property seems to work only via:

$yaml.bar.'<<'.name

Is this the intended model?

Ordered hashtable

It should be possible to support [ordered] HashTable so that keys appear in the same order as in yaml file.

Tags Support?

Hello is there tags support, and if not can it be added? YamlDotNet supports tags.

Multiline String Support

Hello, I saw another issue open and then closed by the person that opened it regarding this with no other comments.

Current state:

something:
  name: test
  key:
  - '-----BEGIN PGP MESSAGE-----'
  - 'Version: GnuPG v2.0.22 (GNU/Linux)'
  - 'blah'
  - 'blah'
  - 'blah'
  - '-----END PGP MESSAGE-----'

Desired state:

something:
  name: test
  key: |
    -----BEGIN PGP MESSAGE-----
    Version: GnuPG v2.0.22 (GNU/Linux)
    blah
    blah
    blah
    -----END PGP MESSAGE-----

Apologies if it is something I am doing, I tried various options but couldn't seem to get it to handle the multiline string.

Module does not load on PowerShell Core/Linux

Note that it does load on PSCore/Windows

Testing using docker image microsoft/powershell image in Docker for Windows with powershell-yaml 0.4.0

PS /usr/local/src> import-module powershell-yaml -force
Add-Type : (7,36): error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
public class StringQuotingEmitter: ChainedEventEmitter {
                                   ^
At /root/.local/share/powershell/Modules/powershell-yaml/0.4.0/powershell-yaml.psm1:297 char:5
+     Add-Type -TypeDefinition $stringQuotingEmitterSource -ReferencedA ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: ((7,36): error CS001\u2026=b77a5c561934e089'.:CSDiagnostic) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

..and a couple of hundred more lines, mostly complaining about the same missing reference.

Whether this is an issue with the Add-Type or is an underlying issue in the .net standard build of YamlDotNet, I don't know at this time.

Error on PowerShell 5.1.14393.1066

On windows 10, with the following $psversiontable output:

Name Value


PSVersion 5.1.14393.1066
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14393.1066
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

I am getting the following error:

ErrorRecord                 : Exception calling "Load" with "1" argument(s): "(Line: 7, Col: 32, Idx: 143) - (Line: 7, 
                              Col: 33, Idx: 144): While parsing a block mapping, did not find expected key."
WasThrownFromThrowStatement : False
Message                     : Exception calling "Load" with "1" argument(s): "(Line: 7, Col: 32, Idx: 143) - (Line: 7, 
                              Col: 33, Idx: 144): While parsing a block mapping, did not find expected key."
Data                        : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException              : System.Management.Automation.MethodInvocationException: Exception calling "Load" with 
                              "1" argument(s): "(Line: 7, Col: 32, Idx: 143) - (Line: 7, Col: 33, Idx: 144): While 
                              parsing a block mapping, did not find expected key." ---> 
                              YamlDotNet.Core.SemanticErrorException: (Line: 7, Col: 32, Idx: 143) - (Line: 7, Col: 
                              33, Idx: 144): While parsing a block mapping, did not find expected key.
                                 at YamlDotNet.Core.Parser.ParseBlockMappingKey(Boolean isFirst) in 
                              c:\projects\yamldotnet\YamlDotNet\Core\Parser.cs:line 709
                                 at YamlDotNet.Core.Parser.MoveNext() in 
                              c:\projects\yamldotnet\YamlDotNet\Core\Parser.cs:line 114
                                 at YamlDotNet.Core.EventReader.Allow[T]() in 
                              c:\projects\yamldotnet\YamlDotNet\Core\EventReader.cs:line 110
                                 at YamlDotNet.Core.EventReader.Expect[T]() in 
                              c:\projects\yamldotnet\YamlDotNet\Core\EventReader.cs:line 67
                                 at YamlDotNet.RepresentationModel.YamlScalarNode..ctor(EventReader events, 
                              DocumentLoadingState state) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlScalarNode.cs:line 57
                                 at YamlDotNet.RepresentationModel.YamlNode.ParseNode(EventReader events, 
                              DocumentLoadingState state) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlNode.cs:line 84
                                 at YamlDotNet.RepresentationModel.YamlMappingNode..ctor(EventReader events, 
                              DocumentLoadingState state) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlMappingNode.cs:line 71
                                 at YamlDotNet.RepresentationModel.YamlNode.ParseNode(EventReader events, 
                              DocumentLoadingState state) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlNode.cs:line 94
                                 at YamlDotNet.RepresentationModel.YamlDocument..ctor(EventReader events) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlDocument.cs:line 72
                                 at YamlDotNet.RepresentationModel.YamlStream.Load(EventReader reader) in 
                              c:\projects\yamldotnet\YamlDotNet\RepresentationModel\YamlStream.cs:line 104
                                 at CallSite.Target(Closure , CallSite , Object , TextReader )
                                 --- End of inner exception stack trace ---
                                 at 
                              System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext 
                              funcContext, Exception exception)
                                 at 
                              System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame 
                              frame)
                                 at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interp
                              retedFrame frame)
                                 at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(Interp
                              retedFrame frame)
                                 at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
                                 at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
                                 at System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object 
                              dollarUnderbar, Object inputToProcess)
                                 at System.Management.Automation.PSScriptCmdlet.DoProcessRecord()
                                 at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetSite                  : System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject] 
                              Invoke(System.Collections.IEnumerable)
StackTrace                  :    at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
                                 at Microsoft.PowerShell.Executor.ExecuteCommandHelper(Pipeline tempPipeline, 
                              Exception& exceptionThrown, ExecutionOptions options)
HelpLink                    : 
Source                      : System.Management.Automation
HResult                     : -2146233087

Problems with round-tripping.

The powershell-yaml module is quite very effective, however there are some tiny things that go missing when converting from yaml to PSObject and back to yaml.

  1. Comments are missing.
    i.e. Comments starting with #

  2. Values with quotes don't convert well
    TargetFolder: '.' becomes TargetFolder: .
    versionSpec: '3.6' becomes versionSpec: "3.6"

Version 0.3.7 broken in Windows PowerShell

Hi!

Just noticed I can no longer use powershell-yaml version 0.3.7. I get this error:

 $data = ConvertFrom-Yaml -Yaml $yaml

Cannot convert argument "input", with value: "YamlDotNet.Core.Parser", for "Load" to type "System.IO.TextReader": "Cannot convert the 
"YamlDotNet.Core.Parser" value of type "YamlDotNet.Core.Parser" to type "System.IO.TextReader"."
At C:\Program Files\WindowsPowerShell\Modules\powershell-yaml\0.3.7\powershell-yaml.psm1:38 char:9
+         $yamlStream.Load([YamlDotNet.Core.IParser] $parser)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

This works without issue if I fall back to 0.3.6

For what it's worth:

PS C:\gitlab\builds\1ecf08d8\0\common\AllTheThings> $PSVersionTable

Name                           Value                                                                                                                         
----                           -----                                                                                                                         
PSVersion                      5.0.10586.117                                                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                       
BuildVersion                   10.0.10586.117                                                                                                                
CLRVersion                     4.0.30319.42000                                                                                                               
WSManStackVersion              3.0                                                                                                                           
PSRemotingProtocolVersion      2.3                                                                                                                           
SerializationVersion           1.1.0.1                                                                                                                       

0.3.6 works fine for my purposes, but in case this was unexpected or a bug, just an FYI : )

Cheers!

ConvertTo-YAML with empty strings getting converted to nulls

Conversion of empty strings is not working correctly:

PS C:\VSProjects\AdvancedComposites\ansible> get-module Powershell-yaml

ModuleType Version    Name                                ExportedCommands                                                                             
---------- -------    ----                                ----------------                                                                             
Script     0.3.7      powershell-yaml                     {ConvertFrom-Yaml, ConvertTo-Yaml, cfy, cty}                                                 

ConvertTo-Yaml

PS C:\> $A = @{'Fred'=""}
PS C:\> $A | ConvertTo-Yaml
Fred: 

Expected Result: Fred: "".

ConvertFrom-Yaml

PS C:\> ('fred: ""' | ConvertFrom-Yaml).fred.value -eq $null
True

Expected Result: False.

Multiple versions throws exception

I'm experiancing the following issue when trying to import a newer version of the module within Powershell 7.0.0-preview.6

PS C:\> Get-Module -Name powershell-yaml -ListAvailable


    Directory: C:\Users\mvangorp\OneDrive - <company>\Documents\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     0.4.1                 powershell-yaml                     Desk      {ConvertTo-Yaml, ConvertFrom-Yaml, cfy, cty}
Script     0.3.5                 powershell-yaml                     Desk      {ConvertTo-Yaml, ConvertFrom-Yaml, cfy, cty}

PS C:\> Import-Module -Name powershell-yaml -RequiredVersion 0.4.1
Exception: YamlDotNet is loaded but missing required types (SerializerBuilder). Older version installed on system?

Fails to serialize arrays with a single element

When trying to serialize an array with a single element, it actually serializes the element itself and not an array with a single element. If array has more than an element, everything is good. Code examples:

PS C:\Users\ibalutoiu\Downloads> ConvertTo-Yaml @(@{'key1'='value1'})
key1: value1

PS C:\Users\ibalutoiu\Downloads> ConvertTo-Yaml @(@{'key1'='value1'}, @{'key2'='value2'})
- key1: value1
- key2: value2

This behavior is not present in the built-in Json library from PowerShell. Everything is serialized as explected:

PS C:\Users\ibalutoiu\Downloads> ConvertTo-Json @(@{'key1'='value1'})
[
    {
        "key1":  "value1"
    }
]
PS C:\Users\ibalutoiu\Downloads> ConvertTo-Json @(@{'key1'='value1'}, @{'key2'='value2'})
[
    {
        "key1":  "value1"
    },
    {
        "key2":  "value2"
    }
]

ConvertTo-Yaml does not supply quotes in YAML files for strings

Conversion of strings is missing quote characters and can result in dropped characters with IP address fragments.

PS C:\VSProjects\ansible> get-module Powershell-yaml

ModuleType Version    Name                                ExportedCommands                                                                             
---------- -------    ----                                ----------------                                                                             
Script     0.3.7      powershell-yaml                     {ConvertFrom-Yaml, ConvertTo-Yaml, cfy, cty}                                                 

I store IP addresses in two fields

PS C:\> $A = @{ IProot="10.100"; IPTail = "0.100" }

PS C:\> $A

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
IProot                         10.100                                                                                                                  
IPTail                         0.100                                                                                                                   



PS C:\> $A | ConvertTo-Yaml
IProot: 10.100
IPTail: 0.100


PS C:\> $A | ConvertTo-Yaml | ConvertFrom-Yaml

Name                           Value                                                                                                                   
----                           -----                                                                                                                   
IProot                         10.1                                                                                                                    
IPTail                         0.1   

Expected result is for these values to NOT drop trailing zeros.

ConvertFrom-Yaml - key named `key` causes weird output when converted back to yaml

This happens when object with key named key are converted back and forth (try below command without the last two convertfrom convertto)
To reproduce, run:

[PSCustomObject]@{keys = @{key = 'YesPlease!'}} | ConvertTo-Yaml | ConvertFrom-Yaml | ConvertTo-Yaml

Which gives following output:

? &o0
  key: YesPlease!
:
keys: *o0

Looking at (aaubry/YamlDotNet#126), it seems like this is related to DisableAliases option.
`

This was reproduced both on PS6 on Linux and PS5 on Win10.

Continuous integration

AppVeyor and TravisCI provide great services that can get your module tested on PowerShell 4+ and Core (on Linux and MacOS) for free.

Would you accept a PR for the CI setup?

ConvertFrom-Yaml forces bool type

Hi all,

I'm having some issues with the ConvertFrom-Yaml function. I'm trying to pass through some inputs via powershell to an application which is fussy about data types. In my input document I have this key:

keep_backups: off

When I run the document through ConvertFrom-Yaml it forces the type to a powershell bool, but ideally I would have the string "off". Escaping the text with different quotes doesn't seem to make any difference. I know I could write logic into my powershell code to catch this, but it's not very convenient especially as the key is buried within a large document. As I can't escape the text, it seems that this is also a divergence from the YAML standard.

Output objects, not hash tables

ConvertFrom-Json outputs objects by default, which are displayed nicer than name/value hashmaps. It only outputs hashmaps if -AsHashTable is passed. It would be great if ConvertFrom-Yaml worked the same.

Output multiple documents

I'm wondering whether it would be possible to output multiple documents with ConvertTo-Yaml -AllDocuments after reading them in with ConvertFrom-Yaml -AllDocuments.

Essentially, this should not change the file:

Get-Content foo.yml | ConvertFrom-Yaml -AllDocuments | ConvertTo-Yaml -AllDocuments | OutFile foo-yml

Indent array values

Is there any way to indent array values with 2 spaces instead of staying at same level? If no could there be option for it?

Example:

@{notes = @('some', 'notes', 'as array')} | ConvertTo-Yaml

Actual:

notes:
- some
- notes
- as array

Desired:

notes:
  - some
  - notes
  - as array

Why can't I use -options EmitDefaults and -JsonCompatible

Hi @gabriel-samfira,
Great job on this module.

I'm wondering why theses two parameters have different parameter sets.
The issue I have is when I want to serialize an object where one of the property is a bool with false value.
When converting To yaml with EmitDefaults, it works. But the JSONCompatible does not have this option and does not show that key when value is false.

Convert*-Json Compatibility

I think a major issue preventing wider adoption of this module is that users expect all the same commands and parameters present in ConvertFrom/To-Json, like -Compress and -Depth.

I'd be willing to write the PRs if you'll entertain merging them and considering some potential breaking changes for the next major version, otherwise I'll write it separately as a new module, PowerYAML.

Let me know your thoughts.

Empty array not working properly

Running

convertfrom-yaml 'empty-array: []' | convertto-yaml

outputs:

Convert-ListToGenericList : Cannot bind argument to parameter 'Data' because it is an empty collection.
At C:\Program Files\PowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:193 char:50
+                 return Convert-ListToGenericList $data
+                                                  ~~~~~
+ CategoryInfo          : InvalidData: (:) [Convert-ListToGenericList], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyCollectionNotAllowed,Convert-ListToGenericList

Exception calling "Serialize" with "2" argument(s): "Too much recursion when traversing the object graph"
At C:\Program Files\PowerShell\Modules\powershell-yaml\0.3.1\powershell-yaml.psm1:277 char:13
+             $serializer.Serialize($wrt, $norm)
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

empty-array:
  BaseObject: &o8 {}
  Members:
  - MemberType: Method
    OverloadDefinitions:
    - string ToString()
    TypeNameOfValue: System.Management.Automation.PSMethod
    Value:
      MemberType: Method
      OverloadDefinitions:
      - string ToString()
      TypeNameOfValue: System.Management.Automation.PSMethod
      Value:
        MemberType: Method
        OverloadDefinitions:
        - string ToString()
        TypeNameOfValue: System.Management.Automation.PSMethod
        Value:
          MemberType: Method
          OverloadDefinitions:
          - string ToString()
          TypeNameOfValue: System.Management.Automation.PSMethod
          Value:
            MemberType: Method
            OverloadDefinitions:
            - string ToString()
            TypeNameOfValue: System.Management.Automation.PSMethod
            Value:
              MemberType: Method
              OverloadDefinitions:
              - string ToString()
              TypeNameOfValue: System.Management.Automation.PSMethod
              Value:
                MemberType: Method
                OverloadDefinitions:
                - string ToString()
                TypeNameOfValue: System.Management.Automation.PSMethod
                Value:
...

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.