-
Notifications
You must be signed in to change notification settings - Fork 10
/
Convert-HashTableToXml.ps1
134 lines (115 loc) · 3.73 KB
/
Convert-HashTableToXml.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
122
123
124
125
126
127
128
129
130
131
132
133
134
function Convert-HashTableToXML() {
<#
.SYNOPSIS
COnverts one or more Powershell hashtables into a simple XML format.
.DESCRIPTION
Creates simpler and more human-readable output for a hashtable than Export-CliXML or ConvertTo-XML.
This is useful for instance when storing attributes or configuration variables for output to other
program or storage in an AD CustomAttribute.
This command will create appropriate subnodes if you have nested hashtables.
.NOTES
Adapted from original script by Blindrood (https://gallery.technet.microsoft.com/scriptcenter/Export-Hashtable-to-xml-in-122fda31)
.PARAMETER InputObject
A Powershell hashtable that contains the name-value pairs you wish to convert to XML elements
.PARAMETER Root
Allows you to specify the root XML element definition.
.PARAMETER OutPath
Path to an output XML file, if desired. If not specified, outputs directly to the pipeline
.EXAMPLE
Create a Hashtable
PS C:\> $Configuration = @{
'Definitions' = @{
'ConnectionString' = 'sql=srv01;port=223'
'MonitoringLevel' = 'MonitoringLevelValue'
}
'Conventions' = @{
'MyConvention' = 'This is my convention'
'Option' = 'Zip'
'ServerType' = 'sql'
'Actions' = @{
'SpecificAction' = 'DoNothing'
'DefaultAction' = 'Destroy it All'
}
'ExceptionAction' = 125
'Period' = New-TimeSpan -Seconds 20
}
'ServiceAccount' = @{
'UserName' = 'mydomain.com\thisisme'
'Password' = '123o123'
}
'GroupConfiguration' = @{
'AdminsGroup' = 'mydomain.com\thisisAdminsGroup'
'UsersGroup' = 'mydomain.com\thisisUsersGroup'
}
}
.EXAMPLE
Export the
$Configuration | Out-HashTableToXml -Root 'Configuration' -File $env:temp\test.xml
-----------------
Test.XML Contents
-----------------
<Configuration>
<Conventions>
<ExceptionAction>125</ExceptionAction>
<ServerType>sql</ServerType>
<Actions>
<SpecificAction>DoNothing</SpecificAction>
<DefaultAction>Destroy it All</DefaultAction>
</Actions>
<Period>00:00:20</Period>
<Option>Zip</Option>
<MyConvention>This is my convention</MyConvention>
</Conventions>
<GroupConfiguration>
<UsersGroup>mydomain.com\thisisUsersGroup</UsersGroup>
<AdminsGroup>mydomain.com\thisisAdminsGroup</AdminsGroup>
</GroupConfiguration>
<Definitions>
<MonitoringLevel>MonitoringLevelValue</MonitoringLevel>
<ConnectionString>sql=srv01;port=223</ConnectionString>
</Definitions>
<ServiceAccount>
<Password>123o123</Password>
<UserName>mydomain.com\thisisme</UserName>
</ServiceAccount>
</Configuration>
#>
Param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[System.Collections.Hashtable]$InputObject,
[ValidateScript({Test-Path $_ -IsValid})]
[System.String]$OutPath,
[System.String]$Root="PSHashTable"
)
Begin{
$ScriptBlock = {
Param($Elem, $Root)
if( $Elem.Value -is [System.Collections.Hashtable] ){
$RootNode = $Root.AppendChild($Doc.CreateNode([System.Xml.XmlNodeType]::Element,$Elem.Key,$Null))
$Elem.Value.GetEnumerator() | ForEach-Object {
$Scriptblock.Invoke( @($_, $RootNode) )
}
}
else{
$Element = $Doc.CreateElement($Elem.Key)
$Element.InnerText = if($Elem.Value -is [Array]) {
$Elem.Value -join ','
}
else{
$Elem.Value | Out-String
}
$Root.AppendChild($Element) | Out-Null
}
}
} #Begin
Process{
$Doc = [xml]"<$($Root)></$($Root)>"
$InputObject.GetEnumerator() | ForEach-Object {
$scriptblock.Invoke( @($_, $doc.DocumentElement) )
}
d
#Output the formatted XML document if OutPath is specified, otherwise send to pipeline
if ($OutPath) {$doc.save($OutPath)}
else {$doc}
} #Process
} #Out-HashTableToXML