Jump to content

How to Provide Progress Updates with Windows PowerShell

0
  chco's Photo
Posted Sep 02 2010 05:38 AM

The following excerpt is from Windows PowerShell Cookbook, Second Edition. The author speaks to what is needed should you wish to display status information to the user for long-running tasks.
To provide status updates, use the Write-Progress cmdlet shown in Example 13-5.


Example 13-5. Using the Write-Progress cmdlet to display status updates

##############################################################################
##
## Invoke-LongRunningOperation
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

Demonstrates the functionality of the Write-Progress cmdlet

#>

Set-StrictMode -Version Latest

$activity = "A long running operation"
$status = "Initializing"

## Initialize the long-running operation
for($counter = 0; $counter -lt 100; $counter++)
{
    $currentOperation = "Initializing item $counter"
    Write-Progress $activity $status -PercentComplete $counter `
        -CurrentOperation $currentOperation
    Start-Sleep -m 20
}

$status = "Running"

## Initialize the long-running operation
for($counter = 0; $counter -lt 100; $counter++)
{
    $currentOperation = "Running task $counter"
    Write-Progress $activity $status -PercentComplete $counter `
        -CurrentOperation $currentOperation
    Start-Sleep -m 20
}


The Write-Progress cmdlet provides a way for you to provide structured status information to the users of your script for long-running operations.

Figure 13-1. Example output from a long-running operation

Attached Image


Like the other detailed information channels (Write-Debug, Write-Verbose, and the other Write-* cmdlets), PowerShell lets users control how much of this information they see.

For more information about the Write-Progress cmdlet, type Get-Help Write-Progress.

Cover of Windows PowerShell Cookbook
Learn more about this topic from Windows PowerShell Cookbook, 2nd Edition. 

This introduction to the Windows PowerShell language and scripting environment provides more than 430 task-oriented recipes to help you solve the most complex and pressing problems, and includes more than 100 tried-and-tested scripts that intermediate to advanced system administrators can copy and use immediately. You'll find hands-on tutorials on fundamentals, common tasks, and administrative jobs that you can apply whether you're on a client or server version of Windows.

Learn More Read Now on Safari


Tags:
0 Subscribe


0 Replies