Private/ConvertTo-RpcXmlObject.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
<#
.Synopsis
   Convert object to XML-RPC object string.
.DESCRIPTION
   Convert object to XML-RPC object string.
.EXAMPLE
   ConvertTo-RpcXmlObject 3
   <int>3</int>
   ConvertTo-RpcXmlObject '3'
   <string>3</string>
   ConvertTo-RpcXmlObject 3.5
   <double>3.5</double>
.OUTPUTS
   The XML-RPC object string.
#>

function ConvertTo-RpcXmlObject {
    param(
        $Object
    )
    if ($Object -ne $null) {
        # integer type
        if ( ($Object -is [int]) -or ($Object -is [int64])) {
            return "<int>$Object</int>"
        }
        # double type
        elseif ( ($Object -is [float]) -or ($Object -is [double]) -or ($Object -is [decimal])) {
            return "<double>$Object</double>"
        }
        # string type
        elseif ( $Object -is [string]) {
            return "<string>$Object</string>"
        }
        # date/time type
        elseif ($Object -is [datetime]) {
            $dateStr = $Object.ToString('yyyyMMddTHH:mm:ss')
            return "<dateTime.iso8601>$dateStr</dateTime.iso8601>"
        }
        # boolean type
        elseif ($Object -is [bool]) {
            $bool = [int]$Object
            return "<boolean>$bool</boolean>"
        }
        # base64 type
        elseif ( ($Object -is [array]) -and ($Object.GetType().GetElementType() -eq [byte])) {
            $base64Str = [Convert]::ToBase64String($Object)
            return "<base64>$base64Str</base64>"
        }
        # array type
        elseif ( $Object -is [array]) {
            $result = '<array>
            <data>'

            foreach ($element in $Object) {
                $value = ConvertTo-RpcXmlObject -Object $element
                $result += "<value>{0}</value>" -f $value
            }
            $result += '</data>
            </array>'

            return $result
        }
        # struct type
        elseif ($Object -is [Hashtable]) {
            $result = '<struct>'
            foreach ($key in $Object.Keys) {
                $member = "<member>
                <name>{0}</name>
                <value>{1}</value>
                </member>"

                $member = $member -f $key, (ConvertTo-RpcXmlObject -Object $Object[$key])
                $result = $result + $member
            }
            $result = $result + '</struct>'
            return $result
        }
        elseif ($Object -is [PSCustomObject]) {
            $result = '<struct>'
            $Object |
                Get-Member -MemberType NoteProperty |
                ForEach-Object {
                $member = "<member>
                <name>{0}</name>
                <value>{1}</value>
                </member>"

                $member = $member -f $_.Name, (ConvertTo-RpcXmlObject -Object $Object.($_.Name))
                $result = $result + $member
            }
            $result = $result + '</struct>'
            return $result
        }
        else {
            throw "[$Object] type is not supported."
        }
    }
}