将两个对象的数据合并为一个电子表格

时间:2017-04-18 18:50:26

标签: powershell

我正在尝试从F5 LTM收集统计信息,当我传递一个对象(吞吐量)时,脚本工作正常,现在我修改了我的脚本并使用foreach循环从第二个对象(activecons)收集统计信息但我的数据是得到第二个对象的覆盖,我只从我的电子表格中的一个对象(activecons)获取统计数据。我不确定如何合并来自两个对象的数据并将其放在一个电子表格中。到目前为止,我已经尝试了以下

$stats = "throughput", "activecons"
foreach($s in $stats){
$Query = New-Object -TypeName iControl.SystemStatisticsPerformanceStatisticQuery
$Query.object_name = $s
$query.start_time = [int][double]::Parse((Get-Date -Date ((get-date).AddDays(-7)) -UFormat %s))
$Query.end_time = [int][double]::Parse((Get-Date -Date (get-date) -UFormat %s))
$Query.interval = 0
$Query.maximum_rows = 0
}
# Make method call passing in an array of size one with the specified query
$ReportData = $SystemStats.get_performance_graph_csv_statistics( (,$Query) )

# Look at the contents of the returned data.
$ReportData




# Allocate  a new encoder and turn the byte array into a string
$ASCII = New-Object -TypeName System.Text.ASCIIEncoding
$csvdata = $ASCII.GetString($ReportData[0].statistic_data)
# Look at the resulting dataset
$csvdata


# Replace commas with tabs in the report data and save to c:\temp\tabdata.txt
$csvdata.Replace(",", "`t`t") > c:\temp\tabdata1.txt
# Allocate an Excel application object and make it visible.
$e = New-Object -comobject "Excel.Application"
$e.visible = $true
# Load the tab delimited data into a workbook and get an instance of the worksheet it was inserted into.
$wb = $e.Workbooks.Open("c:\temp\tabdata1.txt")
$ws = $wb.WorkSheets.Item(1)
# Let's remove the first row of timestamps.  Ideally you'll want this to be the 
# horizontal axis and I'll leave it up to you to figure that one out.
$ws.Range("B2").Formula = "=(A2/86400)+25569+(-5/24)"
$ws.Range("B2").AutoFill($ws.Range("B2:B388"), 0)
$ws.Columns.Item(2).NumberFormat = "m/d/yyyy"
$ws.Columns.Item(3).Entirecolumn.Select()
$ws.Columns.Item(3).Entirecolumn.NumberFormat = "0"
$ws.Columns.Item(5).Entirecolumn.Select()
$ws.Columns.Item(5).Entirecolumn.NumberFormat = "0"
#$ws.Columns.Item(4).Entirecolumn.Select()
#$ws.Columns.Item(4).Entirecolumn.Delete()
$ws.Cells.Item(1,6) = 'AVERAGE'
$ws.Cells.Item(1,7) = 'MAXIMUM'
$ws.Cells.Item(3,6) = '=AVERAGE(C2:C388)'
$ws.Cells.Item(3,7) = '=MAX(C2:C388)'



# The last row of the data is filled with NaN to indicate the end of the result set. Let's delete that row.
$ws.Rows.Item($ws.UsedRange.Rows.Count).Select()
$ws.Rows.Item($ws.UsedRange.Rows.Count).Delete()

我不确定是否可以在一个电子表格中合并这些数据,或者我需要将第二个对象的数据放入新的Excel选项卡中。

1 个答案:

答案 0 :(得分:0)

除非我完全误读您的代码,否则在foreach($s in $stats)循环中您将对象分配给$Query,然后对其执行任何操作。然后循环再次运行并为$Query分配一个不同的对象,这导致您只在输出中获得(activecons)。

您可以尝试将$Query更改为数组,然后附加,可能会为您提供您期望的结果。

$stats = "throughput", "activecons"
$Query = @()

foreach($s in $stats){
    $Q = New-Object -TypeName iControl.SystemStatisticsPerformanceStatisticQuery
    $Q.object_name = $s
    $Q.start_time = [int][double]::Parse((Get-Date -Date ((get-date).AddDays(-7)) -UFormat %s))
    $Q.end_time = [int][double]::Parse((Get-Date -Date (get-date) -UFormat %s))
    $Q.interval = 0
    $Q.maximum_rows = 0
    $Query += $Q
}

现在$Query是一个数组,您需要稍后更改引用它的方式。我没有F5模块,所以我无法测试,但试试这个:

$ReportData0 = $SystemStats.get_performance_graph_csv_statistics($Query[0])
$ReportData1 = $SystemStats.get_performance_graph_csv_statistics($Query[1])

然后查看$ReportData0$ReportData1的内容。