Private/Invoke-RpcMethod.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
<#
.Synopsis
   Invoke XML_RPC method request.
.DESCRIPTION
   Invoke XML_RPC request to RPC server.
.EXAMPLE
   $blogUrl = 'http://www.pstips.net/myrpc.php'
   $method = New-RPCMethod -MethodName 'wp.getPostTypes' -Params @(1,'userName','password')
.OUTPUTS
   The response result from RPC server.
#>

function Invoke-RpcMethod {
    param(
        [uri]$RpcServerUri,
        [string]$RequestBody
    )
    $xmlResponse = Invoke-RestMethod -Uri $RpcServerUri -Method Post -Body $RequestBody
    if ($xmlResponse) {
        # Normal response
        $paramNodes = $xmlResponse.SelectNodes('methodResponse/params/param/value')
        if ($paramNodes) {
            $paramNodes | ForEach-Object {
                $value = $_.ChildNodes |
                    Where-Object { $_.NodeType -eq 'Element' } |
                    Select-Object -First 1
                ConvertFrom-RpcXmlObject -XmlObject $value
            }
        }

        # Fault response
        $faultNode = $xmlResponse.SelectSingleNode('methodResponse/fault')
        if ($faultNode) {
            $fault = ConvertFrom-RpcXmlObject -XmlObject $faultNode.value.struct
            return $fault
        }
    }
}