2017-04-18 128 views
0

我想從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選項卡。

回答

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內容。

+0

嗨,謝謝你的回覆。我之前嘗試過使用這個數組,但它沒有工作,每次都會覆蓋數據。再次我嘗試了你的方法,我得到覆蓋數據的錯誤。當我從$ Query + = $ Q中刪除+符號時,錯誤消失了。錯誤如下:無法將「get_performance_graph_csv_statistics」參數的「objects」與value:「System.Object []」轉換爲鍵入「iControl.SystemStatisticsPerformanceStatisticQuery []」:「無法轉換」System.Object []「 值類型「System.Object []」鍵入「iControl.SystemStatisticsPerformanceStatisticQuery」。「 – user2769144

+0

是的,你需要在腳本後面改變'$ Query'的方式,我會用一個建議更新答案。 – 2017-04-20 07:26:37

+0

在$ ReportData0和$ ReportData1中,我分別獲得活動連接和吞吐量。我不確定我現在如何整合一個電子表格上的數據。我可以打開兩個包含activecons和吞吐量內容的電子表格,但我需要在一個電子表格上添加它們。 – user2769144