<?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>The Geekery &#187; Technology</title>
	<atom:link href="http://jon.netdork.net/category/technology/feed" rel="self" type="application/rss+xml" />
	<link>http://jon.netdork.net</link>
	<description>The Usual Stuff...</description>
	<lastBuildDate>Sun, 18 Jul 2010 16:53:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PowerShell: Loops, ForEach, ForEach-Object, and control functions</title>
		<link>http://jon.netdork.net/2010/07/13/powershell-loops-foreach-control-functions?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-loops-foreach-control-functions</link>
		<comments>http://jon.netdork.net/2010/07/13/powershell-loops-foreach-control-functions#comments</comments>
		<pubDate>Wed, 14 Jul 2010 04:02:14 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=915</guid>
		<description><![CDATA[In my previous post about PowerShell and BITS, I stumbled on a weird quirk in the ForEach-Object function, which had me scratching my head for a bit. In most programming languages, where you have a for, loop, while, foreach, or other such loop, there is often a set of control functions that go with it. [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F13%2Fpowershell-loops-foreach-control-functions">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F13%2Fpowershell-loops-foreach-control-functions&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>In my previous post about <a href="http://jon.netdork.net/2010/07/08/powershell-and-bits" title="The Geekery; PowerShell and BITS">PowerShell and BITS</a>, I stumbled on a weird quirk in the ForEach-Object function, which had me scratching my head for a bit.</p>

<p><span id="more-915"></span></p>

<p>In most programming languages, where you have a for, loop, while, foreach, or other such loop, there is often a set of control functions that go with it.  <em>Continue</em> and <em>Break</em>.  These functions alter the way a loop is behaving at that time.  A call to <em>Continue</em> will stop the current loop iteration at that point in the code, and jump onto the next.  A simple example would look like this:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><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;">-lt</span> <span style="color: #804000;">5</span>; <span style="color: #800080;">$i</span><span style="color: pink;">++</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$i</span> <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">continue</span>;<br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #800080;">$i</span><br />
<span style="color: #000000;">&#125;</span></div></div>

<p>This code simply takes the variable $i, sets it to 0, and whilst $i is less than 5, it loops through the code, each iteration increasing the value of $i by 1.  The if statement executes special code when $i gets to the number 2.  Now if we run this code, we get the following:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #804000;">0</span><br />
<span style="color: #804000;">1</span><br />
<span style="color: #804000;">3</span><br />
<span style="color: #804000;">4</span></div></div>

<p>What happened was the counter hit 2, and the code said that it was done with this iteration, and continue processing the outer loop.  A <em>Break</em> on the other hand just tells the loop it&#8217;s done processing, and to stop looping, and get out.  Using the same example as above, but changing <em>Continue</em> to <em>Break</em> we get the output that looks like this:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #804000;">0</span><br />
<span style="color: #804000;">1</span></div></div>

<p>This is because PowerShell has been told to stop processing any further.</p>

<p>Now for the quirk.  I discovered that <em>ForEach-Object</em> isn&#8217;t actually a programmatic keyword, it&#8217;s a cmdlet.  How does this affect the usage of the control functions?  Lets look at my code from my <a href="http://jon.netdork.net/2010/07/08/powershell-and-bits" title="The Geekery; PowerShell and BITS">BITS</a> article, and compare it to what I had.</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #800080;">$images</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.images<br />
<br />
&nbsp; &nbsp; <span style="color: #800080;">$job</span> <span style="color: pink;">=</span> <span style="color: #800080;">$null</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$img_split</span> <span style="color: pink;">=</span> <span style="color: #800080;">$images</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">'|'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <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;">-lt</span> <span style="color: #800080;">$img_split</span>.Count; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$img_split</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span>.Length <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>

<p>As you can see, I&#8217;m using the continue function inside the array of strings for the images.  If the string value is empty, I skip onto the next one.  This is what the original code looked like:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #800080;">$images</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.images<br />
&nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$images</span>.Length <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">continue</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$job</span> <span style="color: pink;">=</span> $null</div></td></tr></tbody></table></div>

<p>Whilst the code seems pretty similar, there is one distinct difference.  The first block of code is inside a loop, the second set is not.  Even though you think that <em>ForEach-Object</em> seems like a loop, it&#8217;s a cmdlet that does not behave the same way that a loop does.  How does this affect the behavior of the code?  Badly!  Because there is no loop, the control function looks for the next operation it can operate on.  It turns out that the next function it can operate on is the entire script.  It essentially <strong>kills</strong> the script right where the line is.  This means no processing of other objects in the &#8216;loop&#8217;, no continuing with the code further in the script, no nice handling of anything else in the script.  It&#8217;s done, finished, over.  My hint something wasn&#8217;t behaving right was the fact I should have had several thousand images, and yet I only had about 150.  It took 4 or 5 attempts, as well as various debug statements throughout the code to figure out why this was happening.  After I realized where it was going wrong, a Google search dropped me over to James Manning&#8217;s post <a href="http://blogs.msdn.com/b/jmanning/archive/2007/03/22/powershell-gotcha-foreach-keyword-vs-foreach-object-cmdlet.aspx" title="James Manning; PowerShell gotcha - foreach keyword vs. foreach-object cmdlet">PowerShell gotcha &#8211; foreach keyword vs. foreach-object cmdlet</a>.</p>

<p>So if you ever find yourself in need of an object loop, remember that <em>ForEach-Object</em> isn&#8217;t really a keyword, it&#8217;s a cmdlet, and does not behave the same.</p>

<p>See any mistakes? Want to add your feedback? Leave me a note in the comments, I love to hear from you.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/07/13/powershell-loops-foreach-control-functions/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	<!-- google ad injected by adsense-optimizer http://www.adsenseoptimizer.de -->
			<div  style="padding:7px; display: block; margin-left: auto; margin-right: auto; text-align: center;"><!-- Linkblock number: 1 --><script type="text/javascript"><!--
	 
google_ad_client = "pub-5380792458095798";
google_ad_width = 468;
google_ad_height = 15;
google_ad_format = "468x15_0ads_al"; google_ad_channel ="";
google_color_border = "CCCCCC";
google_color_bg = "F7F7F7";
google_color_link = "2970A6";
//--></script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>	<item>
		<title>PowerShell and BITS</title>
		<link>http://jon.netdork.net/2010/07/08/powershell-and-bits?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-and-bits</link>
		<comments>http://jon.netdork.net/2010/07/08/powershell-and-bits#comments</comments>
		<pubDate>Thu, 08 Jul 2010 20:25:23 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=906</guid>
		<description><![CDATA[Ever had to download a bunch of files from a website, and didn’t want to have to write an HTTP handler for it? This comes up on a regular basis here. We get requests from customers that have transferred from one vendor to another, and want to import all their photos to us. Here is [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F08%2Fpowershell-and-bits">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F08%2Fpowershell-and-bits&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>Ever had to download a bunch of files from a website, and didn’t want to have to write an <acronym title="HyperText Transfer Protocol">HTTP</acronym> handler for it? This comes up on a regular basis here. We get requests from customers that have transferred from one vendor to another, and want to import all their photos to us. Here is how I solve it.</p>

<p><span id="more-906"></span></p>

<h2>What is BITS?</h2>

<p><a href="http://msdn.microsoft.com/en-us/library/aa362708%28VS.85%29.aspx" title="Microsoft MSDN; About BITS">BITS</a>, or <em>Background Intelligent Transfer Service</em>, is a service bundled with Windows that is used to transfer files from websites. It was originally introduced in Windows XP RTM back in October 2001. You may not know it, but your computer uses it all the time if you have <em>Windows Automatic Updates</em> enabled.  It is an intelligent service that asynchronously transfers files, automatically adjusting bandwidth usage, throttling downloads, as bandwidth becomes available.  It can even be used to download files across computer starts.</p>

<h2>How is it useful in PowerShell?</h2>

<p>BITS is really easy to use, a few commands which I’ll show shortly, and you can download a bunch of files quickly, without having to write <acronym title="HyperText Transfer Protocol">HTTP</acronym> handler.  In my case above, I receive text files, with a list of image URLs, that need to be imported.</p>

<p>Here is how to do a very simple, single file, transfer.</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Import<span style="color: pink;">-</span>Module BitsTransfer<br />
<span style="color: #800080;">$job</span> <span style="color: pink;">=</span> Start<span style="color: pink;">-</span>BitsTransfer <span style="color: pink;">-</span>Source http:<span style="color: pink;">//</span>somedomain.com<span style="color: pink;">/</span>somefile.jpg <span style="color: #008080; font-style: italic;">-Destination</span> c:\images\somefile.jpg <span style="color: pink;">-</span>Asynchronous<br />
<span style="color: #0000FF;">while</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span>.JobState.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">'Transferring'</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-or</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span>.JobState.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">'Connecting'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #804000;">3</span><br />
<span style="color: #000000;">&#125;</span><br />
Complete<span style="color: pink;">-</span>BitsTransfer <span style="color: pink;">-</span>BitsJob $job</div></div>

<p>Relatively simple, a few lines of code for a single file.  However, BITS really shines when you’re working with multiple files to download.</p>

<p>As the files I receive are quite often comma-separated values (CSV) files, we can use the rather handy <a href="http://technet.microsoft.com/en-us/library/dd347665.aspx" title="Microsoft TechNet; Import-CSV">Import-CSV</a> function, and we build up from there.</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Import<span style="color: pink;">-</span>Module BitsTransfer<br />
<span style="color: #800080;">$filedata</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Import-CSV</span> c:\images\import.csv<br />
<span style="color: #800080;">$filedata</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$vin</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.VIN<br />
&nbsp; &nbsp; <span style="color: #800080;">$images</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Images<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #800080;">$job</span> <span style="color: pink;">=</span> <span style="color: #800080;">$null</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;Processing VIN: {0}&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$vin</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$img_split</span> <span style="color: pink;">=</span> <span style="color: #800080;">$images</span>.Split<span style="color: #000000;">&#40;</span><span style="color: #800000;">'|'</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <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;">-lt</span> <span style="color: #800080;">$img_split</span>.Count; <span style="color: #800080;">$i</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$img_split</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span>.Length <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">continue</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #800080;">$outpath</span> <span style="color: pink;">=</span> <span style="color: #800000;">'C:\Images\{0}_{1}.jpg'</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$vin</span><span style="color: pink;">,</span> <span style="color: #800080;">$i</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #800080;">$job</span> <span style="color: pink;">=</span> Start<span style="color: pink;">-</span>BitsTransfer <span style="color: pink;">-</span>Source <span style="color: #800080;">$img_split</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> <span style="color: #008080; font-style: italic;">-Destination</span> <span style="color: #800080;">$outpath</span> <span style="color: pink;">-</span>Asynchronous<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span> <span style="color: #0000FF;">else</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Add<span style="color: pink;">-</span>BitsFile <span style="color: pink;">-</span>BitsJob <span style="color: #800080;">$job</span> <span style="color: pink;">-</span>Source <span style="color: #800080;">$img_split</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$i</span><span style="color: #000000;">&#93;</span> <span style="color: #008080; font-style: italic;">-Destination</span> <span style="color: #800080;">$outpath</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span> <span style="color: #FF0000;">-ne</span> <span style="color: #800080;">$null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">while</span><span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span>.JobState.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">'Transferring'</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-or</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$job</span>.JobState.ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-eq</span> <span style="color: #800000;">'Connecting'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-weight: bold;">Sleep</span> <span style="color: #804000;">3</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Complete<span style="color: pink;">-</span>BitsTransfer <span style="color: pink;">-</span>BitsJob <span style="color: #800080;">$job</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>

<p>This is a little more complicated, but is still pretty simple.  It introduces <em>Add-BitsFile</em> which allows you to add files to an existing job, and a simple <em>ForEach</em> loop to go through an array of URLs.</p>

<p>I&#8217;ve made a few assumptions here, and thrown out all kinds of error handling in favor of a quick code turn around.  For example, the job state of a BITS job can be one of nine options (which you can see <a href="http://msdn.microsoft.com/en-us/library/aa362809(v=VS.85).aspx" title="Microsoft MSDN; BITS Job State">here</a>), but I assume only two.  I&#8217;m also not handling cases where I exceed the number of jobs, but that never happens because I only ever have one job running at a time.  I&#8217;ll probably work on tidying it up a little for other people&#8217;s use, but for now, it&#8217;s just me using it where I work, so the impact of an error is very minimal.</p>

<p>Give it a shot, download some images off of Flickr, or go a little further, and test it against downloads from Microsoft.</p>

<p>Got any questions? Hints? Tips? Leave them in the comments, I&#8217;d love to hear from you.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/07/08/powershell-and-bits/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Windows 2008, and Hibernation</title>
		<link>http://jon.netdork.net/2010/07/06/windows-2008-and-hibernation?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=windows-2008-and-hibernation</link>
		<comments>http://jon.netdork.net/2010/07/06/windows-2008-and-hibernation#comments</comments>
		<pubDate>Wed, 07 Jul 2010 01:51:18 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Sys Admin]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=903</guid>
		<description><![CDATA[Whilst driving back from our 4th of July celebrations in San Antonio, I got alerted to a server running low on disk space.&#160; As it hadn’t hit critical, and there was still a fair bit of space left, I decided to wait until I got home.&#160; When I looked at the server, I pulled up [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F06%2Fwindows-2008-and-hibernation">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F07%2F06%2Fwindows-2008-and-hibernation&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p></p><p>Whilst driving back from our 4th of July celebrations in San Antonio, I got alerted to a server running low on disk space.&#160; As it hadn’t hit critical, and there was still a fair bit of space left, I decided to wait until I got home.&#160; When I looked at the server, I pulled up my trusted space analyzer, <a href="http://www.jam-software.com/treesize_free/" target="_blank">TreeSize</a>.</p>  <span id="more-903"></span>  <p><a href="http://jon.netdork.net/wp-content/uploads/2010/07/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" class="wlDisabledImage" title="image" border="0" alt="image" align="right" src="http://jon.netdork.net/wp-content/uploads/2010/07/image_thumb.png" width="244" height="72" /></a>A quick analysis of the drive, and a report showed that there was a hefty chunk of space lost to the WinSXS directory, and 8GB of <em>Files</em> in the root of C:.&#160; This was a little odd, I know I usually use c:\temp from time to time, but never the root.&#160; To find out what was there, I first had to show hidden and system files.&#160; To do this, go to Tools, then Folder Options, then onto the View tab.&#160; There are 3 options there that we need to change.</p>  <ol>   <li>Change “Hidden files and folders” to “Show hidden files and folders”</li>    <li>Uncheck “Hide extensions for known file types”</li>    <li>Uncheck “Hide protected operating system files (Recommended)”</li> </ol>  <p>I <strong><em>highly</em></strong> recommend doing 2 all the time, you never know when somebody might sent a .txt.exe file.&#160; Changing 3 will prompt that it isn’t recommended, but do it anyway, the files I was having issues with were still hidden after changing the first two.</p>  <p>After making these changes, two files appeared that were the culprits to the lost disk space.&#160; <em>Pagefile.sys</em> and <em>Hiberfil.sys</em>.&#160; I’ll leave the explanation of the <em><a title="TechNet; What is the Page File for Anyway?" href="http://blogs.technet.com/b/askperf/archive/2007/12/14/what-is-the-page-file-for-anyway.aspx" target="_blank">Pagefile</a></em> to the experts.&#160; <em>Hiberfil.sys</em> is a file that is used to handle the state of memory, and running applications when you hibernate your computer.&#160; </p>  <h2>What is hibernation?</h2>  <p>Hibernation is a heavy sleep state for computers where all the running processes are frozen, the memory is locked, and all the states are saved into that file, then the computer is powered down.&#160; When the computer is turned back on, the operating system sees this <em>state</em> file, and starts to reload everything from there.&#160; This makes it appear like the computer was never even turned off.&#160; The applications are resumed, everything is back to the state it was before hibernation was kicked off.</p>  <h2>Why is that bad?</h2>  <p>Generally speaking, it isn’t.&#160; In fact, it’s a great thing for home computers.&#160; When you’re done reading your email at night, if you hibernate your computer, and hop into bed, your computer will be off, saving power.&#160; However this isn’t so good for servers.&#160; In 95% of cases, servers don’t ever need to hibernate.&#160; They usually have tasks, and operations that they need to run all the time, which means putting them in a state of hibernation is a bad thing as the tasks won’t be run.</p>  <h2>How do you stop this?</h2>  <p>Usually, servers have this feature turned off, but for some reason this particular one had it enabled.&#160; I’ve done some looking around, and it seems it’s may have actually been a default option to have it enabled, but for some (<em>lucky</em>) reason a handful of our other servers don’t have it on.&#160; In earlier versions of Windows, disabling the hibernation option was a case of removing a check mark from a box.&#160; This was done in the <em>Control Panel</em> under <em>Power Options</em>, and the <em>Hibernate</em> tab.&#160; However, in Windows 2008 this option was removed, and you now have to use a command line to manage this advanced option.</p>  <p></p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp;powercfg –hibernate off</div></div>

<p></p>  <p>Within about 10 seconds of issuing that command, the <em>Hiberfil.sys </em>file disappeared, and I recovered 4GB of disk space.&#160; You can read more about the <em>powercfg</em> command <a title="TechNet; Powercfg Command-Line options" href="http://technet.microsoft.com/en-us/library/cc748940%28WS.10%29.aspx" target="_blank">here</a>.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/07/06/windows-2008-and-hibernation/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Configuration Management (Part I): Introduction</title>
		<link>http://jon.netdork.net/2010/06/20/configuration-management-part-i-introduction?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=configuration-management-part-i-introduction</link>
		<comments>http://jon.netdork.net/2010/06/20/configuration-management-part-i-introduction#comments</comments>
		<pubDate>Mon, 21 Jun 2010 01:22:19 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Sys Admin]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=876</guid>
		<description><![CDATA[I&#8217;ve been asked to write a post on configuration management, and version control by a friend, Steven Klassen. Instead of a single post, I&#8217;m going to break this into several posts as some parts might be unimportant to some, and they can easily skip a whole post. I&#8217;ll be posting this over a few days, [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F20%2Fconfiguration-management-part-i-introduction">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F20%2Fconfiguration-management-part-i-introduction&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>I&#8217;ve been asked to write a post on configuration management, and version control by a friend, <a href="http://www.stevenklassen.com/" title="Steven Klassen">Steven Klassen</a>.  Instead of a single post, I&#8217;m going to break this into several posts as some parts might be unimportant to some, and they can easily skip a whole post.  I&#8217;ll be posting this over a few days, so stay tuned.  If you&#8217;re not subscribed to my <a href="http://feeds.feedburner.com/TheGeekery" title="The Geekery; RSS Feed"><acronym title="Really Simple Syndication">RSS</acronym> feed</a>, now would be a great time.</p>

<p><span id="more-876"></span></p>

<h2>The Request</h2>

<p>Earlier today, Steven asked if I used any form of version control.  It was a leading question, straight into &#8220;dev only or system files&#8221;?  His questions were targeted, he was searching for guidance.  Another question or two later, and it boils down to this:</p>

<ul>
<li>Do you use version control for system files?</li>
<li>Files like domain zone files?</li>
<li>What about multi-user environments?</li>
<li>Can you write it up?</li>
</ul>

<p>My take away on the questions boils down to two things; configuration management, and backups.  Anybody that has managed any type of system has, at some point, messed up the configuration file, and forgotten what they did to get there in the first place.  Those who have been at it a while will usually do a quick copy before they do any modifications.  But, as Steven found out in the past, best laid plans can be foiled by a <a href="http://www.stevenklassen.com/2010/06/16/murphy/" title="Steven Klassen; Murphy">simple typo</a>. </p>

<h2>Version Control</h2>

<p>This is what version control is all about.  It acts as a backup, whilst keeping a running history of what you did between changes.  Version control, or revision control as it&#8217;s also known, is big business.  Anywhere you find a big name development company, you&#8217;ll find they&#8217;ve probably developed a revision control system to go with whatever they&#8217;re offering.  Take a look at <a href="http://en.wikipedia.org/wiki/Comparison_of_revision_control_software" title="Wikipedia; Comparison of revision control software">this</a> Wikipedia entry comparing just a handful of them.</p>

<h2>Configuration Management and Version Control</h2>

<p>You don&#8217;t have to be a genius to figure out how useful version control can be for configuration management, but in case your brain often goes off to lala land like <a href="http://twitter.com/j_angliss/status/16407172868" title="Twitter; j_angliss">mine</a>, here is a run down on a few reasons you should really consider using it&#8230;</p>

<ul>
<li>What did I just change again? And why is that service not starting any more? Oh hell&#8230;</li>
<li>6 months down the road, can you figure out <em>why</em> you changed that line to Apache to start 10 daemons instead of 5? No? Most version control systems allow you to add comments to your commits.</li>
<li>As above, but why did Fred in your team change that entry back down to 7 a week later?</li>
<li>In the words of Homer&#8230; <strong>Doh!!</strong> I wasn&#8217;t supposed to delete/overwrite that file!</li>
</ul>

<p>So, there are 4 obvious cases as to why you&#8217;d want to use version control.</p>

<h2>The Breakdown</h2>

<p>As I said at the beginning, I&#8217;ll be breaking this down into several parts.  I expect them to be something along these lines:</p>

<ul>
<li>Introduction</li>
<li>Setting up <acronym title="Subversion">SVN</acronym> and the base repository</li>
<li>Using your repository</li>
<li>Educating your team members</li>
<li>Maintenance and monitoring</li>
<li>Conclusion, and follow-up ideas.</li>
</ul>

<p>If you&#8217;re interested, keep an eye open for the rest of the series.  If not, send me something you&#8217;d like to see me write about.  I&#8217;m open to suggestions, and will probably butcher anything you can throw at me.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/06/20/configuration-management-part-i-introduction/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Massive Uptimes, or the failure of them&#8230;</title>
		<link>http://jon.netdork.net/2010/06/18/massive-uptimes-or-the-failure-of-them?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=massive-uptimes-or-the-failure-of-them</link>
		<comments>http://jon.netdork.net/2010/06/18/massive-uptimes-or-the-failure-of-them#comments</comments>
		<pubDate>Sat, 19 Jun 2010 04:39:26 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Networking]]></category>
		<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=872</guid>
		<description><![CDATA[The Nubby Admin has a great post on uptimes, and the old fascination of having a large uptime. Okay, you can get your minds out the gutter now, not that kind of up time. We&#8217;re talking servers here. The post covers a hidden fear, and the goods and bads of large server uptimes. A good [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F18%2Fmassive-uptimes-or-the-failure-of-them">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F18%2Fmassive-uptimes-or-the-failure-of-them&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=SysAdmin" height="61" width="50" />
			</a>
		</div><p><a href="http://thenubbyadmin.com/" title="The Nubby Admin">The Nubby Admin</a> has a <strong>great</strong> <a href="http://thenubbyadmin.com/2010/06/16/epic-uptime-bragging-rights-or-epic-fail/?utm_source=TheGeekery&amp;utm_medium=web&amp;utm_campaign=epic-uptime-bragging-rights-or-epic-fail" title="The Nubby Admin; Epic Uptime – Bragging Rights or Epic Fail?">post</a> on uptimes, and the old fascination of having a large uptime.  Okay, you can get your minds out the gutter now, not that kind of <em>up</em> time.  We&#8217;re talking servers here.  </p>

<p>The post covers a hidden fear, and the goods and bads of large server uptimes.  A good read, and one you should look at if you&#8217;re watching your server rolling over into the third year of being running.  I get a mention (well more of a quote), and seem to fall in with the general crowd, large server uptimes are generally bad.</p>

<p>Go read, enjoy, learn something new from the great minds Wesley is surrounding himself with (myself excluded).</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/06/18/massive-uptimes-or-the-failure-of-them/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IIS and unknown file types</title>
		<link>http://jon.netdork.net/2010/06/17/iis-and-unknown-file-types?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=iis-and-unknown-file-types</link>
		<comments>http://jon.netdork.net/2010/06/17/iis-and-unknown-file-types#comments</comments>
		<pubDate>Thu, 17 Jun 2010 20:01:34 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=867</guid>
		<description><![CDATA[Since IIS6, or there abouts, IIS will not host a file that it does not understand, or doesn&#8217;t have a MIME handler for. This is a security feature, but can cause some unknown issues&#8230; We use some customer facing training software by Skillsoft. They produce a Java application which allows you to record voice, and [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F17%2Fiis-and-unknown-file-types">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F17%2Fiis-and-unknown-file-types&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=IIS,SysAdmin" height="61" width="50" />
			</a>
		</div><p>Since IIS6, or there abouts, <acronym title="Internet Information Services">IIS</acronym> will not host a file that it does not understand, or doesn&#8217;t have a <acronym title="Multipurpose Internet Mail Extension">MIME</acronym> handler for.  This is a security feature, but can cause some unknown issues&#8230;</p>

<p><span id="more-867"></span></p>

<p>We use some customer facing training software by Skillsoft.  They produce a Java application which allows you to record voice, and screen shows for training, and follow up with a questionnaire after.  Unfortunately because it&#8217;s Java, when something goes wrong, it&#8217;s very <em>blackbox</em>((A term coined to explain that you can see the outsides, but have no idea what&#8217;s going on inside)) like, making it hard to diagnose any issues.  In this case, the splash screen would load, and that&#8217;s where it&#8217;d stop.</p>

<p>Fortunately, the Java console does provide enough information to give us a hint as to what might be wrong.  When opening a Java application, the little console app appears in the notification area next to the clock.  This allows you to see if the app throws any errors.  In our case, it was complaining that a file with the extension .properties was missing from the server (<acronym title="Internet Information Services">IIS</acronym> was throwing a 404 message).  A quick showed it existed in the directory structure.</p>

<p>This is because <acronym title="Internet Information Services">IIS</acronym> doesn&#8217;t know what a <em>.properties</em> file is, or how it should be treated.  This is easily remedied.  In <acronym title="Internet Information Services">IIS</acronym> manager, either under the site, or at the root level, go to the <acronym title="Multipurpose Internet Mail Extension">MIME</acronym> option, and add it.  In this case, the file extension was .properties, and the <acronym title="Multipurpose Internet Mail Extension">MIME</acronym> Type was specified as <em>application/octet-stream</em>. If it&#8217;s done at the root server level, it applies to all sites.  If you do it on a site, it only applies to the site you changed it on.  No resets, or such, required.  Now the file is served properly.</p>

<p>Microsoft has this documented on <a href="http://support.microsoft.com/kb/326965" title="Microsoft; KB326965">KB326965</a>.  Whilst this applies to IIS6, the same applies to IIS7 as well.  To save some waiting in IIS7, you can use the appcmd to add the type for you&#8230;</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">appcmd <a href="http://www.ss64.com/nt/set.html"><span style="color: #b1b100; font-weight: bold;">set</span></a> config /section:staticContent /+&quot;<span style="color: #66cc66;">&#91;</span>fileExtension='.properties',mimeType='application/octet-stream'<span style="color: #66cc66;">&#93;</span>&quot;</div></div>

<p>This applies the setting at the root server level, and is usually inherited down to all sites.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/06/17/iis-and-unknown-file-types/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	<!-- google ad injected by adsense-optimizer http://www.adsenseoptimizer.de -->
			<div  style="padding:7px; display: block; margin-left: auto; margin-right: auto; text-align: center;"><!-- Ad number: 1 --><script type="text/javascript"><!--
    	 
    	google_ad_client = "pub-5380792458095798"; google_alternate_color = "FFFFFF";
		google_ad_width = 468; google_ad_height = 60;
		google_ad_format = "468x60_as"; google_ad_type = "text";
		google_ad_channel =""; google_color_border = "CCCCCC";
		google_color_link = "2970A6"; google_color_bg = "F7F7F7";
		google_color_text = "555555"; google_color_url = "2970A6";
		google_ui_features = "rc:10"; //--></script>
		<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div>	<item>
		<title>PowerShell; Calculated values in results</title>
		<link>http://jon.netdork.net/2010/06/11/powershell-calculated-values-in-results?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-calculated-values-in-results</link>
		<comments>http://jon.netdork.net/2010/06/11/powershell-calculated-values-in-results#comments</comments>
		<pubDate>Fri, 11 Jun 2010 09:41:51 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=848</guid>
		<description><![CDATA[Whilst sitting here on a product release call, I was reading over my twitter feed, specifically looking at #powershell, and I saw a post by @tonjoh. Feeling I had a few minutes on my hands whilst QA bashed away at the servers, I decided to take a look&#8230; I&#8217;m not sure what WMI counter is [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F11%2Fpowershell-calculated-values-in-results">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F11%2Fpowershell-calculated-values-in-results&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>Whilst sitting here on a product release call, I was reading over my twitter feed, specifically looking at <a href="http://twitter.com/#search?q=powershell" title="Twitter Search; PowerShell">#powershell</a>, and I saw a post by <a href="http://twitter.com/tonjoh/statuses/15915032860" title="Twitter; tonjoh">@tonjoh</a>.  Feeling I had a few minutes on my hands whilst QA bashed away at the servers, I decided to take a look&#8230;</p>

<p><span id="more-848"></span></p>

<p>I&#8217;m not sure what <acronym title="Windows Management Instrumentation">WMI</acronym> counter is being used as I cannot find one called free-diskspace-query, but I do know there is a Win32_LogicalDisk, which has enough information in it to tell us free space.  So here we go&#8230; Lets just get the information we want first, and build up from there&#8230;</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Format-Table</span> DeviceId<span style="color: pink;">,</span> Size<span style="color: pink;">,</span> FreeSpace</div></div>

<p>This is pretty simple, and can probably be found as an example all over the place.  For the curious, the output looks like this:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DeviceId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Size &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FreeSpace<br />
<span style="color: pink;">--------</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: pink;">----</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: pink;">---------</span><br />
C: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">119941029888</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">48341823488</span></div></div>

<p>So we can see the size (~111GB), and the amount of free space (~45GB).  Now onto the fun stuff, using expressions.</p>

<p>Expressions allow us to do calculations on the resultset that came back from the pre-piped data.  This can be something like calculating a percentage, or throwing in miscellaneous data (like requested, a timestamp).</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_LogicalDisk <span style="color: pink;">|</span> `<br />
&nbsp; &nbsp; <span style="color: #008080; font-weight: bold;">Format-Table</span> DeviceId<span style="color: pink;">,</span> Size<span style="color: pink;">,</span> FreeSpace<span style="color: pink;">,</span> `<br />
&nbsp; &nbsp; <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Name<span style="color: pink;">=</span><span style="color: #800000;">&quot;PercFree&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.FreeSpace<span style="color: pink;">/</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Size <span style="color: pink;">*</span> <span style="color: #804000;">100</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: pink;">,</span> `<br />
&nbsp; &nbsp; <span style="color: pink;">@</span><span style="color: #000000;">&#123;</span>Name<span style="color: pink;">=</span><span style="color: #800000;">&quot;ReportDate&quot;</span>;Expression<span style="color: pink;">=</span><span style="color: #000000;">&#123;</span><span style="color: #000000;">&#40;</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;yyyyMMdd-HHMMss&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span></div></div>

<p>In the above code, I&#8217;ve used the back-tick (`) to wrap the lines, and make it easier to read.  PowerShell understands this, and will treat the 4 lines as one.  I&#8217;ve created 2 extra, named, columns, one PercFree, and the other ReportDate.  The expression in the first is a calculation based on the values from the Win32_LogicalDisk output, whilst the expression for the second is just <em>extra</em> data in the form of a formatted date.  With the above code, you get the following&#8230;</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DeviceId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Size &nbsp; &nbsp;FreeSpace &nbsp; PercFree ReportDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: pink;">--------</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: pink;">----</span> &nbsp; &nbsp;<span style="color: pink;">---------</span> &nbsp; <span style="color: pink;">--------</span> <span style="color: pink;">----------</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
C: &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">119941029888</span> &nbsp;<span style="color: #804000;">48297115648</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">40</span> <span style="color: #804000;">20100611</span><span style="color: pink;">-</span>040646</div></div>

<p>And there we have it, two extra columns, which were calculated on the fly.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/06/11/powershell-calculated-values-in-results/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Saving the event of the year&#8230; Or at least the pictures</title>
		<link>http://jon.netdork.net/2010/06/08/saving-the-event-of-the-year-or-at-least-the-pictures?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=saving-the-event-of-the-year-or-at-least-the-pictures</link>
		<comments>http://jon.netdork.net/2010/06/08/saving-the-event-of-the-year-or-at-least-the-pictures#comments</comments>
		<pubDate>Wed, 09 Jun 2010 03:03:37 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[SysAdmin]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=839</guid>
		<description><![CDATA[Whilst working away this morning, I received a very pitiful look. It&#8217;s that look that we all know. The &#8220;I did something terribly wrong&#8221; kind of look. This look came with the hands of a digital camera, a small compact point and shoot. A story followed the look, as the camera was handed over to [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F08%2Fsaving-the-event-of-the-year-or-at-least-the-pictures">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F06%2F08%2Fsaving-the-event-of-the-year-or-at-least-the-pictures&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=Backup,SysAdmin" height="61" width="50" />
			</a>
		</div><p>Whilst working away this morning, I received a very pitiful look.  It&#8217;s that look that we all know.  The &#8220;I did something terribly wrong&#8221; kind of look.  This look came with the hands of a digital camera, a small compact point and shoot.  A story followed the look, as the camera was handed over to me&#8230;</p>

<p><span id="more-839"></span></p>

<blockquote>
You&#8217;ve done it before, I know you have&#8230; I don&#8217;t know what happened, if it was me, or my daughter, but all my pictures are gone.  We went to this event, and I had a lot of pictures, and now they&#8217;re all gone.  Can you save them&#8230;?
</blockquote>

<p>Fortunately for me, I know the operation for files deletes.</p>

<h2>Anatomy of a File Delete Operation</h2>

<p>What a lot of people seem to believe is when a file is deleted, the actual data is erased from the disk.  This isn&#8217;t actually true in most cases.  Really what happens behind the scene is the pointer that tells your operating system where the file is, is removed, or marked as &#8220;available&#8221;.  Essentially the index, that tells your computer where the file is, is removed.  Imagine taking whiteout to the index page in a reference book and removing information from there.  Now if you were to read every page in the reference book, you&#8217;d still find the data, but there is no &#8220;quick find&#8221; to get there.</p>

<h2>How <em>The Day</em> was saved</h2>

<p>First&#8230; Have a backup, you should have backups, but in this case, the pictures hadn&#8217;t even been taken off the camera, so chances of the person making a backup was slim, even though I knew it was likely to be slim even after it was off the camera.</p>

<p>That&#8217;s my backup rant out the way (again), now onto the fix-it.  Fortunately the camera used a storage card, so I popped it out, and put it into my card reader.  I then downloaded a copy of <a href="http://www.piriform.com/recuva" title="Piriform; Recuva">Recuva</a>.  This is freeware, but it&#8217;s not the only one, there are quite literally hundreds of different applications.  Not only is it not the only one, but most of the big name flash card manufacturers have their own software.  Once Recuva was opened, you point it to the flash card, tell it to scan, and in about 2 seconds, it&#8217;d finished reading the data on the card.  Unlike normal file access, this actually reads the bites on the card, rather than relying on the storage card to tell us about.  Once finished, you select the files you want recovered, and click the recovery button&#8230; Tada, all saved! 300+ pictures in fact, several dating back to Christmas.</p>

<h2>How it works&#8230;</h2>

<p>The <acronym title="Joint Photographics Experts Group">JPG</acronym> format is fairly easy to scan for.  There are certain markers in the file that define the start of the image, various bits of data about it (known as the EXIF data, which includes things like date taken, camera used etc), the actual image data, and an end of image marker.  When the file recovery applications scan, they look for these markers, and based on the markers they find, they should be able to identify if they can recover the image or not.  When it does the recovery steps, it reads all the bytes, from start to end, and writes to a new location.  This lets the operating system handle all the usual fun stuff like those indexes/references, and you have your file back.</p>

<h2>Caveats</h2>

<p>Now there are caveats to this whole process.  </p>

<p>First, it assumes you didn&#8217;t use a special delete program to remove the file, like <a href="http://eraser.heidi.ie/" title="Eraser">Eraser</a>, which does more than deletes the links, it goes back and writes random &#8220;stuff&#8221; over where the data actually was.  </p>

<p>Second, it assumes the system writing to the disk/memory/flash prefers completely blank space, over the space that just had data.  Most platforms prefer this because it causes less file fragmentation.</p>

<p>Lastly, this should always be considered your last resort. It&#8217;s easy for parts of the file to become overwritten because of other writes on the disk/memory/flash.  So as soon as you&#8217;ve realized you deleted it, get to work recovering it, don&#8217;t wait until next week.</p>

<p>And really, finally&#8230; Backup, backup, backup.  Did I remember to tell you to backup? No? Okay, go backup. Think it&#8217;s too hard? Go read my post on <a href="http://www.piriform.com/recuva" title="Piriform; Recuva">Home Backups</a>, and spend $50/year and get somebody else to do it.  I&#8217;m sure those memories of little Timmy&#8217;s first steps are worth it, or that winning hit at the Texas Rangers game that gets published in the news is worth it.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/06/08/saving-the-event-of-the-year-or-at-least-the-pictures/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cleaning up remote directories with PowerShell</title>
		<link>http://jon.netdork.net/2010/05/24/cleaning-up-remote-directories-with-powershell?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=cleaning-up-remote-directories-with-powershell</link>
		<comments>http://jon.netdork.net/2010/05/24/cleaning-up-remote-directories-with-powershell#comments</comments>
		<pubDate>Tue, 25 May 2010 02:19:41 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[File Management]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=822</guid>
		<description><![CDATA[Part of our application at work sends email notifications to various uses when certain criteria are met. This is great, except testing is difficult as you really don&#8217;t want to send out mails to valid customers, so we disable relaying outbound, and the local mail server just chomps on it a bit, then decides to [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F05%2F24%2Fcleaning-up-remote-directories-with-powershell">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F05%2F24%2Fcleaning-up-remote-directories-with-powershell&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=File+Management,PowerShell,WMI" height="61" width="50" />
			</a>
		</div><p>Part of our application at work sends email notifications to various uses when certain criteria are met.  This is great, except testing is difficult as you really don&#8217;t want to send out mails to valid customers, so we disable relaying outbound, and the local mail server just chomps on it a bit, then decides to not deliver it and drops it to the local file system.  This is great, except in a the matter of days, we have 100k message files.  This becomes entirely unmanageable with Windows, so PowerShell is here to help&#8230;</p>

<p><span id="more-822"></span></p>

<p>There are plenty of articles on file management with PowerShell, pretty much all of them revolving around the <strong><a href="http://technet.microsoft.com/en-us/library/ee176841.aspx" title="Microsoft TechNet; Get-ChildItem">Get-ChildItem</a></strong> function.  This usually works pretty well, but is terrible on memory when it comes to handling this many files, and working with filters (only selecting files that are X days old).</p>

<p>This is when my brain clicked to a <a href="http://powershell.com" title="PowerShell.com">PowerShell.com</a> tip a few weeks back about <a href="http://powershell.com/cs/blogs/tips/archive/2010/04/15/wmi-server-side-filtering.aspx" title="PowerShell.com; WMI Server Side Filtering">Windows Management Instrumentation (<acronym title="Windows Management Instrumentation">WMI</acronym>) based filters</a>.  I won&#8217;t go into the dark recesses of my brain, and try an explain why I have a weird ability to remember some weird function that I used 5 years ago, but I&#8217;ll save you some work and introduce you to the <acronym title="Windows Management Instrumentation">WMI</acronym> class <a href="http://msdn.microsoft.com/en-us/library/aa387236(v=VS.85).aspx" title="MSDN; CIM_DataFile">CIM_DataFile</a>.  The cool thing about using <acronym title="Windows Management Instrumentation">WMI</acronym> to do this, you can use the server side filters to do most of the dirty work for you.  So lets see the example:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> CIM_DataFile <span style="color: pink;">-</span>Computer yourremoteserver `<br />
&nbsp; &nbsp; <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Drive='C:' and Path='\\InetPub\\MailRoot\\BadMail\\'&quot;</span></div></div>

<p>Okay, that was pretty easy&#8230; right?  That just gets all the files in the folder C:\inetpub\mailroot\badmail on the server yourremoteserver.  Now we want to limit it to just keeping the mail from the last 3 days.  Before I did into the code that does this, the date format on CIM_DataFile through PowerShell is a little &#8216;odd&#8217;.  Don&#8217;t believe me?  Try this:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">Get-WmiObject</span> CIM_DataFile `<br />
&nbsp; &nbsp; <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Drive='C:' and Path='\\Temp\\'&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Select</span> FileName<span style="color: pink;">,</span>CreationDate</div></div>

<p>You will probably end up with something that looks like this:</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">FileName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreationDate<br />
<span style="color: pink;">--------</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: pink;">------------</span><br />
process_usage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #804000;">20100407083839.775225</span><span style="color: pink;">-</span><span style="color: #804000;">300</span><br />
proxy1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">20100517150112.509957</span><span style="color: pink;">-</span><span style="color: #804000;">300</span><br />
proxy2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">20100517150105.506804</span><span style="color: pink;">-</span><span style="color: #804000;">300</span><br />
Registration &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">20100131221914.239533</span><span style="color: pink;">-</span><span style="color: #804000;">360</span><br />
sql_job_status &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">20100401160936.442674</span><span style="color: pink;">-</span><span style="color: #804000;">300</span></div></div>

<p>Okay, not too odd, it&#8217;s really just the date in the format YYYYmmddhhmmss.nnnnnn-tzo. n is milliseconds, and tzo is the timezone offset in minutes.  In the above example, 300 would be 5 hours, 360 is 6 hours.  This means, for the query, we have to modify slightly.</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span><br />
<span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #800080;">$date</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$sDate</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;{0:00}{1:00}{2:00}000000.000000-000&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$date</span>.Year<span style="color: pink;">,</span> <span style="color: #800080;">$date</span>.Month<span style="color: pink;">,</span> <span style="color: #800080;">$date</span>.Day<br />
<span style="color: #008080; font-weight: bold;">Get-WmiObject</span> CIM_DataFile <span style="color: pink;">-</span>Computer yourremoteserver `<br />
&nbsp; &nbsp; <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Drive='C:' and Path='\\InetPub\\MailRoot\\BadMail\\' and CreationDate &lt;= '$sDate'&quot;</span></div></td></tr></tbody></table></div>

<p>You might be wondering what that weird stuff on line 3 is, it&#8217;s the PowerShell equivalent to C#&#8217;s <a href="http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.71).aspx" title="MSDN; c# string.format">string.Format</a>.  This takes the date we had, and reformats it nicely for <acronym title="Windows Management Instrumentation">WMI</acronym> to use.  This is much nicer, gets <acronym title="Windows Management Instrumentation">WMI</acronym> to do all the grunt work, and is much more friendly on the server (memory/CPU).  Next, the delete&#8230;</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span><br />
<span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #800080;">$date</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$sDate</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;{0:00}{1:00}{2:00}000000.000000-000&quot;</span> <span style="color: #FF0000;">-f</span> <span style="color: #800080;">$date</span>.Year<span style="color: pink;">,</span> <span style="color: #800080;">$date</span>.Month<span style="color: pink;">,</span> <span style="color: #800080;">$date</span>.Day<br />
<span style="color: #008080; font-weight: bold;">Get-WmiObject</span> CIM_DataFile <span style="color: pink;">-</span>Computer yourremoteserver `<br />
&nbsp; &nbsp; <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;Drive='C:' and Path='\\InetPub\\MailRoot\\BadMail\\' and CreationDate &lt;= '$sDate'&quot;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span><span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Delete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span></div></div>

<p>As I mentioned at the beginning, it&#8217;s possible to use the <strong>Get-ChildItem</strong> function to do the same thing (except it&#8217;s not remote), so I figured I&#8217;d show how too&#8230;</p>

<div class="codecolorer-container posh default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="posh codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span><br />
<span style="color: #800080;">$date</span> <span style="color: pink;">=</span> <span style="color: #800080;">$date</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">'C:\inetpub\mailroot\badmail'</span> <span style="color: pink;">|</span> `<br />
&nbsp; &nbsp; <span style="color: pink;">?</span> <span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.CreationDate <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$date</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> `<br />
&nbsp; &nbsp; <span style="color: #008080; font-weight: bold;">ForEach-Object</span><span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Delete<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span></div></div>

<p>The problem you can see here, the filtering is done <em>after</em> all the items are fetched, rather than restricting the fetched list to begin with.  This cannot be executed remotely either (unless using network shares), so running this from a central job server is a little more tricky.</p>

<p>I&#8217;m sticking with the <acronym title="Windows Management Instrumentation">WMI</acronym> method for now, it&#8217;s fast, memory and CPU friendly, and does the job just as nicely.  How do you guys manage remote files with PowerShell? Any hints and tips?</p>

<p><strong>Edit:</strong> Thanks to <a href="http://www.stevenklassen.com/" title="Steven Klassen; Knowledge exists to be imparted.">Steven Klassen</a>, I edited some of the code.  Apparently the CodeColorer plugin was escaping my escape and only showing a single backslash on the end of some of the commands.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/05/24/cleaning-up-remote-directories-with-powershell/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Screenshot sharing&#8230;</title>
		<link>http://jon.netdork.net/2010/05/17/screenshot-sharing?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=screenshot-sharing</link>
		<comments>http://jon.netdork.net/2010/05/17/screenshot-sharing#comments</comments>
		<pubDate>Tue, 18 May 2010 05:17:26 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[screenshot]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=820</guid>
		<description><![CDATA[From time to time, I have to share screen shots, it&#8217;s a common task for some, especially when showing errors and the likes. This is usually fine because it can be emailed, or saved as an image file for attaching to a bug ticket. I&#8217;ve started sharing some screen shots online, which now requires a [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F05%2F17%2Fscreenshot-sharing">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F05%2F17%2Fscreenshot-sharing&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=screenshot" height="61" width="50" />
			</a>
		</div><p>From time to time, I have to share screen shots, it&#8217;s a common task for some, especially when showing errors and the likes.  This is usually fine because it can be emailed, or saved as an image file for attaching to a bug ticket.  I&#8217;ve started sharing some screen shots online, which now requires a few more steps, and becomes quite time consuming, screen shot, save image, upload to some site.  This is where <a href="http://www.jingproject.com/" title="Jing; Add visuals to your online conversations">jing</a> came in&#8230;</p>

<p><span id="more-820"></span></p>

<p>Jing is an application that can be used to take screen shots, or videos.  It has an added bonus that it is backed by <a href="http://www.screencast.com" title="TechSmith; Screencast.com">Screencast.com</a>.  With the two combined, you could take a screenshot, have it uploaded, and get a <acronym title="Uniform Resource Locator">URL</acronym> to send to other people in as little as 3 clicks.  Pretty cool, especially as you can do video as well.</p>

<p>However, I was struggling with a <a href="http://www.screencast.com/users/jangliss/folders/Jing/media/026d4a5d-7468-4f92-a710-6dda41462c34" title="Jing Media; Memory Usage">memory issue</a>, upwards of 300MB after taking a single screen shot.  The Jing folks <a href="http://twitter.com/JingTips/status/12104798824" title="Twitter - JingTips">followed</a> up on my tweet with an interesting posting on <a href="http://blog.jingproject.com/2008/04/story-of-improving-jing-memory.html" title="Jing; Good, Better, Best... Bester?">memory management</a>.  It details some interesting stuffs about .NET and the Garbage Collector.  Whilst this is a great post, and provides some excellent insight into some application memory management, it doesn&#8217;t resolve my memory issues.  So I am stubborn, and went looking for some other options.</p>

<p>I stumbling across a site called <a href="http://alternativeto.net/desktop/jing/" title="AlternativeTo; Jing">AlternativeTo</a>, which has references to possible alternatives to applications.  A couple of the applications looked interesting, but not quite what I wanted.  Then I stumbled upon <a href="http://code.google.com/p/zscreen/" title="ZScreen">zscreen</a>.  Another .NET application which seems quite a bit less memory hungry.  The UI is basic, but stuffed with options.  Including not limiting your uploads to just a specific site, with built in support for RapidShare, SendSpace, Flickr, ImageShack, TinyPic, Twitter, ImageBarn, and MindTouch to name a few.  That&#8217;s not the only option either, with <acronym title="File Transfer Protocol">FTP</acronym> support an option too, you can push to your own private site, and generate URLs for your use.</p>

<p>Whilst jing faithfully served me as the little glowing yellow ball at the top of my screen for a while, <a href="http://code.google.com/p/zscreen/" title="ZScreen">zscreen</a> has become my screenshot taker of choice.  Give it a shot, worth playing with.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/05/17/screenshot-sharing/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
