From 451c050d26454eaba011bb31d024c29d12b94065 Mon Sep 17 00:00:00 2001 From: Chris Black Date: Tue, 30 Jan 2018 01:53:22 +0000 Subject: [PATCH] Adding ability to convert yaml directly from file --- PSYaml/Public/ConvertFrom-Yaml.ps1 | 55 +++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/PSYaml/Public/ConvertFrom-Yaml.ps1 b/PSYaml/Public/ConvertFrom-Yaml.ps1 index 7af1436..08b11fa 100644 --- a/PSYaml/Public/ConvertFrom-Yaml.ps1 +++ b/PSYaml/Public/ConvertFrom-Yaml.ps1 @@ -1,16 +1,55 @@ -function ConvertFrom-Yaml - { +function ConvertFrom-Yaml { + + <# + .SYNOPSIS + Converts YAML files into PowerShell objects. + + .DESCRIPTION + Converts YAML files into PowerShell objects. Can be invoked either directly from Array String or File Path. + Parameters are mutually exclusive. + + .PARAMETER YamlString + Converts YAML string into PowerShell Array. + + .PARAMETER Path + Converts YAML file into PowerShell Array. + + .EXAMPLE + ConvertFrom-Yaml -Path C:\test.yaml + + .EXAMPLE + @(get-content -Path C:\test.yaml) | ConvertFrom-Yaml + + .LINK + https://github.com/RamblingCookieMonster/PSDeploy/blob/master/PSDeploy/Private/PSYaml/Private/YamlDotNet-Integration.ps1 + + .NOTES + Link above shows where I got the file import from. + + #> [CmdletBinding()] param ( - [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)] - $YamlString + [parameter(Position = 0, Mandatory = $false, ValueFromPipeline = $true)] + $YamlString, + [parameter(Position = 1, Mandatory = $false, ValueFromPipeline = $false)] + $Path ) BEGIN { } PROCESS - {$stringReader = new-object System.IO.StringReader([string]$yamlString) - $yamlStream = New-Object YamlDotNet.RepresentationModel.YamlStream - $yamlStream.Load([System.IO.TextReader]$stringReader) - ConvertFrom-YAMLDocument ($yamlStream.Documents[0])} + { + If($Path){ + $streamReader = [System.IO.File]::OpenText($Path) + $yamlStream = New-Object YamlDotNet.RepresentationModel.YamlStream + $yamlStream.Load([System.IO.TextReader]$streamReader) + ConvertFrom-YAMLDocument ($yamlStream.Documents[0]) + } + Else{ + $stringReader = new-object System.IO.StringReader([string]$yamlString) + $yamlStream = New-Object YamlDotNet.RepresentationModel.YamlStream + $yamlStream.Load([System.IO.TextReader]$stringReader) + ConvertFrom-YAMLDocument ($yamlStream.Documents[0]) + } + } END {} } \ No newline at end of file