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
|
Function Convert-CustomObjectToXml {
<#
.SYNOPSIS
Outputs a human readable simple text XML representation of a simple PS object.
.PARAMETER object
The input object to inspect and dump.
.PARAMETER depth
The number of levels deep to dump. Defaults to 1.
.PARAMETER rootEl
The name of the root element in the document. Defaults to "root"
.PARAMETER indentString
The string used to indent each level of XML. Defaults to two spaces.
Set to "" to remove indentation.
.DESCRIPTION
Outputs a human readable simple text XML representation of a simple PS object.
A PSObject with member types of NoteProperty will be dumped to XML. Only
nested PSObjects up to the depth specified will be searched. All other
note properties will be ouput using their strings values.
The output consists of node with property names and text nodes containing the
proprty value.
#>
param (
[PSCustomObject]$object,
[Int32]$depth = 1,
[String]$rootEl = "root",
[String]$indentString = " ",
[Int32]$indent = 1,
[switch]$isRoot = $true
)
# Output the root element opening tag
if ($isRoot) {
"<{0}>" -f $rootEl
}
# Iterate through all of the note properties in the object.
foreach ($prop in (Get-Member -InputObject $object -MemberType NoteProperty)) {
$child = $object.($prop.Name)
# Check if the property is an object and we want to dig into it
if ($child.GetType().Name -eq "PSCustomObject" -and $depth -gt 1) {
"{0}<{1}>" -f ($indentString * $indent), $prop.Name
Convert-CustomObjectToXml $child -isRoot:$false -indent ($indent + 1) -depth ($depth – 1) -indentString $indentString
"{0}</{1}>" -f ($indentString * $indent), $prop.Name
}
else {
# output the element or elements in the case of an array
foreach ($element in $child) {
"{0}<{1}>{2}</{1}>" -f ($indentString * $indent), $prop.Name, $element
}
}
}
# Output the root element closing tag
if ($isRoot) {
"</{0}>" -f $rootEl
}
}
Function Test-Convert-CustomObjectToXml {
# Create an example object and then try to dump it to the screen.
$a = New-Object PSObject
$a | Add-Member -MemberType NoteProperty -Name "CPU" -Value 4
$a | Add-Member -MemberType NoteProperty -Name "Memory" -Value 800
$a | Add-Member -MemberType NoteProperty -Name "NICs" -Value (New-Object PSObject)
$a.NICs | Add-Member -MemberType NoteProperty -Name "vmnic0" -Value "1000"
$a.NICs | Add-Member -MemberType NoteProperty -Name "vmnic1" -Value "1000"
$a.NICs | Add-Member -MemberType NoteProperty -Name "vmnic2" -Value "1000"
$a.NICs | Add-Member -MemberType NoteProperty -Name "vmnic3" -Value "1000"
$a | Add-Member -MemberType NoteProperty -Name "VMs" -Value (New-Object PSObject)
$a.VMs | Add-Member -MemberType NoteProperty -Name "VM" -Value @()
$a.VMs.VM += "foo"
$a.VMs.VM += "bar"
$a.VMs.VM += "bash"
$a.VMs.VM += "bang"
Convert-CustomObjectToXml $a
Convert-CustomObjectToXml $a -depth 10 -rootEl "config"
}
|