forked from stcu/SharedScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertTo-XmlElements.ps1
76 lines (66 loc) · 2.4 KB
/
ConvertTo-XmlElements.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
<#
.SYNOPSIS
Serializes complex content into XML elements.
.INPUTS
System.Object (any object) to serialize.
.OUTPUTS
System.String for each XML-serialized value or property.
.EXAMPLE
ConvertTo-XmlElements.ps1 @{html=@{body=@{p='Some text.'}}}
<html><body><p>Some text.</p></body></html>
.EXAMPLE
[pscustomobject]@{UserName=$env:USERNAME;Computer=$env:COMPUTERNAME} |ConvertTo-XmlElements.ps1
<Computer>COMPUTERNAME</Computer>
<UserName>username</UserName>
.EXAMPLE
Get-ChildItem *.txt |ConvertTo-XmlElements.ps1
<PSPath>Microsoft.PowerShell.Core\FileSystem::C:\temp\test.txt</PSPath>
<PSParentPath>Microsoft.PowerShell.Core\FileSystem::C:\scripts</PSParentPath>
<PSChildName>test.txt</PSChildName>
<PSDrive></PSDrive>
<PSProvider></PSProvider>
<VersionInfo><FileVersionRaw></FileVersionRaw>
<ProductVersionRaw></ProductVersionRaw>
…
#>
#Requires -Version 3
[CmdletBinding()][OutputType([string])] Param(
<#
A hash or XML element or other object to be serialized as XML elements.
Each hash value or object property value may itself be a hash or object or XML element.
#>
[Parameter(Position=0,ValueFromPipeline=$true)] $Value
)
Begin {$Script:OFS = "`n"}
Process
{
if($null -eq $Value) {}
elseif($Value -is [Array])
{ $Value |ConvertTo-XmlElements.ps1 }
elseif([bool],[byte],[DateTimeOffset],[decimal],[double],[float],[guid],[int],[int16],[long],[sbyte],[timespan],[uint16],[uint32],[uint64] -contains $Value.GetType())
{ [Xml.XmlConvert]::ToString($Value) }
elseif($Value -is [datetime])
{ [Xml.XmlConvert]::ToString($Value,'yyyy-MM-dd\THH:mm:ss') }
elseif($Value -is [string] -or $Value -is [char])
{ [Net.WebUtility]::HtmlEncode($Value) }
elseif($Value -is [Hashtable] -or $Value -is [Collections.Specialized.OrderedDictionary])
{ $Value.Keys |? {$_ -match '^\w+$'} |% {"<$_>$(ConvertTo-XmlElements.ps1 $Value.$_)</$_>"} }
elseif($Value -is [PSObject])
{
$Value |
Get-Member -MemberType Properties |
? Name -NotLike '\W' |
% Name |
% {"<$_>$(ConvertTo-XmlElements.ps1 $Value.$_)</$_>"}
}
elseif($Value -is [xml])
{ $Value.OuterXml }
else
{
$Value |
Get-Member -MemberType Properties |
? Name -NotLike '\W' |
% Name |
% {"<$_>$(ConvertTo-XmlElements.ps1 $Value.$_)</$_>"}
}
}