#------------------------------------------------------------------------ # Source File Information (DO NOT MODIFY) # Source ID: 4c5b8b5b-c4ce-4799-abce-f719d26b2a23 # Source File: Process Chart.psf #------------------------------------------------------------------------ <# .NOTES -------------------------------------------------------------------------------- Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2014 v4.1.62 Generated on: 6/18/2014 3:44 PM Generated by: David Organization: SAPIEN Technologies, Inc. -------------------------------------------------------------------------------- .DESCRIPTION GUI script generated by PowerShell Studio 2014 #> #---------------------------------------------- #region Application Functions #---------------------------------------------- function OnApplicationLoad { #Note: This function is not called in Projects #Note: This function runs before the form is created #Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path #Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList) #Important: Form controls cannot be accessed in this function #TODO: Add snapins and custom code to validate the application load if([Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") -eq $null) { #Microsoft Chart Controls are not installed [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") [void][System.Windows.Forms.MessageBox]::Show("Microsoft Chart Controls for Microsoft .NET 3.5 Framework is required","Microsoft Chart Controls Required") #Open the URL [System.Diagnostics.Process]::Start("http://www.microsoft.com/downloads/en/details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=en"); return $false } return $true #return true for success or false for failure } function OnApplicationExit { #Note: This function is not called in Projects #Note: This function runs after the form is closed #TODO: Add custom code to clean up and unload snapins when the application exits $script:ExitCode = 0 #Set the exit code for the Packager } #endregion Application Functions #---------------------------------------------- # Generated Form Function #---------------------------------------------- function Call-Process_Chart_psf { #---------------------------------------------- #region Import the Assemblies #---------------------------------------------- [void][reflection.assembly]::Load('mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') [void][reflection.assembly]::Load('System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') [void][reflection.assembly]::Load('System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') [void][reflection.assembly]::Load('System.Windows.Forms.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35') #endregion Import Assemblies #---------------------------------------------- #region Generated Form Objects #---------------------------------------------- [System.Windows.Forms.Application]::EnableVisualStyles() $formProcessChart = New-Object 'System.Windows.Forms.Form' $buttonSave = New-Object 'System.Windows.Forms.Button' $chartProcesses = New-Object 'System.Windows.Forms.DataVisualization.Charting.Chart' $savefiledialog1 = New-Object 'System.Windows.Forms.SaveFileDialog' $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' #endregion Generated Form Objects #---------------------------------------------- # User Generated Script #---------------------------------------------- #region Chart Helper Functions <# .SYNOPSIS This functions helps you plot point on a chart .DESCRIPTION Use the function to plot points on a chart or add more charts to a chart control .PARAMETER ChartControl The Chart Control you when to add points to .PARAMETER XPoints Set the X Axis Points. These can be strings or numerical values. .PARAMETER YPoints Set the Y Axis Points. These can be strings or numerical values. .PARAMETER XTitle Set the Title for the X Axis. .PARAMETER YTitle Set the Title for the Y Axis. .PARAMETER Title Set the Title for the chart. .PARAMETER ChartType Set the Style of the chart. See System.Windows.Forms.DataVisualization.Charting.SeriesChartType Enum .PARAMETER SeriesIndex Set the settings of a particular Series and corresponding ChartArea .PARAMETER TitleIndex Set the settings of a particular Title #> function Load-Chart { Param( #$XPoints, $YPoints, $XTitle, $YTitle, $Title, $ChartStyle) [Parameter(Position=1,Mandatory=$true)] [System.Windows.Forms.DataVisualization.Charting.Chart]$ChartControl , [Parameter(Position=2,Mandatory=$true)] $XPoints , [Parameter(Position=3,Mandatory=$true)] $YPoints , [Parameter(Position=4,Mandatory=$false)] [string]$XTitle , [Parameter(Position=5,Mandatory=$false)] [string]$YTitle , [Parameter(Position=6,Mandatory=$false)] [string]$Title , [Parameter(Position=7,Mandatory=$false)] [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]$ChartType , [Parameter(Position=8,Mandatory=$false)] $SeriesIndex = 0 , [Parameter(Position=9,Mandatory=$false)] $TitleIndex = 0, [switch]$Append) $ChartAreaIndex = 0 if($Append) { $name = "ChartArea " + ($ChartControl.ChartAreas.Count + 1).ToString(); $ChartArea = $ChartControl.ChartAreas.Add($name) $ChartAreaIndex = $ChartControl.ChartAreas.Count - 1 $name = "Series " + ($ChartControl.Series.Count + 1).ToString(); $Series = $ChartControl.Series.Add($name) $SeriesIndex = $ChartControl.Series.Count - 1 $Series.ChartArea = $ChartArea.Name if($Title) { $name = "Title " + ($ChartControl.Titles.Count + 1).ToString(); $TitleObj = $ChartControl.Titles.Add($name) $TitleIndex = $ChartControl.Titles.Count - 1 $TitleObj.DockedToChartArea = $ChartArea.Name $TitleObj.IsDockedInsideChartArea = $false } } else { if($ChartControl.ChartAreas.Count -eq 0) { $name = "ChartArea " + ($ChartControl.ChartAreas.Count + 1).ToString(); [void]$ChartControl.ChartAreas.Add($name) $ChartAreaIndex = $ChartControl.ChartAreas.Count - 1 } if($ChartControl.Series.Count -eq 0) { $name = "Series " + ($ChartControl.Series.Count + 1).ToString(); $Series = $ChartControl.Series.Add($name) $SeriesIndex = $ChartControl.Series.Count - 1 $Series.ChartArea = $ChartControl.ChartAreas[0].Name } } $Series = $ChartControl.Series[$SeriesIndex] $ChartArea = $ChartControl.ChartAreas[$Series.ChartArea] $Series.Points.Clear() if($Title) { if($ChartControl.Titles.Count -eq 0) { $name = "Title " + ($ChartControl.Titles.Count + 1).ToString(); [void]$ChartControl.Titles.Add($name) $TitleIndex = $ChartControl.Titles.Count - 1 $TitleObj.DockedToChartArea = $ChartArea.Name $TitleObj.IsDockedInsideChartArea = $false } $ChartControl.Titles[$TitleIndex].Text = $Title } if($ChartType) { $Series.ChartType = $ChartType } if($XTitle) { $ChartArea.AxisX.Title = $XTitle } if($YTitle) { $ChartArea.AxisY.Title = $YTitle } if($XPoints -isnot [Array] -or $XPoints -isnot [System.Collections.IEnumerable]) { $array = New-Object System.Collections.ArrayList $array.Add($XPoints) $XPoints = $array } if($YPoints -isnot [Array] -or $YPoints -isnot [System.Collections.IEnumerable]) { $array = New-Object System.Collections.ArrayList $array.Add($YPoints) $YPoints = $array } $Series.Points.DataBindXY($XPoints, $YPoints) } function Clear-Chart { Param ( [Parameter(Position=1,Mandatory=$true)] [System.Windows.Forms.DataVisualization.Charting.Chart]$ChartControl , [Parameter(Position=2, Mandatory=$false)] [Switch]$LeaveSingleChart ) $count = 0 if($LeaveSingleChart) { $count = 1 } while($ChartControl.Series.Count -gt $count) { $ChartControl.Series.RemoveAt($ChartControl.Series.Count - 1) } while($ChartControl.ChartAreas.Count -gt $count) { $ChartControl.ChartAreas.RemoveAt($ChartControl.ChartAreas.Count - 1) } while($ChartControl.Titles.Count -gt $count) { $ChartControl.Titles.RemoveAt($ChartControl.Titles.Count - 1) } if($ChartControl.Series.Count -gt 0) { $ChartControl.Series[0].Points.Clear() } } #endregion $FormEvent_Load={ #TODO: Initialize Form Controls here } function Load-ProcessChart { Param ( [Parameter(Position=1,Mandatory=$true)] [System.Windows.Forms.DataVisualization.Charting.Chart]$Chart) $Processes = Get-Process | Sort-Object -Property WS | Select-Object Name,WS,ID -Last 5 $ProcNames = @(foreach($Proc in $Processes){$Proc.Name + " (" + $Proc.ID + ")"}) $WS = @(foreach($Proc in $Processes){$Proc.WS/1MB}) Load-Chart $Chart -XPoints $ProcNames -YPoints $WS } $buttonSave_Click={ if($savefiledialog1.ShowDialog() -eq "OK") { $chartProcesses.SaveImage($savefiledialog1.FileName, "PNG") } } $chartProcesses_VisibleChanged={ if($this.Visible) { Load-ProcessChart $this } } # --End User Generated Script-- #---------------------------------------------- #region Generated Events #---------------------------------------------- $Form_StateCorrection_Load= { #Correct the initial state of the form to prevent the .Net maximized form issue $formProcessChart.WindowState = $InitialFormWindowState } $Form_Cleanup_FormClosed= { #Remove all event handlers from the controls try { $buttonSave.remove_Click($buttonSave_Click) $chartProcesses.remove_VisibleChanged($chartProcesses_VisibleChanged) $formProcessChart.remove_Load($FormEvent_Load) $formProcessChart.remove_Load($Form_StateCorrection_Load) $formProcessChart.remove_FormClosed($Form_Cleanup_FormClosed) } catch [Exception] { } } #endregion Generated Events #---------------------------------------------- #region Generated Form Code #---------------------------------------------- $formProcessChart.SuspendLayout() $chartProcesses.BeginInit() # # formProcessChart # $formProcessChart.Controls.Add($buttonSave) $formProcessChart.Controls.Add($chartProcesses) $formProcessChart.ClientSize = '284, 262' $formProcessChart.MinimumSize = '300, 300' $formProcessChart.Name = "formProcessChart" $formProcessChart.Text = "Process Chart" $formProcessChart.add_Load($FormEvent_Load) # # buttonSave # $buttonSave.Anchor = 'Bottom, Right' $buttonSave.Location = '197, 227' $buttonSave.Name = "buttonSave" $buttonSave.Size = '75, 23' $buttonSave.TabIndex = 1 $buttonSave.Text = "Save" $buttonSave.UseVisualStyleBackColor = $True $buttonSave.add_Click($buttonSave_Click) # # chartProcesses # $chartProcesses.Anchor = 'Top, Bottom, Left, Right' $chartProcesses.BackColor = 'Silver' $chartProcesses.BackGradientStyle = 'LeftRight' $System_Windows_Forms_DataVisualization_Charting_ChartArea_1 = New-Object 'System.Windows.Forms.DataVisualization.Charting.ChartArea' $System_Windows_Forms_DataVisualization_Charting_ChartArea_1.AxisX.Title = "Process" $System_Windows_Forms_DataVisualization_Charting_ChartArea_1.AxisY.Title = "Working Set (MB)" $System_Windows_Forms_DataVisualization_Charting_ChartArea_1.Name = "ChartArea1" [void]$chartProcesses.ChartAreas.Add($System_Windows_Forms_DataVisualization_Charting_ChartArea_1) $System_Windows_Forms_DataVisualization_Charting_Legend_2 = New-Object 'System.Windows.Forms.DataVisualization.Charting.Legend' $System_Windows_Forms_DataVisualization_Charting_Legend_2.Name = "Legend1" [void]$chartProcesses.Legends.Add($System_Windows_Forms_DataVisualization_Charting_Legend_2) $chartProcesses.Location = '13, 13' $chartProcesses.Name = "chartProcesses" $System_Windows_Forms_DataVisualization_Charting_Series_3 = New-Object 'System.Windows.Forms.DataVisualization.Charting.Series' $System_Windows_Forms_DataVisualization_Charting_Series_3.ChartArea = "ChartArea1" $System_Windows_Forms_DataVisualization_Charting_Series_3.ChartType = 'StackedBar' $System_Windows_Forms_DataVisualization_Charting_Series_3.CustomProperties = "DrawingStyle=Cylinder" $System_Windows_Forms_DataVisualization_Charting_Series_3.IsVisibleInLegend = $False $System_Windows_Forms_DataVisualization_Charting_Series_3.Legend = "Legend1" $System_Windows_Forms_DataVisualization_Charting_Series_3.Name = "Process WS" $System_Windows_Forms_DataVisualization_Charting_Series_3.Palette = 'Pastel' [void]$chartProcesses.Series.Add($System_Windows_Forms_DataVisualization_Charting_Series_3) $chartProcesses.Size = '259, 204' $chartProcesses.TabIndex = 0 $chartProcesses.Text = "chart1" $System_Windows_Forms_DataVisualization_Charting_Title_4 = New-Object 'System.Windows.Forms.DataVisualization.Charting.Title' $System_Windows_Forms_DataVisualization_Charting_Title_4.Name = "Title1" $System_Windows_Forms_DataVisualization_Charting_Title_4.Text = "Top 5 Processes" [void]$chartProcesses.Titles.Add($System_Windows_Forms_DataVisualization_Charting_Title_4) $chartProcesses.add_VisibleChanged($chartProcesses_VisibleChanged) # # savefiledialog1 # $savefiledialog1.CreatePrompt = $True $savefiledialog1.DefaultExt = "png" $savefiledialog1.Filter = "PNG Image (.png)|*.png" $savefiledialog1.ShowHelp = $True $chartProcesses.EndInit() $formProcessChart.ResumeLayout() #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = $formProcessChart.WindowState #Init the OnLoad event to correct the initial state of the form $formProcessChart.add_Load($Form_StateCorrection_Load) #Clean up the control events $formProcessChart.add_FormClosed($Form_Cleanup_FormClosed) #Show the Form return $formProcessChart.ShowDialog() } #End Function #Call OnApplicationLoad to initialize if((OnApplicationLoad) -eq $true) { #Call the form Call-Process_Chart_psf | Out-Null #Perform cleanup OnApplicationExit }