Powershell 将列表输出转换为表(对象)
从iperf3中,我过滤了以下输出:Powershell 将列表输出转换为表(对象),powershell,object,Powershell,Object,从iperf3中,我过滤了以下输出: & $exe -c my_host | Select-Object -Index (2..12) [ ID] Interval Transfer Bandwidth [ 4] 0.00-1.00 sec 11.4 MBytes 95.4 Mbits/sec [ 4] 1.00-2.00 sec 11.2 MBytes 94.2 Mbits/sec
& $exe -c my_host | Select-Object -Index (2..12)
[ ID] Interval Transfer Bandwidth
[ 4] 0.00-1.00 sec 11.4 MBytes 95.4 Mbits/sec
[ 4] 1.00-2.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 2.00-3.00 sec 11.2 MBytes 94.3 Mbits/sec
[ 4] 3.00-4.00 sec 11.2 MBytes 94.5 Mbits/sec
[ 4] 4.00-5.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 5.00-6.00 sec 11.2 MBytes 94.4 Mbits/sec
[ 4] 6.00-7.00 sec 11.2 MBytes 94.4 Mbits/sec
[ 4] 7.00-8.00 sec 11.2 MBytes 94.3 Mbits/sec
[ 4] 8.00-9.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 9.00-10.00 sec 11.2 MBytes 94.5 Mbits/sec
如何将其转换为powershell中的表/对象以进行进一步处理?使用以下方法:
看起来您已经有了一个字符串数组作为输出,这将生成一个固定宽度的表 下面我用一个Here字符串来模拟这个数组
$output = @"
[ ID] Interval Transfer Bandwidth
[ 4] 0.00-1.00 sec 11.4 MBytes 95.4 Mbits/sec
[ 4] 1.00-2.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 2.00-3.00 sec 11.2 MBytes 94.3 Mbits/sec
[ 4] 3.00-4.00 sec 11.2 MBytes 94.5 Mbits/sec
[ 4] 4.00-5.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 5.00-6.00 sec 11.2 MBytes 94.4 Mbits/sec
[ 4] 6.00-7.00 sec 11.2 MBytes 94.4 Mbits/sec
[ 4] 7.00-8.00 sec 11.2 MBytes 94.3 Mbits/sec
[ 4] 8.00-9.00 sec 11.2 MBytes 94.2 Mbits/sec
[ 4] 9.00-10.00 sec 11.2 MBytes 94.5 Mbits/sec
"@ -split '\r?\n'
$result = for ($i = 1; $i -lt $output.Count; $i++) {
if ($output[$i] -match '^(?<id>.{6})(?<interval>.{19})(?<transfer>.{13})(?<bandwidth>.*)') {
[PsCustomObject]@{
'ID' = $matches['id'].Trim('[] ')
'Interval' = $matches['interval'].Trim()
'Transfer' = $matches['transfer'].Trim()
'BandWidth' = $matches['bandwidth'].Trim()
}
}
}
# output on screen
$result
#output to CSV file
$result | Export-Csv -Path 'X:\table.csv' -NoTypeInformation
$output=@”
[ID]间隔传输带宽
[4]0.00-1.00秒11.4兆字节95.4兆字节/秒
[4]1.00-2.00秒11.2兆字节94.2兆字节/秒
[4]2.00-3.00秒11.2兆字节94.3兆字节/秒
[4]3.00-4.00秒11.2兆字节94.5兆字节/秒
[4]4.00-5.00秒11.2兆字节94.2兆字节/秒
[4]5.00-6.00秒11.2兆字节94.4兆字节/秒
[4]6.00-7.00秒11.2兆字节94.4兆字节/秒
[4]7.00-8.00秒11.2兆字节94.3兆字节/秒
[4]8.00-9.00秒11.2兆字节94.2兆字节/秒
[4]9.00-10.00秒11.2兆字节94.5兆字节/秒
“@-split”\r?\n”
$result=for($i=1;$i-lt$output.Count;$i++){
if($output[$i]-match'^(?{6})(?{19})(?{13})(?.*)){
[PsCustomObject]@{
'ID'=$matches['ID']。修剪('[]'))
'Interval'=$matches['Interval'].Trim()
'Transfer'=$matches['Transfer'].Trim()
“带宽”=$matches[“带宽”]。Trim()
}
}
}
#屏幕上的输出
$result
#输出到CSV文件
$result |导出Csv-路径'X:\table.Csv'-NoTypeInformation
结果:
ID Interval Transfer BandWidth
-- -------- -------- ---------
4 0.00-1.00 sec 11.4 MBytes 95.4 Mbits/sec
4 1.00-2.00 sec 11.2 MBytes 94.2 Mbits/sec
4 2.00-3.00 sec 11.2 MBytes 94.3 Mbits/sec
4 3.00-4.00 sec 11.2 MBytes 94.5 Mbits/sec
4 4.00-5.00 sec 11.2 MBytes 94.2 Mbits/sec
4 5.00-6.00 sec 11.2 MBytes 94.4 Mbits/sec
4 6.00-7.00 sec 11.2 MBytes 94.4 Mbits/sec
4 7.00-8.00 sec 11.2 MBytes 94.3 Mbits/sec
4 8.00-9.00 sec 11.2 MBytes 94.2 Mbits/sec
4 9.00-10.00 sec 11.2 MBytes 94.5 Mbits/sec
ID间隔传输带宽
-- -------- -------- ---------
4 0.00-1.00秒11.4兆字节95.4兆字节/秒
4 1.00-2.00秒11.2兆字节94.2兆字节/秒
4 2.00-3.00秒11.2兆字节94.3兆字节/秒
4 3.00-4.00秒11.2兆字节94.5兆字节/秒
4.00-5.00秒11.2兆字节94.2兆字节/秒
4 5.00-6.00秒11.2兆字节94.4兆字节/秒
4 6.00-7.00秒11.2兆字节94.4兆字节/秒
4 7.00-8.00秒11.2兆字节94.3兆字节/秒
4 8.00-9.00秒11.2兆字节94.2兆字节/秒
4 9.00-10.00秒11.2兆字节94.5兆字节/秒
有一个cmdlet格式表可在表中输出结果。此cmdlet仅适用于其他内部cmdlet、哈希表、PSObject和其他数据结构,但不适用于外部程序。我们必须习惯性地输出它。没有cmdlet可以直接为外部程序执行此操作。我刚刚向cmdlet添加了一个新的
-ommit
参数,该参数允许您从标头和数据中省略某些(特殊)字符(每个省略的字符都将被替换为空格,并最终修剪键和值)。我已相应地更新了答案。
ID Interval Transfer BandWidth
-- -------- -------- ---------
4 0.00-1.00 sec 11.4 MBytes 95.4 Mbits/sec
4 1.00-2.00 sec 11.2 MBytes 94.2 Mbits/sec
4 2.00-3.00 sec 11.2 MBytes 94.3 Mbits/sec
4 3.00-4.00 sec 11.2 MBytes 94.5 Mbits/sec
4 4.00-5.00 sec 11.2 MBytes 94.2 Mbits/sec
4 5.00-6.00 sec 11.2 MBytes 94.4 Mbits/sec
4 6.00-7.00 sec 11.2 MBytes 94.4 Mbits/sec
4 7.00-8.00 sec 11.2 MBytes 94.3 Mbits/sec
4 8.00-9.00 sec 11.2 MBytes 94.2 Mbits/sec
4 9.00-10.00 sec 11.2 MBytes 94.5 Mbits/sec