• Stars
    star
    390
  • Rank 106,472 (Top 3 %)
  • Language
    PowerShell
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PowerShell CmdLets for YAML format manipulation

powershell-yaml

Actions Status

This powershell module is a thin wrapper on top of YamlDotNet that serializes and un-serializes simple powershell objects to and from YAML. It was tested on powershell versions 4 and 5, supports Nano Server and apparently works with powershell on Linux. I suspect it works on Mac as well, but I have not had a chance to test it.

The lib folder contains the YamlDotNet assemblies. They are not really required, just a fall-back in case your system does not already have them installed and loaded. Feel free to remove the lib folder if you prefer to add the required assemblies yourself.

Installation

This module is available for installation via Powershell Gallery. Simply run the following command:

Install-Module powershell-yaml

ConvertTo-Yaml

Import-Module powershell-yaml

PS C:\> $yaml = ConvertTo-Yaml @{"hello"="world"; "anArray"=@(1,2,3); "nested"=@{"array"=@("this", "is", "an", "array")}}
PS C:\> $yaml
anArray:
- 1
- 2
- 3
nested:
  array:
  - this
  - is
  - an
  - array
hello: world

ConvertFrom-Yaml

Single YAML document

Import-Module powershell-yaml

PS C:\> $yaml = @"
anArray:
- 1
- 2
- 3
nested:
  array:
  - this
  - is
  - an
  - array
hello: world
"@

PS C:\> $obj = ConvertFrom-Yaml $yaml
PS C:\> $obj

Name                           Value
----                           -----
anArray                        {1, 2, 3}
nested                         {array}
hello                          world

PS C:\> $obj.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

Multiple YAML documents

Unserializing multiple documents results in an array representing the contents of each document. The result of this does not translate back to the same documents if you pass it back through ConvertTo-Yaml.

Import-Module powershell-yaml

PS C:\> $yaml = @"
---
anArray:
- 1
- 2
- 3
nested:
  array:
  - this
  - is
  - an
  - array
hello: world
---
second: document
goodbye: world
"@

PS C:\> $obj = ConvertFrom-Yaml $yaml -AllDocuments
PS C:\> $obj

Name                           Value
----                           -----
anArray                        {1, 2, 3}
nested                         {array}
hello                          world
goodbye                        world
second                         document

PS C:\> $obj.GetType()

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

PS C:\> $obj[0]

Name                           Value
----                           -----
anArray                        {1, 2, 3}
nested                         {array}
hello                          world

PS C:\> $obj[1]

Name                           Value
----                           -----
goodbye                        world
second                         document

Merge keys support

$mergingYaml = @"
---
default: &default
  value1: 1
  value2: 2

hoge:
  <<: *default
  value3: 3
"@

ConvertFrom-Yaml -Yaml $mergingYaml -UseMergingParser

Name                           Value
----                           -----
default                        {value1, value2}
hoge                           {value2, value3, value1}

Important note: For the time being, overwriting keys will throw a duplicate key exception.

$mergingYamlWithDuplicates = @"
---
default: &default
  value1: 1
  value2: 2

hoge:
  <<: *default
  # this is a duplicate
  value1: 44
  value3: 3
"@

Converting from YAML to JSON

The awesome YamlDotNet assembly allows us to serialize an object in a JSON compatible way. Unfortunately it does not support indentation. Here is a simple example:

Import-Module powershell-yaml

PS C:\> $yaml = @"
anArray:
- 1
- 2
- 3
nested:
  array:
  - this
  - is
  - an
  - array
hello: world
"@

PS C:\> $obj = ConvertFrom-Yaml $yaml
PS C:\> $obj

Name                           Value
----                           -----
anArray                        {1, 2, 3}
nested                         {array}
hello                          world

PS C:\> ConvertTo-Yaml -JsonCompatible $obj
{"anArray": [1, 2, 3], "nested": {"array": ["this", "is", "an", "array"]}, "hello": "world"}

# Or you could do it in one line.
PS C:\> ConvertFrom-Yaml $yaml | ConvertTo-Yaml -JsonCompatible
{"anArray": [1, 2, 3], "nested": {"array": ["this", "is", "an", "array"]}, "hello": "world"}

Using tags

Using tags is prefered as opposed to allowing powershell-yaml to infer the type. Whenever there is a risc of ambiguity, use tags to make sure your values are converted using the intended type. This module supports the tags specified by the core schema, and aditionally the !!timestamp tag.

Import-Module powershell-yaml

PS C:\> $data = @"
aPhoneNumber: !!str +40123456789
aPhoneNrWithoutTags: +40123456789
"@
PS C:\> ConvertFrom-Yaml $data

Name                           Value
----                           -----
aPhoneNrWithoutTags            40123456789
aPhoneNumber                   +40123456789

