我有一个脚本,返回以下信息
Analog Phones IP Phones Location ------------- --------- -------- 0 883 Site1 413 0 Site1 0 4 Site1 0 258 Site2 9 75 Site2 183 0 Site2
这个信息存储在一个数组$ arrayX中
我想弄清楚如何让输出显示
Analog Phones IP Phones Location ------------- --------- -------- 413 887 Site1 192 333 Site2
馈送arrays的数据不是以这种方式呈现的; 有没有人有任何想法如何完成这样的事情? 我可以发布整个脚本,但现在是java,axl和powershell的混杂。
脚本中的数据看起来像这样
Analog Phones IP Phones Location ------------- --------- -------- 413 0 Site1-AGW-DP 0 883 Site1-PHONES-DP 0 4 Site1-PHSRST-DP
正在使用的XML文件是从java axl程序输出的。 其中一个的结果如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <axl:executeSQLQueryResponse xmlns:axl="http://www.cisco.com/AXL/API/6.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" sequence="1394831301468"> <return> <row> <analog_phones>0</analog_phones> <ip_phones>47</ip_phones> <devicepool>Site20-PHONES-DP</devicepool> </row> <row> <analog_phones>533</analog_phones> <ip_phones>0</ip_phones> <devicepool>Site20-AGW-DP</devicepool> </row> <row> <analog_phones>1</analog_phones> <ip_phones>689</ip_phones> <devicepool>Site20-PHSRST-DP</devicepool> </row> </return> </axl:executeSQLQueryResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
在java之后启动Powershell脚本
$files = (Get-ChildItem .\*out.xml).name foreach ($file in $files){ [xml]$xmlfiles = gc $file $finalxml += $xmlfiles.envelope.body.executeSQLQueryResponse.return.row Remove-Item $pwd\$file } $outarray = @() $sitelist = import-csv sites.csv foreach($item in $finalxml){ if(!$item.devicepool){$sed="BLANK"} else{$sed=($item.devicepool).substring(0,4)} $location=($sitelist | where-object {$_.prefix -eq $sed}).Facility if(!$location){$location=$sed} $outarray+=New-object psobject -property @{ 'Analog Phones' = $item.analog_phones 'IP Phones' = $item.ip_phones 'Location' = $location } } $outarray | Sort-Object 'Analog Phones','IP PHones' -unique | sort-object Location | select-object 'Analog Phones','IP Phones',Location |export-csv $csvout
像这样的东西应该工作:
$result = @() Function getIndex($location) { for($j = 0; $j -lt $result.Count; $j++) { if($result[$j].Location -contains "$location") { $index=$j break; } } return $index } for ($i=0; $i -lt $outarray.length; $i++) { $loc=$outarray[$i].Location $aphones=$outarray[$i].'Analog Phones' $ipphones=$outarray[$i].'IP Phones' if(!($result | where-object {$_.Location -eq "$loc"})) { $result+=New-object psobject -property @{'Location' = "$loc"; 'Analog Phones' = $aphones; 'IP Phones' = $ipphones} } else { $index=getIndex($loc) $result[$index].'Analog Phones' = $result[$index].'Analog Phones' + $aphones $result[$index].'IP Phones' = $result[$index].'IP Phones' + $ipphones } } $result
$result输出是:
Analog Phones IP Phones Location ------------- --------- -------- 413 887 Site1 192 333 Site2
这是逻辑:
Define an empty $result array to store final results Iterate over $outarray each time a location is found if location does not exists in $result then add full line from $outarray into $result for this location if location already exists in $result then retrieve the index for the given location in $result update Analog Phones number in $result with values from $outarray + $result for the given location (index) update IP Phones number in $result with values from $outarray + $result for the given location (index)
PS:我不是Powershell Guru,所以我认为getIndex可以改进。 不过,它基于你的源代码为我工作。
希望它会帮助!
编辑
以下是我的代码整合到你的方式:
首先,我创build了像你这样的$outarray (我认为),除了我使用硬编码的值(不parsingXML文件):
$outarray = @() $outarray+=New-object psobject -property @{'Analog Phones' = 0;'IP Phones' = 883;'Location' = "Site1"} $outarray+=New-object psobject -property @{'Analog Phones' = 413;'IP Phones' = 0;'Location' = "Site1"} $outarray+=New-object psobject -property @{'Analog Phones' = 0;'IP Phones' = 4;'Location' = "Site1"} $outarray+=New-object psobject -property @{'Analog Phones' = 0;'IP Phones' = 258;'Location' = "Site2"} $outarray+=New-object psobject -property @{'Analog Phones' = 9;'IP Phones' = 75;'Location' = "Site2"} $outarray+=New-object psobject -property @{'Analog Phones' = 183;'IP Phones' = 0;'Location' = "Site2"}
从这一点来看,我的$outarray看起来像这样:
Analog Phones IP Phones Location ------------- --------- -------- 0 883 Site1 413 0 Site1 0 4 Site1 0 258 Site2 9 75 Site2 183 0 Site2
然后我在你的export-csv命令后面添加了我的代码:
$outarray | Sort-Object............ MY CODE HERE
编辑2
我发现你的实际代码有问题:数字被解释为string,所以他们不能正确计算。 你必须将它们转换成整数:
foreach($item in $finalxml){ ... ... $outarray+=New-object psobject -property @{ 'Analog Phones' = [int]$item.analog_phones 'IP Phones' = [int]$item.ip_phones 'Location' = $location } }
从它的外观来看,你正试图把所有的结果合并成一个单一的网站? 例如 – 每个位置的总模拟电话和总IP电话。
如果是这样的话,你应该可以遍历该数组。 每个站点的模拟电话和IP电话总数,然后使用这些值来填充一个新的arrays。
如果你正在寻找一些代码,我们将不得不知道如何构build数组来构build逻辑。