{"id":802,"date":"2014-08-20T00:01:00","date_gmt":"2014-08-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/08\/20\/powershell-mini-scripting-games-2014-answer-3\/"},"modified":"2014-08-20T00:01:00","modified_gmt":"2014-08-20T00:01:00","slug":"powershell-mini-scripting-games-2014-answer-3","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-mini-scripting-games-2014-answer-3\/","title":{"rendered":"PowerShell Mini-Scripting Games 2014: Answer 3"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Here is the answer to Problem 3 in Windows PowerShell Mini-Scripting Games 2014.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here. Today it is time to look at Problem 3. Last week, I posted <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2014\/08\/13\/powershell-mini-scripting-games-2014-problem-3.aspx\" target=\"_blank\">the complete Problem 3 description and task outline<\/a>.<\/p>\n<h2>Problem 3 synopsis<\/h2>\n<p>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.\nYour 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 &ldquo;locked up.&rdquo;<\/p>\n<h2>Write-Progress to the rescue<\/h2>\n<p>Windows PowerShell includes the <b>Write-Progress<\/b> 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 <b>Start-Sleep<\/b> command to cause the script to pause for a half-second before each increment.\nTo demonstrate using the <b>Write-Progress<\/b> cmdlet, I will simply monitor progress towards counting to the number 25. This is an ideal place to use the <b>For<\/b> statement. First, I count to 25. I do this by beginning at 1 and continuing while the number <b>$i<\/b> is less than or equal to 25. After each iteration, I increment the value of <b>$i<\/b> by 1. This is done by using <b>$i++<\/b>. Here is my <b>For<\/b> statement:<\/p>\n<p style=\"margin-left:30px\">For($i=1; $i -le 25 ; $i++)\nIn the script block for the <b>For<\/b> statement, I use the <b>Write-Progress<\/b> cmdlet. The activity is <b>&ldquo;Counting to 25&rdquo;<\/b>, and the status is a display of the value of <b>$i<\/b> 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:<\/p>\n<p style=\"margin-left:30px\">Write-Progress -Activity &#8220;Counting to 25&#8221; -Status &#8220;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Currently at $i&#8221; -PercentComplete($i \/ 25 * 100)\nAfter the <b>Write-Progress<\/b> command, I sleep for a half-second. This is the command that accomplishes that task:<\/p>\n<p style=\"margin-left:30px\">Start-Sleep -Milliseconds 500&nbsp;\nThe complete script is shown here:<\/p>\n<p style=\"margin-left:30px\">For($i=1; $i -le 25 ; $i++)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{ Write-Progress -Activity &#8220;Counting to 25&#8221; -Status &#8220;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Currently at $i&#8221; -PercentComplete($i \/ 25 * 100)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Start-Sleep -Milliseconds 500<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; }\nThat is all there is to solving Problem 3. Mini-Scripting Games Answer Week will continue tomorrow when I will talk about solving Problem 4.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[191,521,3,4,45,77],"class_list":["post-802","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-community","tag-mini-scripting-games","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-writing"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=802"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/802\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}