我有一个返回以下信息的脚本
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
提供给数组的数据不是以这种方式呈现的;有人知道如何实现这样的事情吗?我可以发布整个脚本,但它现在是 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>
Powershell 脚本在 Java 位之后启动
$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
答案1
类似这样的事情应该有效:
$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 专家,所以我认为getIndex
可以改进。但是,根据您的源代码,它对我来说是有效的。
希望它能有所帮助!
编辑
以下是我将我的代码集成到您的代码中的方式:
首先我创建了$outarray
像你这样的(我认为),除了我使用了硬编码值(而不是解析 xml 文件):
$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
我发现您的实际代码存在问题:数字被解释为字符串,因此无法正确计算。您必须将它们转换为整数:
foreach($item in $finalxml){
...
...
$outarray+=New-object psobject -property @{
'Analog Phones' = [int]$item.analog_phones
'IP Phones' = [int]$item.ip_phones
'Location' = $location
}
}
答案2
从外观上看,您想将所有结果合并到每个站点的单个条目中?例如 - 每个位置的模拟电话总数和 IP 电话总数。
如果是这种情况,您应该能够遍历该数组。计算每个站点的模拟电话和 IP 电话总数,然后使用这些值填充新数组。
如果您正在寻找一些代码,我们就必须知道如何构造数组来构建逻辑。