Search code examples
xmlpowershell.net-corexml-rpcread-eval-print-loop

Hello World for Powershell Send-XmlRcpRequest?


Desired output something like:

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
        <value><string>South Dakota</string></value>
    </param>
  </params>
</methodResponse>

How do I use the xml-rpc powershell module to view the response?

Perhaps it's possible to "cat" or somehow pipe the result from baidu to something that will print it? Or, put it into an object?

PS /home/nicholas/powershell> 
PS /home/nicholas/powershell> Send-XmlRpcRequest -Url "http://ping.baidu.com/ping/RPC2" -MethodName "weblogUpdates.extendedPing" -Params @('jdon')                             

xml                            methodResponse
---                            --------------
version="1.0" encoding="UTF-8" methodResponse

PS /home/nicholas/powershell> 

or the more full request:

Send-XmlRpcRequest -Url "http://ping.baidu.com/ping/RPC2" -MethodName "weblogUpdates.extendedPing" -Params @('jdon','http://www.jdon.com/','http://www.jdon.com/47686','http://www.jdon.com/rss')  

generates a similar result. Something like:

$http = New-Object Chilkat.Http

$xmlReq = "<?xml version=`"1.0`"?><methodCall><methodName>demo.sayHello</methodName><params /></methodCall>"

$xmlResponse = $http.XmlRpc("http://www.cknotes.com/xmlrpc.php",$xmlReq)
if ($http.LastMethodSuccess -ne $true) {
    $($http.LastErrorText)
    exit
}

$($xmlResponse)

except, with this module, where is the response?


Solution

  • So your question is really "How do I convert an xml document to a string with nice and readable formatting" - the answer to which might look something like this:

    function Convert-XmlToString
    {
      param(
        [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [xml]$Xml
      )
    
      begin {
        # Create formatting settings
        $writerSettings = [System.Xml.XmlWriterSettings]@{Indent = $true; OmitXmlDeclaration = $true}
    
        # Create StringBuilder
        $stringBuilder = [System.Text.StringBuilder]::new()
      }
    
      process {
        try {
          # Create XmlWriter
          $xmlWriter = [System.Xml.XmlTextWriter]::Create($stringBuilder, $writerSettings)
    
          # Write xml document to XmlWriter
          $xml.WriteContentTo($xmlWriter)
    
          # Flush XmlWriter to output target (ie. the stringbuilder)
          $xmlWriter.Flush()
    
          # Output resulting string
          $stringBuilder.ToString()
        }
        finally{
          # Clean up
          $xmlWriter.Dispose()
          $stringBuilder = $stringBuilder.Clear()
        }
      }
    }
    

    With which you should be able to do something like:

    PS C:\> $xmlRPCResponse = Send-XmlRpcRequest -Url "http://ping.baidu.com/ping/RPC2" -MethodName "weblogUpdates.extendedPing" -Params @('jdon','http://www.jdon.com/','http://www.jdon.com/47686','http://www.jdon.com/rss')  
    PS C:\> $xmlRPCResponse |Convert-XmlToString
    <methodResponse>
      <params>
        <param>
            <value><string>South Dakota</string></value>
        </param>
      </params>
    </methodResponse>