Taking Control of VM Sprawl (Part 11)

If you would like to read the other parts in this article series please go to:

In my previous article, I walked you through a script that displayed a list of all of the virtual machines that were recently created, as well as who created each virtual machine. The script makes for a good start, but it would be a lot more useful if we could filter the output by date or even track VM creation and deletion trends. We will get to that soon enough, but in this article I need to pause and teach you a bit more PowerShell.

My eventual goal is to build a chart that tracks virtual machine creation and deletion on a weekly or monthly basis. For right now though, I need to show you how to dump PowerShell script output into a chart.

PowerShell does not have any native chart plotting capabilities, but Microsoft does provide a free set of chart controls that work with the .NET Framework. Because the .NET Framework can be leveraged through PowerShell it is possible to use the Microsoft Chart Controls to create charts from your PowerShell reports.

You can download the Microsoft Chart Controls here. The download consists of a 1.8 MB executable file. Simply verify that version 3.5 of the .NET Framework is installed on your system, and then download, and run the file. The installation process is really simply. The installation wizard just requires you to accept the license agreement and then click Next a few times.

Once the Microsoft Chart Controls have been installed, you can begin building your chart. Let me just say up front that the charting process takes a little bit of getting used to. There are some great tutorials with code samples here and here. The charting technique that I am going to be using is basically a combination of the techniques used on these pages, with just enough of my own techniques thrown in to make things interesting. So with that said, here is a script that I have created for demonstration purposes:

void][Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms.DataVisualization”)

$scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition

 

# chart object

   $chart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart

   $chart1.Width = 1000

   $chart1.Height = 1000

   $chart1.BackColor = [System.Drawing.Color]::White

 

# title

   [void]$chart1.Titles.Add(“Virtual Machines Created and Deleted”)

   $chart1.Titles[0].Font = “Arial,13pt”

   $chart1.Titles[0].Alignment = “topLeft”

 

# chart area

   $chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea

   $chartarea.Name = “ChartArea1”

   $chartarea.AxisY.Title = “Virtual Machines”

   $chartarea.AxisX.Title = “Time”

   $chartarea.AxisY.Interval = 1

   $chartarea.AxisX.Interval = 1

   $chart1.ChartAreas.Add($chartarea)

 

# legend

   $legend = New-Object system.Windows.Forms.DataVisualization.Charting.Legend

   $legend.name = “Legend1”

   $chart1.Legends.Add($legend)

 

# data source

 

$DataSource1 = @{January=5; February=9; March=7; April=2; May=6}

$DataSource2 = @{January=8; February=2; March=1; April=4; May=6}

 

# Virtual Machines Created data series

   [void]$chart1.Series.Add(“VM_Created”)

   $chart1.Series[“VM_Created”].ChartType = “Line”

   $chart1.Series[“VM_Created”].BorderWidth  = 3

   $chart1.Series[“VM_Created”].IsVisibleInLegend = $true

   $chart1.Series[“VM_Created”].chartarea = “ChartArea1”

   $chart1.Series[“VM_Created”].Legend = “Legend1”

   $chart1.Series[“VM_Created”].color = “Green”

   $Chart1.Series[“VM_Created”].Points.DataBindXY($DataSource1.keys, $DataSource1.Values)

 

# Virtual machines deleted data series

   [void]$chart1.Series.Add(“VM_Deleted”)

   $chart1.Series[“VM_Deleted”].ChartType = “Line”

   $chart1.Series[“VM_Deleted”].IsVisibleInLegend = $true

   $chart1.Series[“VM_Deleted”].BorderWidth  = 3

   $chart1.Series[“VM_Deleted”].chartarea = “ChartArea1”

   $chart1.Series[“VM_Deleted”].Legend = “Legend1”

   $chart1.Series[“VM_Deleted”].color = “Blue”

   $Chart1.Series[“VM_Deleted”].Points.DataBindXY($DataSource2.keys, $DataSource2.Values)

 

# display the chart on a form

 

$Form = New-Object Windows.Forms.Form

$Form.Text = “VM Sprawl”

$Form.Width = 1000

$Form.Height = 1000

$Form.controls.add($Chart1)

$Form.Add_Shown({$Form.Activate()})

$Form.ShowDialog()

You can see what the script’s output looks like in Figure A.

Image
Figure A: This is the chart created by my PowerShell script.

Before I talk about how the script works, there are a few things that I want to point out. First, you will notice that the chart above contains two lines. One line represents the number of virtual machines created in a given month, while the other represents the number of virtual machines deleted in the same time period. The legend in the upper right portion of the chart shows which line is which. You can therefore use a chart like this one to monitor virtual machine creation trends.

The next thing that I need to mention is that you shouldn’t worry about the data shown on the chart, at least not yet. For right now I have hard coded some meaningless data into the script just so that I would have something to display. As a result of using hard coded, fake data, the months at the bottom of the chart are currently out of order. Don’t worry about the position of the months because we will fix this as we configure the script to display real data.

So why am I using fake data? Well, real data adds a layer of complexity and for right now I wanted to focus solely on explaining how the charting process works. I’m not going to go into a super in depth explanation, but there are a few key parts of the script that I want to point out.

If you look back at the source code, you will notice that the script is broken into sections. First, we define a chart object. Next, we assign a title to the chart. In doing so, we can even specify a title font.

The next thing that we have to do is to set some parameters for the chart area. In other words, we need to control the way that the chart is displayed. As you can see in the script, I have set a title and an interval for the X and Y axis. The interval controls the scale of the chart. Since none of my data values are greater than 10, I set the interval to 1.

Next, I add a legend to the chart and from there I define my data sources. This is the really important part. I have created two data sources, both of which are arrays. One is called $DataSource1 and the other is called $DataSource2. You saw in the figure that there were two lines on the chart. Each line corresponds to a data source.

The trick to creating a chart that contains multiple lines is that you have to define multiple data series, one series for each line. In this case, I created two series. One series was called VM_Created and the other was called VM_Deleted. You can adjust parameters such as the chart type, legend to be used, and color separately for each series.

Now, take a look at the last line in the series definition. It looks like this:

  $Chart1.Series[“VM_Created”].Points.DataBindXY($DataSource1.keys, $DataSource1.Values)

$Chart1 refers to my chart. I only have one chart, so both series will use it. Notice that the line above references $Chart1.Series[“VM_Created”]. This is the line that adds the data from my VM creation series to the chart. This command also uses DataBindXY to associate the keys and the values from $DataSource1 with the VM_Created series. In other words, I have defined my data as $DataSource1 and then linked that data to a series called VM_Created, which will be plotted onto $Chart1 using a green line. Keep in mind that this command creates the chart, but it does not draw it on screen. If you want the chart to be displayed, you will have to use the last section of  the script to define a Windows form and then associate the chart with the form.

Conclusion

In this article, I have shown you the basics of using PowerShell to create a chart. In the next article in the series, we will begin adapting our VM Sprawl script to support charting.

If you would like to read the other parts in this article series please go to:

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top