powershellsoapinvoke-restmethodcucm

Parsing Invoke-Restmethod XML SOAP Request (Risport70 CUCM if that helps)


I've run into a weird scenario when trying to pull real time info on phones. I can grab the data just fine and drill down into it but run into an issue I'm not sure how to work around.

Here's a sample XML response Cisco provides. After <ns1:CMNodes> in powershell, I cant drill down further or search the nodes. It turns all the fields underneath into essentially one big string.

enter image description here

Heres my powershell code to invoke the rest-method

$RisBody = "<soapenv:Envelope xmlns:soapenv=`"http://schemas.xmlsoap.org/soap/envelope/`" xmlns:soap=`"http://schemas.cisco.com/ast/soap`">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:selectCmDevice>
         <soap:StateInfo></soap:StateInfo>
         <soap:CmSelectionCriteria>
            <soap:MaxReturnedDevices>1000</soap:MaxReturnedDevices>
            <soap:DeviceClass>Phone</soap:DeviceClass>
            <soap:Model>255</soap:Model>
            <soap:Status>Any</soap:Status>
            <soap:NodeName></soap:NodeName>
            <soap:SelectBy>Name</soap:SelectBy>
            <soap:SelectItems>
               <soap:item>
                  <soap:Item>SEPABCDEF123456</soap:Item>
               </soap:item>
            </soap:SelectItems>
            <soap:Protocol>Any</soap:Protocol>
            <soap:DownloadStatus>Any</soap:DownloadStatus>
         </soap:CmSelectionCriteria>
      </soap:selectCmDevice>
   </soapenv:Body>
</soapenv:Envelope>"

$RisRequest = Invoke-RestMethod 'https://CUCM:8443/realtimeservice2/services/RISService70?wsdl' -Method 'POST' -Headers $headers -Body $RisBody

To drill down all the way to CMNodes, I use $RisInfo.Envelope.Body.selectCmDeviceResponse.selectCmDeviceReturn.SelectCmDeviceResult.cmnodes

Heres a look into the output I'm given (removed sensitive info for InnerXML and OuterXML but the values above in the cisco picture all smushed together and unreadable)

Name            : ns1:CmNodes
LocalName       : CmNodes
NamespaceURI    : http://schemas.cisco.com/ast/soap
Prefix          : ns1
NodeType        : Element
ParentNode      : SelectCmDeviceResult
OwnerDocument   : #document
IsEmpty         : False
Attributes      : {}
HasAttributes   : False
SchemaInfo      : System.Xml.XmlName
InnerXml        : Phone INFO
NextSibling     : 
PreviousSibling : TotalDevicesFound
Value           : 
ChildNodes      : {Cucm1, cucm2, cucm3, cucm4}
FirstChild      : item
LastChild       : item
HasChildNodes   : True
IsReadOnly      : False
OuterXml        : Phone INFO
BaseURI         : 
PreviousText    : 

The results I want to look at our inner/outer xml but they are "strings" (via getType()) which makes it impossible to search via select node. Am I just going to have to parse it out the rough way?

I've tried using invoke-webrequest as well to see if it made any difference but got the same result. Also tried to convert inner/outerxml to [xml] but usually would fail or give me the same structure of string output. Seems like the RisPort70 just puts everything to strings after CMNodes.

Thank you!

Edit-Added .innerxml raw code for view.

    <ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soap"><ns1:ReturnCode>NotFound</ns1:ReturnCode><ns1:Name>IPAddress1</ns1:Name><ns1:NoChange>false</
ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.co
m/ast/soap"><ns1:ReturnCode>Ok</ns1:ReturnCode><ns1:Name>IPAddress1</ns1:Name><ns1:NoChange>false</ns1:NoChange><ns1:CmDevices><ns1:item><ns1:Name>SE
PDCC36115199A</ns1:Name><ns1:DirNumber>0435-Registered,0432-Registered</ns1:DirNumber><ns1:DeviceClass>Phone</ns1:DeviceClass><ns1:Model>683</ns1:Model
><ns1:Product>568</ns1:Product><ns1:BoxProduct>0</ns1:BoxProduct><ns1:Httpd>Yes</ns1:Httpd><ns1:RegistrationAttempts>0</ns1:RegistrationAttempts><ns1:I
sCtiControllable>true</ns1:IsCtiControllable><ns1:LoginUserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:Status>Registere
d</ns1:Status><ns1:StatusReason>0</ns1:StatusReason><ns1:PerfMonObject>2</ns1:PerfMonObject><ns1:DChannel>0</ns1:DChannel><ns1:Description>John Doe</
ns1:Description><ns1:H323Trunk><ns1:ConfigName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:TechPrefix xmlns:xsi="http://ww
w.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:Zone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:RemoteCmServer1 xmln
s:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:RemoteCmServer2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" 
/><ns1:RemoteCmServer3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:AltGkList xmlns:xsi="http://www.w3.org/2001/XMLSchema-i
nstance" xsi:nil="1" /><ns1:ActiveGk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><ns1:CallSignalAddr xmlns:xsi="http://www.w3.o
rg/2001/XMLSchema-instance" xsi:nil="1" /><ns1:RasAddr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:H323Trunk><ns1:TimeSta
mp>1662497967</ns1:TimeStamp><ns1:Protocol>SIP</ns1:Protocol><ns1:NumOfLines>2</ns1:NumOfLines><ns1:LinesStatus><ns1:item><ns1:DirectoryNumber>0432</ns
1:DirectoryNumber><ns1:Status>Registered</ns1:Status></ns1:item><ns1:item><ns1:DirectoryNumber>0432</ns1:DirectoryNumber><ns1:Status>Registered</ns1:St
atus></ns1:item></ns1:LinesStatus><ns1:ActiveLoadID>sip88xx.14-1-1-0001-125</ns1:ActiveLoadID><ns1:InactiveLoadID>sip88xx.12-7-1-0001-393</ns1:Inactive
LoadID><ns1:DownloadStatus>Successful</ns1:DownloadStatus><ns1:DownloadFailureReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" 
/><ns1:DownloadServer>IPAddress7</ns1:DownloadServer><ns1:IPAddress><ns1:item><ns1:IP>IPAddress6</ns1:IP><ns1:IPAddrType>ipv4</ns1:IPAddrType><ns1:
Attribute>Unknown</ns1:Attribute></ns1:item></ns1:IPAddress></ns1:item></ns1:CmDevices></ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soa
p"><ns1:ReturnCode>NotFound</ns1:ReturnCode><ns1:Name>IPAddress3</ns1:Name><ns1:NoChange>false</ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.
org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soap"><ns1:ReturnCode>NotFound</ns1:ReturnCode>
<ns1:Name>IPAddress4</ns1:Name><ns1:NoChange>false</ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /><
/ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soap"><ns1:ReturnCode>NotFound</ns1:ReturnCode><ns1:Name>IPAddress5</ns1:Name><ns1:NoChan
ge>true</ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:item><ns1:item xmlns:ns1="http://schemas
.cisco.com/ast/soap"><ns1:ReturnCode>NotFound</ns1:ReturnCode><ns1:Name>IPAddress8</ns1:Name><ns1:NoChange>true</ns1:NoChange><ns1:CmDevices xmlns:xs
i="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soap"><ns1:ReturnCode>NotFound
</ns1:ReturnCode><ns1:Name>IPAddress9</ns1:Name><ns1:NoChange>false</ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
" xsi:nil="1" /></ns1:item><ns1:item xmlns:ns1="http://schemas.cisco.com/ast/soap"><ns1:ReturnCode>NotFound</ns1:ReturnCode><ns1:Name>IPAddress10</ns1
:Name><ns1:NoChange>false</ns1:NoChange><ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" /></ns1:item>

Solution

  • Here is sample code using Xml Linq

    using assembly System
    using assembly System.Collections
    using assembly System.Xml.Linq
    
    $filename = "c:\temp\test.xml"
    $doc = [System.Xml.Linq.XDocument]::Load($filename)
    $table = [System.Collections.ArrayList]::new()
    
    $CmNodes = $doc.Descendants().Where( {$_.Name.LocalName -eq "CmNodes"})[0]
    $ns1 = $CmNodes.GetNamespaceOfPrefix("ns1")
    foreach($item in $CmNodes.Elements($ns1 + "item"))
    {
       $newRow = New-Object -TypeName psobject
    
       foreach($node in $item.Descendants())
       {
          $tagName = $node.Name.LocalName
          if(($node.Value.Length -gt 0) -and (-not $node.HasElements))
          {
             # do not add name if already exists
             if($newRow.$tagName -eq $null)
             {
                $newRow | Add-Member -NotePropertyName $tagName -NotePropertyValue $node.Value
             }
          }
       }
    
       $table.Add($newRow)  | Out-Null
    }
    $table | Format-List
    

    Here is the xml I used

    <soapenv:Envelope xmlns:soapenv="http://schemas/xmlsoap.org/soap/envelope/">
      <soapenv:Body>
        <ns1:selectCmDeviceResponse xmlns:ns1="http://schemas.cisco.com/est/soap">
          <ns1:selectCmDeviceReturn>
            <ns1:selectCmDeviceResult>
              <ns1:TotalDevicesFound>1</ns1:TotalDevicesFound>
              <ns1:CmNodes>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress1</ns1:Name>
                  <ns1:NoChange>false</ns1:NoChange>
                  <ns1:CmDevices>
                    <ns1:item>
                      <ns1:Name>SEPDCC36115199A</ns1:Name>
                      <ns1:DirNumber>0435-Registered,0432-Registered</ns1:DirNumber>
                      <ns1:DeviceClass>Phone</ns1:DeviceClass>
                      <ns1:Model>683</ns1:Model
    >
                      <ns1:Product>568</ns1:Product>
                      <ns1:BoxProduct>0</ns1:BoxProduct>
                      <ns1:Httpd>Yes</ns1:Httpd>
                      <ns1:RegistrationAttempts>0</ns1:RegistrationAttempts>
                      <ns1:IsCtiControllable>true</ns1:IsCtiControllable>
                      <ns1:LoginUserId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                      <ns1:Status>Registered</ns1:Status>
                      <ns1:StatusReason>0</ns1:StatusReason>
                      <ns1:PerfMonObject>2</ns1:PerfMonObject>
                      <ns1:DChannel>0</ns1:DChannel>
                      <ns1:Description>John Doe</ns1:Description>
                      <ns1:H323Trunk>
                        <ns1:ConfigName xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:TechPrefix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:Zone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:RemoteCmServer1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:RemoteCmServer2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1"/>
                        <ns1:RemoteCmServer3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:AltGkList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:ActiveGk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:CallSignalAddr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                        <ns1:RasAddr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                      </ns1:H323Trunk>
                      <ns1:TimeStamp>1662497967</ns1:TimeStamp>
                      <ns1:Protocol>SIP</ns1:Protocol>
                      <ns1:NumOfLines>2</ns1:NumOfLines>
                      <ns1:LinesStatus>
                        <ns1:item>
                          <ns1:DirectoryNumber>0432</ns1:DirectoryNumber>
                          <ns1:Status>Registered</ns1:Status>
                        </ns1:item>
                        <ns1:item>
                          <ns1:DirectoryNumber>0432</ns1:DirectoryNumber>
                          <ns1:Status>Registered</ns1:Status>
                        </ns1:item>
                      </ns1:LinesStatus>
                      <ns1:ActiveLoadID>sip88xx.14-1-1-0001-125</ns1:ActiveLoadID>
                      <ns1:InactiveLoadID>sip88xx.12-7-1-0001-393</ns1:InactiveLoadID>
                      <ns1:DownloadStatus>Successful</ns1:DownloadStatus>
                      <ns1:DownloadFailureReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1"/>
                      <ns1:DownloadServer>IPAddress7</ns1:DownloadServer>
                      <ns1:IPAddress>
                        <ns1:item>
                          <ns1:IP>IPAddress6</ns1:IP>
                          <ns1:IPAddrType>ipv4</ns1:IPAddrType>
                          <ns1:Attribute>Unknown</ns1:Attribute>
                        </ns1:item>
                      </ns1:IPAddress>
                    </ns1:item>
                  </ns1:CmDevices>
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress3</ns1:Name>
                  <ns1:NoChange>false</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress4</ns1:Name>
                  <ns1:NoChange>false</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress5</ns1:Name>
                  <ns1:NoChange>true</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress8</ns1:Name>
                  <ns1:NoChange>true</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress9</ns1:Name>
                  <ns1:NoChange>false</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
                <ns1:item>
                  <ns1:ReturnCode>NotFound</ns1:ReturnCode>
                  <ns1:Name>IPAddress10</ns1:Name>
                  <ns1:NoChange>false</ns1:NoChange>
                  <ns1:CmDevices xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="1" />
                </ns1:item>
              </ns1:CmNodes>
            </ns1:selectCmDeviceResult>
          </ns1:selectCmDeviceReturn>
        </ns1:selectCmDeviceResponse>
      </soapenv:Body>
    </soapenv:Envelope>
    

    Here is the results

    ReturnCode           : NotFound
    Name                 : IPAddress1
    NoChange             : false
    DirNumber            : 0435-Registered,0432-Registered
    DeviceClass          : Phone
    Model                : 683
    Product              : 568
    BoxProduct           : 0
    Httpd                : Yes
    RegistrationAttempts : 0
    IsCtiControllable    : true
    Status               : Registered
    StatusReason         : 0
    PerfMonObject        : 2
    DChannel             : 0
    Description          : John Doe
    TimeStamp            : 1662497967
    Protocol             : SIP
    NumOfLines           : 2
    DirectoryNumber      : 0432
    ActiveLoadID         : sip88xx.14-1-1-0001-125
    InactiveLoadID       : sip88xx.12-7-1-0001-393
    DownloadStatus       : Successful
    DownloadServer       : IPAddress7
    IP                   : IPAddress6
    IPAddrType           : ipv4
    Attribute            : Unknown
    
    ReturnCode : NotFound
    Name       : IPAddress3
    NoChange   : false
    
    ReturnCode : NotFound
    Name       : IPAddress4
    NoChange   : false
    
    ReturnCode : NotFound
    Name       : IPAddress5
    NoChange   : true
    
    ReturnCode : NotFound
    Name       : IPAddress8
    NoChange   : true
    
    ReturnCode : NotFound
    Name       : IPAddress9
    NoChange   : false
    
    ReturnCode : NotFound
    Name       : IPAddress10
    NoChange   : false