<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; Progress Bars</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/progress-bars/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Progress Bars within the Powershell Console</title>
		<link>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366</link>
		<comments>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366#comments</comments>
		<pubDate>Tue, 15 Sep 2009 20:14:35 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[Progress Bars]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366</guid>
		<description><![CDATA[Progress bars can be a nice visual indicator as to how a far a task is into its workload. Windows Powershell provides us with the ability to create these within the console fairly easily. This simple code will demonstrate the basics of the Write-Progress cmdlet, which allows us to deploy progress bars in our scripts. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366">Using Progress Bars within the Powershell Console</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Progress bars can be a nice visual indicator as to how a far a task is into its workload. Windows <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> provides us with the ability to create these within the console fairly easily.</p>
<p>This simple code will demonstrate the basics of the <a href="http://technet.microsoft.com/en-us/library/dd347663.aspx" target="_blank">Write-Progress</a> <a href="http://msdn.microsoft.com/en-us/library/ms714395(VS.85).aspx" target="_blank">cmdlet</a>, which allows us to deploy progress bars in our scripts.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code4'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3664"><td class="code" id="p366code4"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">100</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Activity&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$i</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">100</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Progress bar within the Powershell Console" border="0" alt="image thumb Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image_thumb.png" width="244" height="127" /></a> </p>
<p>It’s possible to spawn multiple progress bars within the Console. This may be handy for displaying the advance of child tasks. Here’s a simple example.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code5'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3665"><td class="code" id="p366code5"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$i</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">100</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Parent Task $i&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$i</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">100</span>;
	<span style="color: #008000;"># Spawn a child Progress bar specify a different Id</span>
	<span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>; <span style="color: #800080;">$x</span> <span style="color: #FF0000;">-le</span> <span style="color: #804000;">10</span>; <span style="color: #800080;">$x</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Child Task $x&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">*</span> <span style="color: #804000;">10</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
		<span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #008080; font-style: italic;">-Milliseconds</span> <span style="color: #804000;">30</span>;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Spawning multiple progress bars within the Powershell Console" border="0" alt="image thumb1 Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/image_thumb1.png" width="244" height="159" /></a></p>
<p>To show a practical example, I’ve enhanced a script from a previous post; <a href="http://www.youdidwhatwithtsql.com/check-disk-space-with-powershell-2/" target="_blank">Check disk space with Powershell</a> to use some of the code showcased here (check this for setup instructions). Enjoy!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p366code6'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3666"><td class="code" id="p366code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Issue warning if % free disk space is less </span>
<span style="color: #800080;">$percentWarning</span> <span style="color: pink;">=</span> <span style="color: #804000;">15</span>;
<span style="color: #008000;"># Get server list</span>
<span style="color: #800080;">$servers</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\serverlist.txt&quot;</span>;
<span style="color: #800080;">$datetime</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #008080; font-style: italic;">-Format</span> <span style="color: #800000;">&quot;yyyyMMddHHmmss&quot;</span>;
&nbsp;
<span style="color: #008000;"># Add headers to log file</span>
<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\server disks $datetime.txt&quot;</span> <span style="color: #800000;">&quot;server,deviceID,size,freespace,percentFree&quot;</span>;
<span style="color: #008000;"># How many servers</span>
<span style="color: #800080;">$server_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$servers</span>.Length;
<span style="color: #008000;"># processed server count</span>
<span style="color: #800080;">$i</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$server</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$servers</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$server_progress</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: pink;">/</span> <span style="color: #800080;">$server_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #008000;"># Parent progress bar</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking $server&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$server_progress</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing servers - $server_progress%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;"># Sleeping just for progress bar demo</span>
	<span style="color: #008000;"># Get fixed drive info</span>
	<span style="color: #800080;">$disks</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$server</span> <span style="color: #008080; font-style: italic;">-Class</span> Win32_LogicalDisk <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;DriveType = 3&quot;</span>;
&nbsp;
 	<span style="color: #008000;"># How many disks are there?</span>
	<span style="color: #800080;">$disk_count</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disks</span>.Length;
&nbsp;
 	<span style="color: #800080;">$x</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$disk</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$disks</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #800080;">$disk_progress</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Ceiling<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$x</span> <span style="color: pink;">/</span> <span style="color: #800080;">$disk_count</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$disk_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.Name;
		<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checking disk $disk_name&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #800080;">$disk_progress</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Processing server disks - $disk_progress%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
		<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$deviceID</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.DeviceID;
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">float</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$size</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.Size;
		<span style="color: #000000;">&#91;</span><span style="color: #008080;">float</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">=</span> <span style="color: #800080;">$disk</span>.FreeSpace;
&nbsp;
		<span style="color: #800080;">$percentFree</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">/</span> <span style="color: #800080;">$size</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$sizeGB</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$size</span> <span style="color: pink;">/</span> <span style="color: #804000;">1073741824</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
		<span style="color: #800080;">$freeSpaceGB</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Math<span style="color: #000000;">&#93;</span>::Round<span style="color: #000000;">&#40;</span><span style="color: #800080;">$freespace</span> <span style="color: pink;">/</span> <span style="color: #804000;">1073741824</span><span style="color: pink;">,</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span>;
&nbsp;
		<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Green&quot;</span>;
		<span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$percentFree</span><span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$percentWarning</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #800080;">$colour</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;Red&quot;</span>;
		<span style="color: #000000;">&#125;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #008080; font-style: italic;">-ForegroundColor</span> <span style="color: #800080;">$colour</span> <span style="color: #800000;">&quot;$server $deviceID percentage free space = $percentFree&quot;</span>;
		<span style="color: #008080; font-weight: bold;">Add-Content</span> <span style="color: #800000;">&quot;$Env:USERPROFILE\server disks $datetime.txt&quot;</span> <span style="color: #800000;">&quot;$server,$deviceID,$sizeGB,$freeSpaceGB,$percentFree&quot;</span>;
		<span style="color: #800080;">$x</span><span style="color: pink;">++</span>;
	<span style="color: #000000;">&#125;</span>
	<span style="color: #008000;"># Finish off the progress bar</span>
	<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Finshed checking disks for this server&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done - 100%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">2</span>;
	<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>; <span style="color: #008000;"># Just so we see!</span>
	<span style="color: #800080;">$i</span><span style="color: pink;">++</span>;
<span style="color: #000000;">&#125;</span>
<span style="color: #008080; font-weight: bold;">Write-Progress</span> <span style="color: #008080; font-style: italic;">-Activity</span> <span style="color: #800000;">&quot;Checked all servers&quot;</span> <span style="color: #008080; font-style: italic;">-PercentComplete</span> <span style="color: #804000;">100</span> <span style="color: #008080; font-style: italic;">-Status</span> <span style="color: #800000;">&quot;Done - 100%&quot;</span> <span style="color: #008080; font-style: italic;">-Id</span> <span style="color: #804000;">1</span>;
<span style="color: #008080; font-weight: bold;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>&#160;<a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/check_disk_powershell_progress_bars.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Powershell script checking disk space with progress bars" border="0" alt="check disk powershell progress bars thumb Using Progress Bars within the Powershell Console" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2009/09/check_disk_powershell_progress_bars_thumb.png" width="244" height="134" /></a></p>
<p><map name='google_ad_map_366_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/366?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_366_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=366&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fusing-progress-bars-within-the-powershell-console%2F366' title="Using Progress Bars within the Powershell Console" alt=" Using Progress Bars within the Powershell Console" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366">Using Progress Bars within the Powershell Console</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/using-progress-bars-within-the-powershell-console/366/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