PS C:\> $obj = ConvertFrom-Yaml $data
PS C:\> $obj.aPhoneNumber.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

PS C:\> $obj.aPhoneNrWithoutTags.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Int64                                    System.ValueType

As you can see, the phone number without tags was cast to Int64. This is most likely not the desired result and a case where tags should be used.

Running the tests

Before running the associated unit tests; please make sure you have Pester installed, as it is the testing framework of choice.

After Pester is up and running, the tests may be ran by simply entering the tests directory and running Invoke-Pester:

PS C:\> Install-Module Pester
PS C:\> Install-Module Assert
PS C:\> git clone https://github.com/cloudbase/powershell-yaml.git $HOME\powershell-yaml
PS C:\> cd $HOME\powershell-yaml
PS C:\Users\Guest\powershell-yaml> powershell.exe -NonInteractive -Command {Invoke-Pester}

More Repositories

1

windows-imaging-tools

Tools to automate the creation of a Windows image for OpenStack, supporting KVM, Hyper-V, ESXi and more.
PowerShell
579
star
2

cloudbase-init

Cross-platform instance initialization
Python
363
star
3

unattended-setup-scripts

Various unattended setup scripts and configuration files. Note: the content of this repo is mostly WiP for R&D. When scripts are ready they are usually moved to a dedicated repo.
PowerShell
100
star
4

coriolis

Cloud Migration as a Service
Python
90
star
5

garm

GitHub Actions Runners Manager
Go
82
star
6

qemu

Fork of git://git.qemu.org/qemu.git
C
75
star
7

FreeRDP-Windows-Build

FreeRDP Windows build scripts
PowerShell
69
star
8

salt-openstack

SaltStack
61
star
9

wnbd

Windows Ceph RBD NBD driver
C++
55
star
10

go-winrm

Golang WinRM package
Go
51
star
11

cloudbase-init-offline-install

PowerShell
41
star
12

vdi-broker

OpenStack VDI broker
Python
31
star
13

winrm-scripts

Scripts to configure and use WinRM certificate authentication
Python
23
star
14

PyMI

A blazing fast replacement for the Python WMI module
C++
19
star
15

aurora

A fresh OpenStack dashboard
CSS
18
star
16

openvswitch-hyperv

Open vSwitch Hyper-V porting
C
17
star
17

WindowsUpdateCLI

Windows Update PowerShell tools
PowerShell
16
star
18

kubinstaller

A desktop GUI for simplifying the deployment of Kubernetes clusters
JavaScript
15
star
19

windows-heat-templates

Windows OpenStack Heat templates
Shell
13
star
20

openstack-rdo-scripts

RDO automation scripts
Shell
12
star
21

coriolis-web

Web UI for Coriolis
TypeScript
12
star
22

HyperVPCapExt

Hyper-V virtual switch packet capturing extension with libpcap / Wireshark format
C
12
star
23

ceph-windows-installer

Ceph Windows Installer
PowerShell
12
star
24

VSKubernetes

Kubernetes extension for Visual Studio
C#
11
star
25

python-ovmclient

Python Oracle VM Client
Python
8
star
26

openstack-puppet-samples

OpenStack Puppet samples
Puppet
8
star
27

openvswitch-hyperv-kernel

Hyper-V Open vSwitch forwarding extension
C
7
star
28

v-magine

The easiest way to deploy OpenStack on Windows
JavaScript
7
star
29

cloudbase-init-ci

Continuous integration testing framework for Cloudbase-Init
Python
7
star
30

checkhypervisor

Utility to detect if the OS is running as a guest on a known hypervisor
C
7
star
31

heat2arm

OpenStack Heat to Azure ARM template conversion tool
Python
7
star
32

cloudbase-init-installer

Cloudbase-Init MSI installer
Python
7
star
33

adk-tools-maas

ADK tools for automated WinPE provisioning in MaaS
PowerShell
7
star
34

wmi

Python WMI module, clone from SVN repository: http://svn.timgolden.me.uk/wmi/trunk
Python
6
star
35

windows-curtin-hooks

Python
6
star
36

maas-hacks

Python
6
star
37

openstack-dashboard-cloudbase-theme

CSS
5
star
38

EHS

Embedded HTTP Server (EHS) is a C++ class library which can be inherited from to add HTTP(S) server functionality to any class or application. It is easily extendable (samples included) and supports SSL, form data via POST or GET, uploads via multi-part form attachments and WebSockets. Operation modes include single- and multi-threaded operation. Forked from: http://sourceforge.net/projects/ehs/
C++
5
star
39

cloudbase-init-test-resources

PowerShell
5
star
40

hyper-c

Hyper-Converged OpenStack Nano Server deployment tools
Python
5
star
41

openvswitch-windows-port

Port of Open vSwitch 1.9.x to Microsoft Windows
C
5
star
42

kolla-resources

