Quantcast
Channel: Windows PowerShell - SAPIEN Blog
Viewing all articles
Browse latest Browse all 308

Progress Bar Series (Part 3) – Handling Progress Status Information in a GUI Application

$
0
0

In Part 3 of the Progress Bar series, we are going to take a more in-depth look at the “Progress Bar Overlay” control. This control allows us to display text in the progress bar while processing the script in the application.

Getting Computer Information Scenario

In this GUI application, we will retrieve some system information and at the same time display each data collection step of the process by using the “Progress Bar Overlay” control.

As the script steps execute, the TextBox control will show the computer name for the data that is being collected. Next, the progress bar will increment as the different script steps complete, and then the RichTextBox control will display the results.

“Get Info” Button Control

The “Progress Bar Overlay” control contains the TextOverlay property, where you can add a text string that will display as the progress bar increments during script execution.

The script’s processing takes place in the code behind the “Get Info” button control. The text box will display the system from which the data is being collected. The computer names are stored in a string array, and then the for-each loop will pick one computer name at a time.

In this scenario, to get the progress bar Maximum property value, we get the total count of items in the array and then multiply by the number of steps. The Step property is set to 1 which will allow the Perform() method to increment the bar by 1.  Using the Start-Sleep cmdlet helps to provide a delay of 1 second to display the “Processing…” text in the progress TextOverlay property, which allows us to see the progress bar increment while executing the script steps in the for-each loop logic.

:
## - Set list of systems:
$svrList = @("$($env:COMPUTERNAME)", "$($env:COMPUTERNAME)");

## - TextOverlay before executing function:(Array count * number of steps)
$progressbaroverlay1.Maximum = $svrList.Count * 3;
$progressbaroverlay1.Step = 1;
$progressbaroverlay1.Value = 0;
$progressbaroverlay1.TextOverlay = "Processing...";
Start-Sleep -Seconds 1;
:

Then, inside the for-each loop in each of the script step’s sections, the following progress bar actions are performed:

  1. The progress bar TextOverlay property displays the step text.
  2. The script step executes.
  3. The progress bar PerformStep() method visually increments the bar.
  4. The Start-Sleep cmdlet at the end of each step gives a slight delay allowing the progress bar to display the script step text.
:
## - Foreach to get system information:
$global:SvrObj += foreach ($name in $svrList)
{
## - Get Computername:
$textbox1.Text = "Collecting information from Computer - $name";

## - Script Step 1:
$progressbaroverlay1.TextOverlay = "Step 1: Get Server Operating System Info...";
$x = get-wmiObject -Class Win32_OperatingSystem -Computername $name `
-ErrorAction 'SilentlyContinue' -ErrorVariable MyError;

$progressbaroverlay1.PerformStep();
Start-Sleep -Seconds 1;

## - Script Step 2:
$progressbaroverlay1.TextOverlay = "Step 2: Get Server Computer System Info...";

$x1 = get-wmiObject -Class Win32_ComputerSystem -Computername $name `
-ErrorAction 'SilentlyContinue' -ErrorVariable MyError;

$progressbaroverlay1.PerformStep();
Start-Sleep -Seconds 1;
## - Script Step 3:
$progressbaroverlay1.TextOverlay = "Step 3: Create $name PSObject ...";

## - Build PSCustomObject:
[PSCustomObject]$SystemInfo = New-Object PSObject -Property @{
ComputerName = $x.csname;
OperatingSystem = $x.Name.Split('|')[0];
LastBootUpTime = $x.ConvertToDateTime($x.LastBootUpTime);
Status = "Success";
Manufacturer = $x1.manufacturer;
SystemModel = $x1.model;
'PhysicalMemory(GB)' = [float]($x1.TotalPhysicalMemory/1gb);
} -ErrorVariable MyError; $SystemInfo

$progressbaroverlay1.PerformStep();
Start-Sleep -Seconds 1;
};
:

  

Now we can see the progress bar increment and display the text for the script step that has executed.

Another variation for this scenario is to use multiple progress bars while executing a series of script steps. This involves modifying the form to display overall progress and also individual step progress while collecting information from each system.

  

As you can see, there are different ways that your application can display progress.

Summary

The scenario outlined above follows the same coding approach as the previous post in the series, except here we are executing a series of script steps. Additionally, using the progress bar overlay allows us to provides details about what is happening by displaying text as the progress bar increments.

Here is a general view of the logic used in the progress bar overlay sample:

Download Progress Bar Overlay sample form: ProgressOverlayMultiTask01.zip

Here is the multi-steps progress bar code overview:

Download the multi-steps progress bar sample form: ProgressOverlayMultiTask02.zip

What’s next?

Next in the series is “Part 4 – Handling Steps Progress with a Background Job in a GUI Application” where we will use a “Progress Bar” control in a “responsive” GUI application with the ability to stop a process.

Progress Bar series topics:

  • Progress Bar Series (Part 1) – Displaying Script Progress in GUI Applications
  • Progress Bar Series (Part 2) – Handling Progress with Notion of Completion in a GUI Application
  • Progress Bar Series (Part 3) – Handling Progress Status Information in a GUI Application
  • Progress Bar Series (Part 4) – Handling Steps Progress with a Background Job in a GUI Application
  • Progress Bar Series (Part 5) – Display Progress in a Script (non-GUI) solution

Related Articles

Feedback

As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.


Viewing all articles
Browse latest Browse all 308

Trending Articles