Sean February 2016

Creating a progress bar for a search function

I am trying to make a progress bar in a powershell generated GUI that will show the progress of a search as the system caches all the groups in AD (there are about 12,000 so this just otherwise causes the system to hang for a short while)

I managed to build the bar etc but I cannot get it to fill the bar as the system adds the groups. Here is what I have so far:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$Form = New-Object System.Windows.Forms.Form
$Form.width = 345
$Form.height = 345
$Form.StartPosition = "CenterScreen"
$Form.ShowInTaskbar = $True
$Form.FormBorderStyle = 'FixedToolWindow'

$ExitButt = new-object System.Windows.Forms.Button
$ExitButt.Location = new-object System.Drawing.Size(235,5)
$ExitButt.Size = new-object System.Drawing.Size(100,20)
$ExitButt.Text = "Exit"
$Form.Controls.Add($ExitButt)
$ExitButt.Add_Click({$Form.Close()})

$Prog = New-Object System.Windows.Forms.ProgressBar
$Prog.Maximum = 10000
$Prog.Minimum = 0
$Prog.Location = new-object System.Drawing.Size(50,50)
$Prog.size = new-object System.Drawing.Size(100,50)
$Form.Controls.Add($Prog)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(120,100)
$Button.Size = new-object System.Drawing.Size(100,30)
$Button.Text = "Start Progress"
$Form.Controls.Add($Button)
$Button.add_click(
    {
    $GroupsList = Get-ADGroup -Server "server" -Filter *
    $Count = ($GroupsList | Measure-Object).Count
    $prog.Value = $Count
    }
    )
$form.ShowDialog() | Out-Null

Answers


Richard February 2016

To update a WinForms ProgressBar just set its Value property.

Eg.

# Initialisation...
$myProgBar.Minimum = 1;
$myProgBar.Maximum = 100;

# When something happens...
$myProgBar.Value = 50;

will set it to half way.

Post Status

Asked in February 2016
Viewed 2,503 times
Voted 9
Answered 1 times

Search




Leave an answer