OpenStack Kolla scripts and other resources
Shell
4
star
43

python-coriolisclient

Cloud Migration as a Service
Python
4
star
44

gitpoll

Easy way to poll for changes on remote git repositories and execute url based actions
Python
4
star
45

maas

MaaS + Hyper-V
Python
4
star
46

juju-powershell-modules

Juju Powershell utilities
HTML
4
star
47

nova-vix-driver

OpenStack Nova compute driver for VMware Workstation or Player (Windows, Linux) and Fusion (Mac OS X).
Python
4
star
48

garm-provider-azure

Garm external provider for Azure
Go
4
star
49

rct-service

REST Service for the Hyper-V RCT API
Rust
3
star
50

coriolis-logger

Coriolis Logging
Go
3
star
51

neutron-virtualbox

Neutron VirtualBox agent
Python
3
star
52

nova-virtualbox

Nova VirtualBox driver
Python
3
star
53

os-windows

Python
3
star
54

vdi-web

VDI web frontend
Java
3
star
55

puppet-nano-server

Puppet for Windows Nano Server
Ruby
3
star
56

reBot

Plastic as a Service
Java
3
star
57

garm-provider-aws

Garm external provider for EC2
Go
3
star
58

garm-provider-incus

Incus external provider for GARM
Go
3
star
59

openvswitch-hyperv-installer

Open vSwitch Hyper-V installer
PowerShell
3
star
60

FreeRDP-WebConnect-Installer

FreeRDP-WebConnect Windows installer
JavaScript
3
star
61

VSSSnapshotCopy

Windows VSS backup file copy utility
C#
3
star
62

StackCraft

OpenStack on MineCraft!
Java
3
star
63

job-runner

Lightweight application created for running jobs on demand with a RESTful API interface and a scalable backend. Job queues are based on Zeromq.
Python
3
star
64

openstack-hyperv-release-tests

OpenStack Hyper-V integration testing tools
PowerShell
2
star
65

active-directory-charm

Active Directory Charm
PowerShell
2
star
66

rpi-tensorflow-build

Raspberry Pi Dockerfiles for TensorFlow & co.
HCL
2
star
67

arestor

Python
2
star
68

BMK

Shell
2
star
69

horizon-cloudbase

Cloudbase CSS and settings for Horizon
CSS
2
star
70

coriolis-ovm-exporter

Tools for Oracle VM snapshot management
Go
2
star
71

garm-provider-equinix

Equinix Metal external provider for GARM
Go
2
star
72

garm-provider-lxd

LXD external provider for GARM
Go
2
star
73

nova-ci

Hyper-V Nova CI
Shell
2
star
74

ovs-windows-installer

Open vSwitch Windows installer
JavaScript
2
star
75

FreeRDP-WebConnect-Packages

Binary packages and scripts for FreeRDP-WebConnect
2
star
76

ci-overcloud-init-scripts

Shell
2
star
77

SetUserAccountRights

Simple CLI for managing Windows user account rights, including Nano Server support
C++
2
star
78

hyperv-networking-ci

CI for networking-hyperv project
Shell
1
star
79

gh-auto-labeler

1
star
80

freerdp-charm

PowerShell
1
star
81

libuuid

MinGW compatible libuuid
Shell
1
star
82

python-hnvclient

Python client for the HNV (Hyper-V Network Virtualization) REST API
Python
1
star
83

coriolis-snapshot-agent

Go
1
star
84

cinder-ci

Cinder CI for iSCSI support on Microsoft Windows and SMB3 support on Windows and Linux
PowerShell
1
star
85

oswin-ci

OS-Win CI
Shell
1
star
86

coriolis-resources

PowerShell
1
star
87

nova-hyperv-charm

OpenStack Nova Hyper-V juju charm
PowerShell
1
star
88

k8s-operator-garm

1
star
89

aurora-core

TypeScript
1
star
90

garm-provider-openstack

Garm external provider for OpenStack
Go
1
star
91

magnum-charm

Magnum Charm
Python
1
star
92

openstack-kolla-arm64-scripts

Deploy OpenStack with Kolla on ARM64
Shell
1
star
93

adk-tools

ADK image build automation scripts for Windows Server 2012 and Hyper-V Server 2012
PowerShell
1
star
94

android-emulator-ci

PowerShell
1
star
95

hyperv-compute-ci

CI for compute-hyperv project
Shell
1
star
96

garm-provider-common

Common functionality for GARM providers
Go
1
star
97

cloudbase-init-packer-scripts

Packer scripts for creation of Windows images with cloudbase-init
PowerShell
1
star
98

nova-guest-agent

Python
1
star
99

windows-charms-boilerplate

Boilerplate template for Windows Juju charms
PowerShell
1
star
100

openstack-dev-scripts

Some basic time saving scripts
Shell
1
star