Skip to content

ConvertTo-Yaml - PSCustomObject with $null properties #24

@thecliguy

Description

@thecliguy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions