问题
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?
回答1:
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>
回答2:
this is close:
PS /home/nicholas>
PS /home/nicholas> $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 /home/nicholas>
PS /home/nicholas> $xmlRPCResponse | ConvertTo-Xml -As String <?xml version="1.0" encoding="utf-8"?>
<Objects>
<Object Type="System.Xml.XmlDocument">System.Xml.XmlDocument</Object>
</Objects>
PS /home/nicholas>
although not quite the desired output.
thanks to Mathias, and see:
Powershell: Convert XML to String
better result:
PS /home/nicholas>
PS /home/nicholas> $xmlRPCResponse.OuterXml
<?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><int>0</int></value></param></params></methodResponse>
PS /home/nicholas>
although kind of a useless example. seems to work.
来源:https://stackoverflow.com/questions/64874703/hello-world-for-powershell-send-xmlrcprequest