-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConvertTo-PSON.ps1
121 lines (92 loc) · 3.99 KB
/
ConvertTo-PSON.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#Requires -Version 5
<#
.SYNOPSIS
This function tests whether the specified configuration file (PSD1) contains
all of the required fields.
.NOTES
This code adheres to the style guidelines published at
https://poshcode.gitbooks.io/powershell-practice-and-style/
Copyright (c) 2018 David Passarelli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#>
function ConvertTo-PSON {
[CmdletBinding()]
param(
[Parameter(Position=0)]
[Hashtable]$Source
)
# https://blogs.technet.microsoft.com/heyscriptingguy/2014/12/03/enforce-better-script-practices-by-using-set-strictmode/
#
Set-StrictMode -Version Latest
###################################
##### INPUT VALIDATION #####
###################################
if ($null -eq $Source) {
throw "The input value is required."
}
###################################
##### MAIN ALGORITHM #####
###################################
[System.Text.StringBuilder]$output = New-Object -Type System.Text.StringBuilder # interesting read @ http://www.yoda.arachsys.com/csharp/stringbuilder.html
$output.Append("@{") | Out-Null # `Out-Null` is required here to keep extraneous output from being returned...see https://stackoverflow.com/a/22682725
foreach ($key in $Source.Keys) {
$output.Append($key) | Out-Null
$output.Append("=") | Out-Null
$value = $Source.$key
if ($value -eq $null) {
$output.Append("`$null") | Out-Null
}
else {
$typeName = $value.GetType().Name
switch ($typeName) {
"string" {
$output.Append("""$value""") | Out-Null
}
"int32" {
$output.Append($value) | Out-Null
}
"double" {
$output.Append($value) | Out-Null
}
"boolean" {
if ($value) {
$output.Append("`$true") | Out-Null
}
else {
$output.Append("`$false") | Out-Null
}
}
"datetime" {
$output.Append("[datetime]""") | Out-Null
$output.Append($value.ToString("O")) | Out-Null
$output.Append("""") | Out-Null
}
"hashtable" {
$nestedTable = (ConvertTo-PSON $value)
$output.Append($nestedTable) | Out-Null
}
default {
throw "Unable to serialize type [$typeName]"
}
}
}
$output.Append(";") | Out-Null
}
$output.Append("}") | Out-Null
Write-Verbose "ConvertTo-PSON output: $($output.ToString())"
$output.ToString()
}