-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to convert yaml directly from file
- Loading branch information
Chris Black
committed
Jan 30, 2018
1 parent
1240d1d
commit 451c050
Showing
1 changed file
with
47 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {} | ||
} |