A N

January 31, 2014

Get C drive space with Chart – Sapien Powershell Studio

Filed under: Tips — donrsh @ 11:23 am

Copy the code and run it as ps1 file.
c

#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: 560a7619-3673-48a1-b726-29efcc753954
# Source File: C:\Users\administrator\Documents\SAPIEN\PowerShell Studio 2012\Projects\C\MainForm.pff
#------------------------------------------------------------------------
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
oRAAAB+LCAAAAAAABADNWG2P2kYQ/l6p/8HyZ4oxxpBExtLF12tPvSQokLQfKlXLMnDbW++i9fou
zq/v+BW/Ecj1iioEZmfnfZ6dZfA+ApWPoJJroon/4w+G4X1QbMcE4TeMw3sSgv+OMHEjVTjcb7ee
1dnOhNLVZ1ARk8K3h2PPqhNyteu/gWpDJ3uYm8sk0hAOf2diI5+iYao9/xwYfVsDo1A1nwxH6Wtg
BDHXsYK5gFgrwgfGIl5zRn+DZCUfQMzXsxlxqTu1XzsTGL16bRoCnZ2bZTCmQe8Z3yhkNQMptJI8
MjNP0deFkntQOilk3hL6EEgulem/jbWW4le2u+f41p5Vsh4RDTgDoZfsK5i+O5kMjMl0fFIoTatZ
pf0k+1ITpRcyYhozZPoBGgS1pApAnJRdwRdt+tcsosZyTyh0BX5+RH0F950kG9P/INJn6lq251nZ
o+Q/Xec8h/95pdeZmeBbhe7m405Skqdx7MwGxnjaScixchXmzuDP0TBzUb1zBvuKrG/FBr6Yvn0O
d1bQ4M2fZ/B+iuAzi2LClzrhUEP5SsVdJLSwgMimD1XYf2XLFhSsHAvnIyNtQrlH7GtWhmFwj9hm
Ypd/6cdMV+wZKHLs9dZ55U7JxplOwHFLFNHUrv19IMp8vVJAajzIdYue/5voU40XzYB1iPA0mO5g
B2LzYhHn6v7H4VaNwh4PDNc9u0/kgDqDfUE4aI0SN0z1nsbetuKO0J2J/X2NZXR2Yznu/RIUS+sB
dQBk5TSKSOZmGolppI7OzdLROmBQooBRg1iRjfc1qNn1iuVV6xP2DqexrbTaKfRW6x7VvVoKcg03
7Sw0emBzcRVFECIkodRYUhK/AP07IsgOQuyow6tYy7AFdee5UPesylK/5Re4hU/aePF7/7TFa0We
sLc8x9bI2brb2da2N+6IOOQbtsKISsXZ+jIRYSO8iKE/Qn6ZgLBDUC1VggfpkVF4FjDOL9bFbpeW
D9WyPP3eAn994WFXpYu/gABUXGvHOUM1chX3VB+1IN7SYvZqsDap2GLYFiIdYGfT2fDmWR1axR3E
Ebahcr+m+9jGghO9xaT6P6V3Rrmotj/GYrm6Sq+f4ttBUD7hDHMPnJdDZDo2dYhlk21mC+8iGiuG
V1fZxb2mQGtgPcTRS8W7bhNT3eU+tlHQ26Xqo15DRBXbN9Ns9VIDGe6JSOpZb1MCuU9UOpc2mDq0
23RAxBG+5WA/uXfezwWOb6Fn4WEQKHwN66NB478Bq4Z+xEL9L4l/ACVBCrihEAAA#>
#endregion
#========================================================================
# Code Generated By: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.24
# Generated On: 1/31/2014 11:11 AM
# Generated By: Administrator
#========================================================================
#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-MainForm_pff {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[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.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("mscorlib, Version=4.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.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.Windows.Forms.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$MainForm = New-Object 'System.Windows.Forms.Form'
	$buttonC = New-Object 'System.Windows.Forms.Button'
	$chart1 = New-Object 'System.Windows.Forms.DataVisualization.Charting.Chart'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	function Get-DriveSpace  {
		
		$free = Get-PSDrive -PSProvider FileSystem  -Name c | ForEach-Object {[math]::truncate($_.free / 1GB)}
		$used =  Get-PSDrive -PSProvider FileSystem  -Name c | ForEach-Object {[math]::truncate($_.used / 1GB)}
		Load-Chart $chart1 -XPoints ("Used ({0:N1} GB)" -f $Used), ("Free Space ({0:N1} GB)" -f $free) -YPoints $Used, $Free -ChartType "pie"
	}
	
	
	
	$OnLoadFormEvent={
	#TODO: Initialize Form Controls here
	
	}
	
	
	
	
	
	#region Control Helper Functions
	function Load-Chart
	{
	<#
		.SYNOPSIS
			This functions helps you plot points 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
		
		.PARAMETER SeriesName
			Set the settings of a particular Series using its name and corresponding ChartArea. 
			The Series will be created if not found.
			If SeriesIndex is set, it will replace the Series' name if the Series does not exist
		
		.PARAMETER Enable3D
			The chart will be rendered in 3D.
		
		.PARAMETER Disable3D
			The chart will be rendered in 2D.	
		
		.PARAMETER Append
			When this switch is used, a new ChartArea is added to Chart Control.
	
		.LINK
			http://www.sapien.com/blog/2011/05/05/primalforms-2011-designing-charts-for-powershell/
		
	#>
		Param( #$XPoints, $YPoints, $XTitle, $YTitle, $Title, $ChartStyle)
		 	[ValidateNotNull()]
			[Parameter(Position=1,Mandatory=$true)]
	  		[System.Windows.Forms.DataVisualization.Charting.Chart]$ChartControl
			,
			[ValidateNotNull()]
			[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 = -1
			,
			[Parameter(Position=9,Mandatory=$false)]
	  		$TitleIndex = 0,
			[Parameter(Mandatory=$false)]
	  		[string]$SeriesName = $null,
			[switch]$Enable3D,
			[switch]$Disable3D,
			[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($Title)
				$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)
			{
				if(-not $SeriesName)
				{
					$SeriesName = "Series " + ($ChartControl.Series.Count + 1).ToString();
				}
				
				$Series = $ChartControl.Series.Add($SeriesName) 
				$SeriesIndex = $ChartControl.Series.Count - 1
				$Series.ChartArea = $ChartControl.ChartAreas[$ChartAreaIndex].Name
			}
			elseif($SeriesName)
			{
				$Series = $ChartControl.Series.FindByName($SeriesName)
					
				if($Series -eq $null)
				{
					if(($SeriesIndex -gt -1) -and ($SeriesIndex -lt $ChartControl.Series.Count))
					{
						$Series = $ChartControl.Series[$SeriesIndex]
						$Series.Name = $SeriesName
					}
					else
					{
						$Series = $ChartControl.Series.Add($SeriesName)
						$SeriesIndex = $ChartControl.Series.Count - 1
					}
					
					$Series.ChartArea = $ChartControl.ChartAreas[$ChartAreaIndex].Name
				}
				else
				{
					$SeriesIndex = $ChartControl.Series.IndexOf($Series)
					$ChartAreaIndex = $ChartControl.ChartAreas.IndexOf($Series.ChartArea)
				}		
			}
		}
		
		if(($SeriesIndex -lt 0) -or ($SeriesIndex -ge $ChartControl.Series.Count))
		{
			$SeriesIndex = 0
		}
		
		$Series = $ChartControl.Series[$SeriesIndex]
		$Series.Points.Clear()
		$ChartArea = $ChartControl.ChartAreas[$Series.ChartArea]
		
		if($Enable3D)
		{
			$ChartArea.Area3DStyle.Enable3D = $true
		}
		elseif($Disable3D)
		{
			$ChartArea.Area3DStyle.Enable3D = $false
		}
		
		if($Title)
		{
			if($ChartControl.Titles.Count -eq 0)
			{
				#$name = "Title " + ($ChartControl.Titles.Count + 1).ToString();
				$TitleObj = $ChartControl.Titles.Add($Title)
				$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
	{
	<#
		.SYNOPSIS
			This function clears the contents of the chart
	
		.DESCRIPTION
			Use the function to remove contents from the chart control
	
		.PARAMETER  ChartControl
			The Chart Control to clear
	
		.PARAMETER  LeaveSingleChart
			Leaves the first chart and removes all others from the control
		
		.LINK
			http://www.sapien.com/blog/2011/05/05/primalforms-2011-designing-charts-for-powershell/
	#>
		Param (	
		[ValidateNotNull()]
		[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
	
	
	$buttonC_Click={
		#TODO: Place custom script here
		Get-DriveSpace
	}
		# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$MainForm.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$buttonC.remove_Click($buttonC_Click)
			$MainForm.remove_Load($OnLoadFormEvent)
			$MainForm.remove_Load($Form_StateCorrection_Load)
			$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	#
	# MainForm
	#
	$MainForm.Controls.Add($buttonC)
	$MainForm.Controls.Add($chart1)
	$MainForm.BackColor = 'ButtonHighlight'
	$MainForm.ClientSize = '544, 462'
	$MainForm.Name = "MainForm"
	$MainForm.StartPosition = 'CenterScreen'
	$MainForm.Text = "Disc Space"
	$MainForm.add_Load($OnLoadFormEvent)
	#
	# buttonC
	#
	$buttonC.Location = '237, 26'
	$buttonC.Name = "buttonC"
	$buttonC.Size = '75, 23'
	$buttonC.TabIndex = 1
	$buttonC.Text = "C:\"
	$buttonC.UseVisualStyleBackColor = $True
	$buttonC.add_Click($buttonC_Click)
	#
	# chart1
	#
	$System_Windows_Forms_DataVisualization_Charting_ChartArea_1 = New-Object 'System.Windows.Forms.DataVisualization.Charting.ChartArea'
	$System_Windows_Forms_DataVisualization_Charting_ChartArea_1.Name = "ChartArea1"
	[void]$chart1.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]$chart1.Legends.Add($System_Windows_Forms_DataVisualization_Charting_Legend_2)
	$chart1.Location = '12, 55'
	$chart1.Name = "chart1"
	$chart1.Palette = 'Fire'
	$chart1.Size = '502, 413'
	$chart1.TabIndex = 0
	$chart1.Text = "chart1"
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $MainForm.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$MainForm.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $MainForm.ShowDialog()

} #End Function

#Call the form
Call-MainForm_pff | Out-Null

 
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: