如何用新数据更新后台作业Windows图表对象?-Powershell



我有一个Powershell脚本,在启动时,它从几个平面文件中获取数据,对其进行解析,将其放入逗号分隔的方式中,然后将其放入内存中。然后,这些数据被传递到一个Windows图表对象(从这里下载(,并生成一个图表。

但是我如何自动刷新它呢?

我看过PoshBoard,但我只想在一个时间间隔内获取新数据,并更新现有的控件。

以下是我提出的逻辑:

  1. 获取数据(编码成功(
  2. 分析数据(编码成功(
  3. 创建和显示表单(编码成功(
  4. 更新所述表单不中断显示---在30分钟计时器上再次运行步骤1-2,并将其传递到图表中

请注意:我在后台作业上创建/显示表单,因此它在一个单独的线程上,否则".Show(("one_answers".ShowDialog((("表单会调用锁定脚本。这被用来监视gajillion美元系统上重要的内存使用统计数据,我不能让脚本锁定。

请原谅它的肮脏。这是脚本:

New-Item -ItemType f -Path c:/batch/lcontents.txt -Force
New-Item -ItemType f -Path c:/batch/gajilliondollarsystem.txt -Force
$form = $null
$zid = Read-Host "What is your ZID?"
$PW = read-host "What is your password?"
""
"If this hangs for more than 20 seconds, your credentials are wrong."
""
# Get contents of /import/sftw/vmstat_***.txt
c:batchplink.exe ***-admin -l $zid -pw $PW "cat /import/sftw/vmstat_***.txt" > c:batchlcontents.txt
""
"Credentials accepted."
""
# Select specific information from cat file for last 36 hours (Every half hour (72), 3 entries per (72x3 = 216)
Select-String -Path C:batchlcontents.txt -SimpleMatch "CDT", "Free memory", "Inactive memory" | select -last 216 > c:batchlcontentsnew.txt
# Extract contents of lcontentsnew into memory
$loglist = gc c:batchlcontentsnew.txt | ?{$_.trim() -ne ""} | %{ $_ -replace ".*txt:d+:"}
# rewrite in comma-delimited fashion
$count1 = 0
$count2 = 1
$count3 = 2
$gajilliondollarsystemlist = @()
do {
$gajilliondollarsystemlist += "$($loglist[$count1].trim()),$($loglist[$count2].trim()),$($loglist[$count3].trim())" 
$count1+=3 
$count2+=3 
$count3+=3
} 
until ($count1 -gt $loglist.count-3)
add-content c:batchgajilliondollarsystem.txt $gajilliondollarsystemlist
#Import-CSV for grouping purposes
$list = import-csv c:batchgajilliondollarsystem.txt -Delimiter "," -Header "Date","Inactive","Free"
$list | export-csv C:Batchgajilliondollarsystem.csv -Delimiter "," -NoTypeInformation

$ldate = $list.date
$linactive = ($list.inactive -replace " M inactive memory").trim()
$lfree = ($list.free -replace " M free memory").trim()
# Convert free and inactive to integers
$linactiveint = @()
$lfreeint = @()
foreach ($l in $linactive) {
$linactiveint += [int]$l
}
foreach ($l in $lfree) {
$lfreeint += [int]$l
}
#Combine free and inactive quantities into $lcombined
$count = 0
$lcombined = @()
do
{
$lcombined += ($linactiveint[$count] + $lfreeint[$count])
$count++
} 
until ($count -eq $lfreeint.count)
function GraphData {
# load the appropriate assemblies for charting
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
# create chart object 
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 900
$Chart.Height = 300
$Chart.Left = 0 
$Chart.Top = 0
# create a chartarea to draw on and add to chart 
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
$Chart.ChartAreas.Add($ChartArea)
# add data to chart 
[void]$Chart.Series.Add("Inactive") 
[void]$Chart.Series.Add("Free")
[void]$Chart.Series.Add("Combined")
$Chart.Series[0].Points.DataBindXY($args[0], $args[2])
$Chart.Series[1].Points.DataBindXY($args[0], $args[1])
$Chart.Series[2].Points.DataBindXY($args[0], $args[3])
# set chart type 
$Chart.Series[0].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line
$Chart.Series[1].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line
$Chart.Series[2].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Line
#format the lines
$chart.series[0].BorderWidth = 3
$chart.series[0].Color = "Blue"
$chart.series[1].BorderWidth = 3
$chart.series[1].Color = "Red"
$chart.series[2].BorderWidth = 3
$chart.series[2].Color = "Green"
# add title and axes labels 
[void]$Chart.Titles.Add("gajilliondollarsystem Memory Monitoring Trend (36 Hrs.)") 

# change chart area colour 
$Chart.BackColor = [System.Drawing.Color]::Transparent
# Legend Properties
$chart.legends.add("Inactive")
$chart.legends.add("Free")
$chart.legends.add("Combined")
#axis titles
$chart.chartareas.axisx.title = "Date"
$chart.chartareas.axisy.title = "Memory in MB"

# save chart to file 
$Chart.SaveImage($Env:USERPROFILE + "DesktopChart.png", "PNG")
# display the chart on a form 
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
                [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left 
$Form = New-Object Windows.Forms.Form 
$Form.Text = "PowerShell Chart $(get-date)" 
$Form.Width = 950 
$Form.Height = 325
$Form.controls.add($Chart) 
# $Form.Add_Shown({$Form.Activate()}) 
$form.startposition = "CenterScreen"
$Form.ShowDialog()
} 
#clear-host
Start-Job $function:GraphData -argumentlist @($ldate, $lfreeint, $linactiveint, $lcombined)
clear-host
Read-Host "Press ENTER to continue." 
get-job | %{remove-job $_ -Force}

据我所知,刷新图表上的数据;您需要先清除现有的点。

$Chart.Series[2].Points.Clear()
$Chart.Series[2].Points.DataBindXY($args[0], $args[3])

此外,请记住自动缩放Y轴

#auto-scale Y-axis
$Chart.ChartAreas.AxisY.Minimum = [Double].NaN
$Chart.ChartAreas.AxisY.Maximum = [Double].NaN

在尝试解决问题时涉及到另一个资源后,我发现这是一个简单得多的解决方案:在计时器上运行带有递归函数调用的永久do循环。工作起来很有魅力!谢谢@sqlchow的澄清。它有助于理解我真正刷新它后发生的一些问题。

$foo = $false
$form = $null
$zid = Read-Host "What is your ZID?"
$PW = Get-Credential -UserName $zid -Message "Please supply your password."
""
"If this hangs for more than 20 seconds, your credentials are wrong."
""
Do {
 # Do lots of stuff here to collect the data and parse it
function GraphData {
# Do the stuff here to graph the data, apply it to a form and display it
} 
#clear-host
Start-Job $function:GraphData -argumentlist @($ldate, $lfreeint, $linactiveint, $lcombined)
#Wait 30 minutes
start-sleep -s 1800
"Checking processes."
#grab process for window based on defined window title in GraphData
$proc = Get-Process | Where-Object {$_.MainWindowTitle -like "<TITLE>"}
#kill the process, closing the window
kill $proc.id
#clear background jobs
get-job | %{remove-job $_ -Force}
}
While ($foo -eq $false)

最新更新