PowerShell Mini-Scripting Games 2014: Answer 3

Doctor Scripto

Summary: Here is the answer to Problem 3 in Windows PowerShell Mini-Scripting Games 2014. Microsoft Scripting Guy, Ed Wilson, is here. Today it is time to look at Problem 3. Last week, I posted the complete Problem 3 description and task outline.

Problem 3 synopsis

One of your teammates wrote a script that runs against a select subgroup of users once a week. This script takes a rather long time to complete (upwards of five minutes in some cases). Therefore, the sign-in appears to hang when it is their turn for processing. Users have done everything (called the Help Desk, rebooted the computers, booted up with the network cable unplugged) until they completed signing in with cached credentials to get around the annoying problem. Your manager has tasked you with providing some sort of visual feedback to show the progress of the script. He did not task you with improving the script or figuring out another way to accomplish the task. He only wants you to write some code that will provide visual feedback to the users that something is happening, and that their computers are not “locked up.”

Write-Progress to the rescue

Windows PowerShell includes the Write-Progress cmdlet to provide visual feedback on the progress of one or more tasks that the script performs. For this problem, this Windows PowerShell cmdlet is ideal. It can provide notification about the task being performed, the current status of the activity, and even the progress towards completion. To make the script output a bit easier to track, I included the Start-Sleep command to cause the script to pause for a half-second before each increment. To demonstrate using the Write-Progress cmdlet, I will simply monitor progress towards counting to the number 25. This is an ideal place to use the For statement. First, I count to 25. I do this by beginning at 1 and continuing while the number $i is less than or equal to 25. After each iteration, I increment the value of $i by 1. This is done by using $i++. Here is my For statement:

For($i=1; $i -le 25 ; $i++) In the script block for the For statement, I use the Write-Progress cmdlet. The activity is “Counting to 25”, and the status is a display of the value of $i at that moment. I calculate the percentage of completeness, and I use that to display the progress bar. Here is that portion of the script:

Write-Progress -Activity “Counting to 25” -Status “

   Currently at $i” -PercentComplete($i / 25 * 100) After the Write-Progress command, I sleep for a half-second. This is the command that accomplishes that task:

Start-Sleep -Milliseconds 500  The complete script is shown here:

For($i=1; $i -le 25 ; $i++)

 { Write-Progress -Activity “Counting to 25” -Status “

   Currently at $i” -PercentComplete($i / 25 * 100)

   Start-Sleep -Milliseconds 500

   } That is all there is to solving Problem 3. Mini-Scripting Games Answer Week will continue tomorrow when I will talk about solving Problem 4. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon