-
Notifications
You must be signed in to change notification settings - Fork 29
/
Format-Xml.ps1
106 lines (98 loc) · 3.78 KB
/
Format-Xml.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
<#
.SYNOPSIS
Pretty-print XML.
.INPUTS
System.Xml.XmlDocument to serialize.
.OUTPUTS
System.String containing the serialized XML document with desired indents.
.FUNCTIONALITY
XML
.EXAMPLE
Get-PSProvider alias |ConvertTo-Xml |Format-Xml.ps1
<Objects>
<Object Type="System.Management.Automation.ProviderInfo">
<Property Name="ImplementingType" Type="System.RuntimeType">Microsoft.PowerShell.Commands.AliasProvider</Property>
<Property Name="HelpFile" Type="System.String">System.Management.Automation.dll-Help.xml</Property>
<Property Name="Name" Type="System.String">Alias</Property>
<Property Name="PSSnapIn" Type="System.Management.Automation.PSSnapInInfo">Microsoft.PowerShell.Core</Property>
<Property Name="ModuleName" Type="System.String">Microsoft.PowerShell.Core</Property>
<Property Name="Module" Type="System.Management.Automation.PSModuleInfo" />
<Property Name="Description" Type="System.String"></Property>
<Property Name="Capabilities" Type="System.Management.Automation.Provider.ProviderCapabilities">ShouldProcess</Property>
<Property Name="Home" Type="System.String"></Property>
<Property Name="Drives" Type="System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSDriveInfo]">
<Property Type="System.Management.Automation.PSDriveInfo">Alias</Property>
</Property>
</Object>
</Objects>
.EXAMPLE
Get-PSProvider alias |ConvertTo-Xml |Format-Xml.ps1 -NewLineOnAttributes
<Objects>
<Object
Type="System.Management.Automation.ProviderInfo">
<Property
Name="ImplementingType"
Type="System.RuntimeType">Microsoft.PowerShell.Commands.AliasProvider</Property>
<Property
Name="HelpFile"
Type="System.String">System.Management.Automation.dll-Help.xml</Property>
<Property
Name="Name"
Type="System.String">Alias</Property>
<Property
Name="PSSnapIn"
Type="System.Management.Automation.PSSnapInInfo">Microsoft.PowerShell.Core</Property>
<Property
Name="ModuleName"
Type="System.String">Microsoft.PowerShell.Core</Property>
<Property
Name="Module"
Type="System.Management.Automation.PSModuleInfo" />
<Property
Name="Description"
Type="System.String"></Property>
<Property
Name="Capabilities"
Type="System.Management.Automation.Provider.ProviderCapabilities">ShouldProcess</Property>
<Property
Name="Home"
Type="System.String"></Property>
<Property
Name="Drives"
Type="System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSDriveInfo]">
<Property
Type="System.Management.Automation.PSDriveInfo">Alias</Property>
</Property>
</Object>
</Objects>
#>
#Requires -Version 7
[CmdletBinding()][OutputType([string])] Param(
# The XML string or document to format.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][xml] $Xml,
# A whitespace indent character to use, space by default.
[ValidatePattern('\A\s\z',ErrorMessage='A whitespace character is required')]
[char] $IndentChar = ' ',
<#
The number of IndentChars to use per level of indent, 2 by default.
Set to zero for no indentation.
#>
[int] $Indentation = 2,
# Indicates attributes should be written on a new line.
[Alias('SplitAttributes','AttributesSeparated')][switch] $NewLineOnAttributes
)
Process
{
[Xml.XmlWriterSettings] $cfg = New-Object Xml.XmlWriterSettings -Property @{
Indent = !!$Indentation
IndentChars = "$IndentChar" * $Indentation
OmitXmlDeclaration = $true
NewLineOnAttributes = $NewLineOnAttributes
}
$sw = New-Object IO.StringWriter
[Xml.XmlWriter] $xw = [Xml.XmlWriter]::Create($sw, $cfg)
$Xml.WriteTo($xw)
$xw.Dispose()
$sw.ToString()
$sw.Dispose()
}