Created
August 15, 2019 12:05
-
-
Save biacz/78aca54e260a681b1017feeaa41005c3 to your computer and use it in GitHub Desktop.
Checking disk space
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CleanVariables { Get-Variable | Where-Object { $startupVariables -notcontains $_.Name } | ForEach-Object { | |
try { Remove-Variable -Name "$($_.Name)" -Force -Scope "global" -ErrorAction SilentlyContinue -WarningAction SilentlyContinue } | |
catch { } | |
} | |
} | |
CleanVariables | |
<# This form was created using POSHGUI.com a free online gui designer for PowerShell | |
.NAME | |
Disk Space Analyzer | |
#> | |
Add-Type -AssemblyName System.Windows.Forms | |
[System.Windows.Forms.Application]::EnableVisualStyles() | |
$Form = New-Object system.Windows.Forms.Form | |
$Form.ClientSize = '800,800' | |
$Form.text = "Form" | |
$Form.TopMost = $false | |
$TB_ComputerName = New-Object system.Windows.Forms.TextBox | |
$TB_ComputerName.multiline = $false | |
$TB_ComputerName.width = 219 | |
$TB_ComputerName.height = 20 | |
$TB_ComputerName.location = New-Object System.Drawing.Point(147,145) | |
$TB_ComputerName.Font = 'Microsoft Sans Serif,10' | |
$LB_ComputerName = New-Object system.Windows.Forms.Label | |
$LB_ComputerName.text = "Computer Name:" | |
$LB_ComputerName.AutoSize = $true | |
$LB_ComputerName.width = 25 | |
$LB_ComputerName.height = 10 | |
$LB_ComputerName.location = New-Object System.Drawing.Point(34,148) | |
$LB_ComputerName.Font = 'Microsoft Sans Serif,10' | |
$TB_UserName = New-Object system.Windows.Forms.TextBox | |
$TB_UserName.multiline = $false | |
$TB_UserName.width = 219 | |
$TB_UserName.height = 20 | |
$TB_UserName.location = New-Object System.Drawing.Point(147,172) | |
$TB_UserName.Font = 'Microsoft Sans Serif,10' | |
$LB_UserName = New-Object system.Windows.Forms.Label | |
$LB_UserName.text = "User Name:" | |
$LB_UserName.AutoSize = $true | |
$LB_UserName.width = 25 | |
$LB_UserName.height = 10 | |
$LB_UserName.location = New-Object System.Drawing.Point(34,175) | |
$LB_UserName.Font = 'Microsoft Sans Serif,10' | |
$LB_INFO = New-Object system.Windows.Forms.Label | |
$LB_INFO.text = "Please enter the Computer Name and User Name below and press `"RUN!`"" | |
$LB_INFO.AutoSize = $true | |
$LB_INFO.width = 25 | |
$LB_INFO.height = 10 | |
$LB_INFO.location = New-Object System.Drawing.Point(34,81) | |
$LB_INFO.Font = 'Microsoft Sans Serif,10' | |
$LB_TITLE = New-Object system.Windows.Forms.Label | |
$LB_TITLE.text = "DISK SPACE ANALYZER v0.1b" | |
$LB_TITLE.AutoSize = $true | |
$LB_TITLE.width = 25 | |
$LB_TITLE.height = 10 | |
$LB_TITLE.location = New-Object System.Drawing.Point(137,20) | |
$LB_TITLE.Font = 'Microsoft Sans Serif,20' | |
$BTN_RUN = New-Object system.Windows.Forms.Button | |
$BTN_RUN.text = "RUN!" | |
$BTN_RUN.width = 60 | |
$BTN_RUN.height = 30 | |
$BTN_RUN.location = New-Object System.Drawing.Point(147,209) | |
$BTN_RUN.Font = 'Microsoft Sans Serif,10' | |
$BTN_SAVE = New-Object system.Windows.Forms.Button | |
$BTN_SAVE.text = "SAVE" | |
$BTN_SAVE.width = 60 | |
$BTN_SAVE.height = 30 | |
$BTN_SAVE.location = New-Object System.Drawing.Point(207,209) | |
$BTN_SAVE.Font = 'Microsoft Sans Serif,10' | |
$BTN_Cancel = New-Object system.Windows.Forms.Button | |
$BTN_Cancel.text = "CLOSE" | |
$BTN_Cancel.width = 60 | |
$BTN_Cancel.height = 30 | |
$BTN_Cancel.location = New-Object System.Drawing.Point(267,209) | |
$BTN_Cancel.Font = 'Microsoft Sans Serif,10' | |
$LB_Status = New-Object system.Windows.Forms.Label | |
$LB_Status.text = "Status: " | |
$LB_Status.AutoSize = $true | |
$LB_Status.width = 25 | |
$LB_Status.height = 10 | |
$LB_Status.location = New-Object System.Drawing.Point(34,113) | |
$LB_Status.Font = 'Microsoft Sans Serif,10,style=Bold' | |
$LB_StatusUpdate = New-Object system.Windows.Forms.Label | |
$LB_StatusUpdate.AutoSize = $true | |
$LB_StatusUpdate.width = 25 | |
$LB_StatusUpdate.height = 10 | |
$LB_StatusUpdate.location = New-Object System.Drawing.Point(85,113) | |
$LB_StatusUpdate.Font = 'Microsoft Sans Serif,10' | |
$Form.controls.AddRange(@($TB_ComputerName,$LB_ComputerName,$TB_UserName,$LB_UserName,$LB_INFO,$LB_TITLE,$BTN_RUN,$BTN_SAVE,$BTN_Cancel,$LB_Status,$LB_StatusUpdate)) | |
$BTN_Cancel.Add_Click({ closeForm }) | |
$BTN_SAVE.Add_Click({ saveImage }) | |
$BTN_RUN.Add_Click({ RunCalculation }) | |
#Write your logic code here | |
Add-Type -AssemblyName System.Windows.Forms.DataVisualization | |
$Chart = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Chart | |
$Chart.Size = '750,600' | |
$ChartArea = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.ChartArea | |
$ChartArea.AxisX.Title = 'Directory' | |
$ChartArea.AxisY.Title = 'SizeGB' | |
$ChartArea.AxisX.Interval = '1' | |
$ChartArea.AxisY.Interval = '5' | |
$ChartArea.AxisX.LabelStyle.Enabled = $true | |
$ChartArea.AxisX.LabelStyle.Angle = 90 | |
$Chart.ChartAreas.Add($ChartArea) | |
$Chart.Series.Add('SizeGB') | |
$Chart.Series['SizeGB'].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::StackedBar | |
#$Chart.Series['SizeGB'].IsValueShownAsLabel = $true | |
$Title = New-Object -TypeName System.Windows.Forms.DataVisualization.Charting.Title | |
$Chart.Titles.Add($Title) | |
$Chart.Titles[0].Text = 'Folder Size Analyzer' | |
function closeForm { $Form.close() } | |
function saveImage { | |
if(!$Chart.Series.Points) { | |
$LB_StatusUpdate.Text = "Run the report first..." | |
} | |
else { | |
$SaveChooser = New-Object -Typename System.Windows.Forms.SaveFileDialog | |
$SaveChooser.ShowDialog() | |
$Save_Filename = $SaveChooser.Filename | |
$LB_StatusUpdate.Text = "$Save_Filename\DirectorySizeChart.png" | |
$Chart.SaveImage("$Save_Filename\DirectorySizeChart.png", "PNG") | |
} | |
} | |
function RunCalculation { | |
$computer = $TB_ComputerName.Text | |
$user = $TB_UserName.Text | |
$LB_StatusUpdate.Text = "Running disk space query on \\$computer\c$\users\$user..." | |
Start-Sleep 3 | |
$LB_StatusUpdate.Text = "Do not close this window before you see the chart below!" | |
Start-Sleep 3 | |
$output = get-childitem -force "\\$computer\c$\users\$user" -ErrorAction SilentlyContinue | where-object { $_ -is [io.directoryinfo] } | | |
foreach-object { | |
$len = 0 | |
get-childitem -recurse -force $_.FullName -ErrorAction SilentlyContinue | foreach-object { $len += $_.length } | |
[pscustomobject]@{ | |
Name = $_.FullName | |
SizeGB = [math]::round($len / 1Gb, 2) | |
} | |
} | |
$output | sort-object SizeGB | foreach-object { | |
$Value = $Chart.Series['SizeGB'].Points.AddXY($_.Name,$_.SizeGB) | |
$LB_StatusUpdate.Text = "Report created..." | |
} | |
$Chart.Series.Points | ForEach-Object { | |
if($_.YValue -eq 0) { $_.IsValueShownAsLabel = $false } | |
} | |
$Chart.Left = 20 | |
$Chart.Top = 240 | |
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Left -bor | |
$Form.controls.add($Chart) | |
} | |
$Form.Add_Shown({ $Form.Activate() }) | |
$result = $Form.ShowDialog() | |
if ($result -eq [System.Windows.Forms.DialogResult]::Cancel) | |
{ | |
write-output 'User pressed cancel' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
need help with 2 things: