PowerShell XML Multiple ChildNodes Comma Separated Strings to Array

Viewed 152

I have an API for a Sonus SBC that returns results in XML for analogue ports. I have three child nodes with the same number of values in each that I need to turn into an array/table. The problem is each node has a comma delimited string so I am left with three objects each containing a string like below:

ApplyToChannelList      1.1,1.2,1.3,1.4
ChannelOwnNumberList    71750,51375,53004,53146
FXSHotlineNumberList    53940,,,70309

I have tried trying to ConvertFrom-String but that creates three separate arrays that I can't work out how to join together. I have tried a For-Each that would not process anything and gave no result.

  • I have tried splitting the strings and adding them to a custom object.
  • I have tried splitting the strings and joining them together with commas to try and make an array.

My guess is I need to split the strings first and then get them into a custom object using possibly nested For-Each but when my first For-Each fails it makes it tricky.

To get the three nodes I need I am using the following:

$rest = Invoke-RestMethod -Uri $BackupUrl -Method GET -ContentType "text/html;charset=UTF-8" -Body $null -WebSession $ps -Verbose
$xml = ([xml]$rest.Trim())
$child = $xml.ChildNodes.Cassg | Select-Object ApplyToChannelList, ChannelOwnNumberList, FXSHotlineNumberList

Any suggestions or tips on what to type of commands to try is greatly appreciated.

Expected Output:

ApplyToChannelList ChannelOwnNumberList FXSHotlineNumberList
1.1                 71750               
1.2                 51375               56072

XML Output

id                              : 20001
href                            : https://hostname.dns/rest/cassg/20001
ActionSetTableId                : 0
ApplyToChannelList              : 1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.20,1.21,1.22,1.23,1.24,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.20
CallForward                     : 0
CallForwardActivate             : *72
CallForwardDeactivate           : *73
CallInformationFormatENM        : 0
CallerIDType                    : 0
CallerIdPrivacySignaling        : 
CasEnMProfileId                 : 0
CasLineType                     : 0
CasLoopStartFxoProfileId        : 0
CasLoopStartFxsProfileId        : CasLoopStartFxsProfileId
CasR2ProfileId                  : 0
CasSgDTMFCallerIDEndDelimiter   : 12
CasSgDTMFCallerIDStartDelimiter : 13
CasSgDirection                  : 2
CasSgENMDelimiterDigit          : 15
CasSgENMEndDigit                : 15
CasSgENMStartDigit              : 15
CasSgHuntMethod                 : 5
CasSgRingbackPlayPolicy         : 0
CasSignalingType                : 0
CasSupplementaryProfileId       : 0
ChannelOwnNumberList            : 71750,51375,53004,53146,53940,58940,70153,70309,71125,71681,75033,75021,75027,75015,75104,75120,75126,75145,75151,75320,75326,75351,75380,75449,52417,52895,75393,58627,53154,58473,51932,51871,53022,53595,19144,19152,19127,1
                                  9137,19101,19105,19112,19117,53410,54952
ConfigIEState                   : 1
Description                     : CAS FXS Analog Ports
EndOfDialDigit                  : 
FXSCallForwardingNumberList     : ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
FXSHotlineNumberList            : ,,,,,,,,,,56072,56072,56072,56072,56072,,,,56072,,,,,,,,,,,,,,,,,,,,,,,,,
NoChannelAvailableId            : 34
RelayCallingName                : 1
RouteTableId                    : RouteTableId
TimerSanitySetup                : 255000
ToneTableId                     : ToneTableId
customAdminState                : 1
rt_CasSgCurrentCalls            : 1
rt_CasSgIncomingCallsAttempted  : 197
rt_CasSgIncomingCallsConnected  : 143
rt_CasSgOutgoingCallsAttempted  : 22
rt_CasSgOutgoingCallsConnected  : 9
rt_CasSignalingType             : 0
1 Answers

Try with this, you would need to use $xml.ChildNodes.Cassg instead of $test in my case.

I'm not sure if you actually want to remove the leading commas from FXSHotlineNumberList, but in case you do, use this:

$fxsNumber = $test.FXSHotlineNumberList.TrimStart(',').Split(',')
# Since I don't have the original XML I'm using this to reproduce the object you already have.
$test = [pscustomobject]@{
    ApplyToChannelList = '1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,1.11,1.12,1.13,1.14,1.15,1.16,1.17,1.18,1.19,1.20,1.21,1.22,1.23,1.24,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,2.11,2.12,2.13,2.14,2.15,2.16,2.17,2.18,2.19,2.20'
    ChannelOwnNumberList = '71750,51375,53004,53146,53940,58940,70153,70309,71125,71681,75033,75021,75027,75015,75104,75120,75126,75145,75151,75320,75326,75351,75380,75449,52417,52895,75393,58627,53154,58473,51932,51871,53022,53595,19144,19152,19127,19137,19101,19105,19112,19117,53410,54952'
    FXSHotlineNumberList = ',,,,,,,,,,56072,56072,56072,56072,56072,,,,56072,,,,,,,,,,,,,,,,,,,,,,,,,'
}

$chnlist = $test.ApplyToChannelList.Split(',')
$chnOwnNumber = $test.ChannelOwnNumberList.Split(',')
$fxsNumber = $test.FXSHotlineNumberList.Split(',')

$count = ($chnlist.Count,$chnOwnNumber.Count,$fxsNumber.Count | Measure-Object -Maximum).Maximum

$result = for($i = 0; $i -lt $count; $i++)
{
    [pscustomobject]@{
        ApplyToChannelList = $chnlist[$i]
        ChannelOwnNumberList = $chnOwnNumber[$i]
        FXSHotlineNumberList = $fxsNumber[$i]
    }
}

Output

PS /> $result | Select-Object -First 20

ApplyToChannelList ChannelOwnNumberList FXSHotlineNumberList
------------------ -------------------- --------------------
1.1                71750                                    
1.2                51375                                    
1.3                53004                                    
1.4                53146                                    
1.5                53940                                    
1.6                58940                                    
1.7                70153                                    
1.8                70309                                    
1.9                71125                                    
1.10               71681                                    
1.11               75033                56072               
1.12               75021                56072               
1.13               75027                56072               
1.14               75015                56072               
1.15               75104                56072               
1.16               75120                                    
1.17               75126                                    
1.18               75145                                    
1.19               75151                56072               
1.20               75320                                    
Related