<?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; PowerShell</title>
	<atom:link href="http://jon.netdork.net/category/technology/powershell-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>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>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>PowerShell: Restart remote services</title>
		<link>http://jon.netdork.net/2010/04/10/powershell-restart-remote-services?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-restart-remote-services</link>
		<comments>http://jon.netdork.net/2010/04/10/powershell-restart-remote-services#comments</comments>
		<pubDate>Sat, 10 Apr 2010 22:34:38 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=760</guid>
		<description><![CDATA[In a follow up to @ScriptingGuys post about restarting services using VBScript, here is the same using PowerShell. I&#8217;ll probably be dragging the same onto some server monitoring stuff to get a service back up and running we&#8217;ve been having a weird issue with. $names = Get-Content c:\temp\computers.txt foreach&#40;$name in $names&#41; &#123; &#160; &#160; $svc [...]]]></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%2F04%2F10%2Fpowershell-restart-remote-services">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F04%2F10%2Fpowershell-restart-remote-services&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>In a follow up to @<a href="http://twitter.com/ScriptingGuys" title="Twitter; Scripting Guys">ScriptingGuys</a> post about <a href="http://twitter.com/ScriptingGuys/status/11949671215" title="ScriptingGuys; VBScript to restart SNMP service">restarting services using VBScript</a>, here is the same using PowerShell.  I&#8217;ll probably be dragging the same onto some server monitoring stuff to get a service back up and running we&#8217;ve been having a weird issue with.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$names</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> c:\temp\computers.txt<br />
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$name</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$names</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$svc</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-WmiObject</span> Win32_Service <span style="color: #008080; font-style: italic;">-ComputerName</span> <span style="color: #800080;">$name</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: pink;">-</span><span style="color: #0000FF;">Filter</span> <span style="color: #800000;">&quot;name='wuauserv'&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$svc</span>.started <span style="color: #FF0000;">-eq</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #800080;">$svc</span>.StopService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$svc</span>.StartService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#125;</span></div></div>

<p>This takes a file &#8220;computers.txt&#8221;, loops through the data, and uses <acronym title="Windows Management Instrumentation">WMI</acronym> to connect to the remote machines to get the <em>wuauserv</em> service (or in plain English Windows Automatic Update).  Easy as pie.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/04/10/powershell-restart-remote-services/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell: Top x Processes using CPU</title>
		<link>http://jon.netdork.net/2010/04/06/powershell-top-x-processes-using-cpu?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-top-x-processes-using-cpu</link>
		<comments>http://jon.netdork.net/2010/04/06/powershell-top-x-processes-using-cpu#comments</comments>
		<pubDate>Wed, 07 Apr 2010 01:31:38 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[Monitoring]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=733</guid>
		<description><![CDATA[This is a quick and handy one for server monitoring, and tracking down that process that is using all your CPU. It makes use of WMI counters. Get-WmiObject Win32_PerfFormattedData_PerfProc_Process &#124; ` &#160; where-object&#123; $_.Name -ne &#34;_Total&#34; -and $_.Name -ne &#34;Idle&#34;&#125; &#124; ` &#160; Sort-Object PercentProcessorTime -Descending &#124; ` &#160; select -First 5 &#124; ` &#160; [...]]]></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%2F04%2F06%2Fpowershell-top-x-processes-using-cpu">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F04%2F06%2Fpowershell-top-x-processes-using-cpu&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=CPU,Monitoring,PowerShell" height="61" width="50" />
			</a>
		</div><p>This is a quick and handy one for server monitoring, and tracking down that process that is using all your CPU.  It makes use of <acronym title="Windows Management Instrumentation">WMI</acronym> counters.</p>

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

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell 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_PerfFormattedData_PerfProc_Process <span style="color: pink;">|</span> `<br />
&nbsp; <span style="color: #008080; font-weight: bold;">where-object</span><span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;_Total&quot;</span> <span style="color: #FF0000;">-and</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Name <span style="color: #FF0000;">-ne</span> <span style="color: #800000;">&quot;Idle&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> `<br />
&nbsp; <span style="color: #008080; font-weight: bold;">Sort-Object</span> PercentProcessorTime <span style="color: #008080; font-style: italic;">-Descending</span> <span style="color: pink;">|</span> `<br />
&nbsp; <span style="color: #008080; font-weight: bold;">select</span> <span style="color: #008080; font-style: italic;">-First</span> <span style="color: #804000;">5</span> <span style="color: pink;">|</span> `<br />
&nbsp; <span style="color: #008080; font-weight: bold;">Format-Table</span> Name<span style="color: pink;">,</span>IDProcess<span style="color: pink;">,</span>PercentProcessorTime <span style="color: #008080; font-style: italic;">-AutoSize</span></div></div>

<p>Okay, this one is a mouthful of pipes, and formatting to make it easier to see.  But the breakdown is pretty easy&#8230;</p>

<ul>
<li>Get the process information</li>
<li>Exclude _Total and Idle</li>
<li>Sort the output by descending processor usage</li>
<li>Only get the first 5 rows</li>
<li>Format the results into a table, with the columns Name, Process ID, and processor usage.</li>
</ul>

<p>Output looks something like this:</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Name &nbsp; &nbsp; &nbsp; IDProcess PercentProcessorTime<br />
<span style="color: pink;">----</span> &nbsp; &nbsp; &nbsp; <span style="color: pink;">---------</span> <span style="color: pink;">--------------------</span><br />
chrome<span style="color: #008000;">#2 &nbsp; &nbsp; &nbsp; &nbsp;3912 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 24</span><br />
System &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">4</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">12</span><br />
svchost<span style="color: #008000;">#12 &nbsp; &nbsp; &nbsp;4276 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0</span><br />
mobsync &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #804000;">5688</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #804000;">0</span><br />
wmpnetwk &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #804000;">3396</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #804000;">0</span></div></div>

<p>Another handy hint here, the back-tick is PowerShell&#8217;s way of allowing you to line-wrap.  This makes it a little easier to read, especially when giving it as samples like above.</p>

<p>I&#8217;ll be bundling parts of this for Nagios to return what processes are using the most processor when I get CPU alerts, allowing me to make quicker judgments on what I need to do to correct the issue.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/04/06/powershell-top-x-processes-using-cpu/feed</wfw:commentRss>
		<slash:comments>2</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: Timing Commands</title>
		<link>http://jon.netdork.net/2010/04/06/powershell-timing-commands?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-timing-commands</link>
		<comments>http://jon.netdork.net/2010/04/06/powershell-timing-commands#comments</comments>
		<pubDate>Tue, 06 Apr 2010 15:07:12 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[General Ramblings]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=728</guid>
		<description><![CDATA[Curious about how long your script took to execute? How about just that cmdlet? Powershell has a built in function for you. Open Powershell command prompt Use the command Measure-Command like so: Measure-Command &#123;c:\scripts\yourscript.ps1&#125; Enjoy the output broken down by days, hours, minutes, seconds, milliseconds, and ticks. Days &#160; &#160; &#160; &#160; &#160; &#160; &#160;: [...]]]></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%2F04%2F06%2Fpowershell-timing-commands">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F04%2F06%2Fpowershell-timing-commands&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly" height="61" width="50" />
			</a>
		</div><p>Curious about how long your script took to execute? How about just that cmdlet?  Powershell has a built in function for you.</p>

<ul>
<li>Open Powershell command prompt</li>
<li>Use the command <code>Measure-Command</code> like so:</li>
</ul>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">Measure-Command</span> <span style="color: #000000;">&#123;</span>c:\scripts\yourscript.ps1<span style="color: #000000;">&#125;</span></div></div>

<p>Enjoy the output broken down by days, hours, minutes, seconds, milliseconds, and ticks.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Days &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0</span><br />
Hours &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0</span><br />
Minutes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0</span><br />
Seconds &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">34</span><br />
Milliseconds &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">287</span><br />
Ticks &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">342873445</span><br />
TotalDays &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0.000396844265046296</span><br />
TotalHours &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0.00952426236111111</span><br />
TotalMinutes &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0.571455741666667</span><br />
TotalSeconds &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">34.2873445</span><br />
TotalMilliseconds : <span style="color: #804000;">34287.3445</span></div></div>

<p>The same works for cmdlets too&#8230;</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-weight: bold;">PS</span> C:\scripts<span style="color: pink;">&gt;</span> <span style="color: #008080; font-weight: bold;">Measure-Command</span> <span style="color: #000000;">&#123;</span><span style="color: #008080; font-weight: bold;">Get-Process</span><span style="color: #000000;">&#125;</span><br />
<br />
Days &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0</span><br />
Hours &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0</span><br />
Minutes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0</span><br />
Seconds &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">0</span><br />
Milliseconds &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">6</span><br />
Ticks &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : <span style="color: #804000;">65919</span><br />
TotalDays &nbsp; &nbsp; &nbsp; &nbsp; : 7.62951388888889E<span style="color: pink;">-</span>08<br />
TotalHours &nbsp; &nbsp; &nbsp; &nbsp;: 1.83108333333333E<span style="color: pink;">-</span>06<br />
TotalMinutes &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0.000109865</span><br />
TotalSeconds &nbsp; &nbsp; &nbsp;: <span style="color: #804000;">0.0065919</span><br />
TotalMilliseconds : <span style="color: #804000;">6.5919</span></div></div><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/04/06/powershell-timing-commands/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PowerShell: Cleaning up IIS Logs</title>
		<link>http://jon.netdork.net/2010/04/01/powershell-cleaning-up-iis-logs?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-cleaning-up-iis-logs</link>
		<comments>http://jon.netdork.net/2010/04/01/powershell-cleaning-up-iis-logs#comments</comments>
		<pubDate>Thu, 01 Apr 2010 14:44:22 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[IIS]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=720</guid>
		<description><![CDATA[This one is a quick and easy one. We have multiple IIS boxes, which each generate large usage logs on a daily basis. To save space, and for analytics off-server, we compress the log files. Having found Rar is one of the better compression algorithms, we simply shell out to rar, compress the logs, 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%2F04%2F01%2Fpowershell-cleaning-up-iis-logs">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F04%2F01%2Fpowershell-cleaning-up-iis-logs&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=IIS,PowerShell" height="61" width="50" />
			</a>
		</div><p>This one is a quick and easy one.  We have multiple <acronym title="Internet Information Services">IIS</acronym> boxes, which each generate large usage logs on a daily basis.  To save space, and for analytics off-server, we compress the log files.  Having found <a href="http://www.rarlabs.com" title="RarLabs">Rar</a> is one of the better compression algorithms, we simply shell out to rar, compress the logs, and cleanup older logs.</p>

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

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$winrar</span> <span style="color: pink;">=</span> <span style="color: #800000;">'d:\program files\winrar\rar.exe'</span><br />
<br />
<span style="color: #800080;">$path</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$args</span></a><span style="color: #000000;">&#91;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#93;</span><br />
<br />
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800080;">$path</span> <span style="color: #008080; font-style: italic;">-recurse</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">where</span><span style="color: #000000;">&#123;</span><a href="about:blank"><span style="color: #000080;">$_</span></a>.Extension <span style="color: #FF0000;">-match</span> <span style="color: #800000;">&quot;log&quot;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Foreach-Object</span> `<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #800080;">$log_name</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.FullName<br />
&nbsp; &nbsp; <span style="color: #800080;">$rar_name</span> <span style="color: pink;">=</span> <span style="color: #800080;">$log_name</span>.Replace<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;.log&quot;</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;.rar&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <a href="about:blank"><span style="color: #000080;">$args</span></a> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;-ri1 m $rar_name $log_name&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000;">&#91;</span>Diagnostics.Process<span style="color: #000000;">&#93;</span>::Start<span style="color: #000000;">&#40;</span><span style="color: #800080;">$winrar</span><span style="color: pink;">,</span> <a href="about:blank"><span style="color: #000080;">$args</span></a><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008080; font-weight: bold;">Start-Sleep</span> <span style="color: pink;">-</span>s <span style="color: #804000;">5</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000000;">&#125;</span><br />
<br />
<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;">$month</span> <span style="color: pink;">=</span> <span style="color: #800080;">$date</span>.Month<br />
<span style="color: #800080;">$year</span> <span style="color: pink;">=</span> <span style="color: #800080;">$date</span>.Year<br />
<br />
<span style="color: #800080;">$new_date</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Date</span> <span style="color: #800000;">&quot;$month/01/$year&quot;</span><br />
<span style="color: #800080;">$log_date</span> <span style="color: pink;">=</span> <span style="color: #800080;">$new_date</span>.AddMonths<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #804000;">3</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800080;">$path</span> <span style="color: #008080; font-style: italic;">-recurse</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.Extension <span style="color: #FF0000;">-match</span> <span style="color: #800000;">&quot;rar&quot;</span> <span style="color: #FF0000;">-and</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.LastWriteTime <span style="color: #FF0000;">-le</span> <span style="color: #800080;">$log_date</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">ForEach-Object</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #008080; font-weight: bold;">Remove-Item</span> <a href="about:blank"><span style="color: #000080;">$_</span></a>.FullName<br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>

<p>This could probably do with some cleaning up as there are probably some slightly more efficient ways of doing some of this, but it works.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/04/01/powershell-cleaning-up-iis-logs/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PowerShell: Remote Registry, and fixing an Office 2007 install</title>
		<link>http://jon.netdork.net/2010/03/29/powershell-remote-registry-and-fixing-an-office-2007-install?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-remote-registry-and-fixing-an-office-2007-install</link>
		<comments>http://jon.netdork.net/2010/03/29/powershell-remote-registry-and-fixing-an-office-2007-install#comments</comments>
		<pubDate>Mon, 29 Mar 2010 16:10:33 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[KB928218]]></category>
		<category><![CDATA[Remote Registry]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=688</guid>
		<description><![CDATA[We&#8217;ve had a stubborn Windows 2003 server that we&#8217;ve been trying to get Office 2007 installed for a while. It has a couple of things wrong with it which have made the install substantially harder. The first being an MSI speed issue. Because the server plays host to about 120+ custom applications, all installed using [...]]]></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%2F03%2F29%2Fpowershell-remote-registry-and-fixing-an-office-2007-install">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2010%2F03%2F29%2Fpowershell-remote-registry-and-fixing-an-office-2007-install&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=KB928218,Microsoft,PowerShell,Remote+Registry" height="61" width="50" />
			</a>
		</div><p>We&#8217;ve had a stubborn Windows 2003 server that we&#8217;ve been trying to get Office 2007 installed for a while.  It has a couple of things wrong with it which have made the install substantially harder.  </p>

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

<p>The first being an MSI speed issue.  Because the server plays host to about 120+ custom applications, all installed using MSI, any further MSI installs simply drag whilst it reads through all the keys, does its validation and backup checks, and then proceeds.</p>

<p>The second issue we have is that this box has a partially installed, corrupted Office 2007 install already on here.  Attempting to do &#8220;upgrades&#8221; fail, and uninstalling just barfs with a delightful error.  This resulted in some stumbling to figure out how to get it off, and start again.</p>

<p>Fortunately Microsoft do frequently document things like this, you just have to find the right search terms to find it, which luckily I did, in the form of <a href="http://support.microsoft.com/kb/928218/" title="Microsoft Support; KB928218; How to manually uninstall the 2007 Office system if you cannot uninstall it by using the 'Add or Remove Programs' feature">KB928218</a>.  This article gives you a list of the registry keys you have to go through and remove, and you should be all well and good.  Unfortunately, this list is over 200 registry keys on our count, and quite a list all over the place.  This is where Powershell comes in handy&#8230;</p>

<p>As the server itself doesn&#8217;t (yet) have Powershell installed, I ran the commands from my laptop, which required remote registry access.  Powershell makes use of the .Net libraries to do this:</p>

<div class="codecolorer-container powershell 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 /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #800000;">'MyRemoteServer'</span><br />
<span style="color: #800080;">$reg</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'LocalMachine'</span><span style="color: pink;">,</span> <span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>

<p>The <em>LocalMachine</em> reference here is the part of the registry we want to open, which is equivalent to HKEY_LOCAL_MACHINE.  This now gives us a registry object in the HKLM section of the registry.  From here, there are a bunch of functions we can use to fetch the keys we need.  The first is <em>OpenSubKey</em>.  This opens a subkey in the <em>$reg</em> object.</p>

<div class="codecolorer-container powershell 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>4<br />5<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800000;">&quot;Removing Office 12 Software Keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Office'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>

<p>This assigns <em>$regKey</em> to the subkey.  The boolean flag <em>$true</em> on the end is to make sure the key is opened in writable mode.  This will be required to modify the contents of the key.</p>

<p>Per the <acronym title="Kilobyte">KB</acronym> article, Microsoft say to delete all of the 12.0 subkey from under here.  I had originally used the following:</p>

<div class="codecolorer-container powershell 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>6<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$regKey</span>.DeleteSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'12.0'</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>

<p>This resulted in an error about not being allowed to delete subkeys.  Then I stumbled upon the much more suitable <em>DeleteSubKeyTree</em>, which, as the name hints, deletes the tree.  So the new code became:</p>

<div class="codecolorer-container powershell 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>6<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$regKey</span>.DeleteSubKeyTree<span style="color: #000000;">&#40;</span><span style="color: #800000;">'12.0'</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>

<p>This was repeated for the various other subkeys Microsoft recommended deleting.  It became more complex when it asks you to delete keys that match some of the characters below, and use a * as the match.  For example:</p>

<blockquote>
Note In the following registry keys, the asterisk character (*) represents one or more characters in the subkey name.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*0FF1CE*
</blockquote>

<p>Fortunately, Powershell is very good at string manipulation, and queries, so this was a bit of a case for a where, and foreach loop.</p>

<div class="codecolorer-container powershell 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>23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*0FF1CE*&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #800080;">$regKey</span>.DeleteSubKeyTree<span style="color: #000000;">&#40;</span><a href="about:blank"><span style="color: #000080;">$_</span></a><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>

<p>It looks pretty messy, but it&#8217;s actually pretty easy if you understand pipes (the | character).  The above line 24 boils down to:</p>

<ul>
<li>Get the subkey names</li>
<li>Find only the ones that have 0FF1CE in them (yes I know that&#8217;s office with a zero and a one)</li>
<li>For each one that matches, go delete it, and its children</li>
</ul>

<p>See, not so hard.</p>

<p>There are some parts of this that venture <acronym title="Kilobyte">KB</acronym> into other areas of the registry, so the syntax changes in the opening command, for example:</p>

<div class="codecolorer-container powershell 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>67<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$reg</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'ClassesRoot'</span><span style="color: pink;">,</span> <span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div>

<p>This opens HKEY_CLASSES_ROOT.  You can use CurrentUser to access HKEY_CURRENT_USER too.</p>

<p>That&#8217;s about it for remote registry access and fixing this blasted Office install.  As a side, the script saved me about 30 minutes of bashing through about 200+ registry keys for cleaning up.</p>

<p>Below is the full script, with the delete statements commented out.  If you have 2007 installed, give it a run, see what happens (<strong>please</strong> double check that all the deletes are in fact commented out, this script was used against a server to fix an install issue, and did have active delete statements in, and I cannot remember if I got all the deletes).</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><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 />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br /></div></td><td><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #800000;">'MyRemoteServer'</span><br />
<span style="color: #800080;">$reg</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;LocalMachine&quot;</span><span style="color: pink;">,</span> <span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Office 12 Software Keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Office\'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #008000;">#$regKey.DeleteSubKeyTree('12.0')</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing 2003 shadow keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Software\Microsoft\Windows NT\CurrentVersion\Terminal Server\Install\Software\Microsoft\Office'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #008000;">#$regKey.DeleteSubKeyTree('12.0')</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing office download references&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Office\Delivery\SourceEngine\Downloads'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span><span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*0FF1CE*&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_);</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Office Uninstall keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*0FF1CE*&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Office upgrade keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UpgradeCodes'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*F01FEC&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Office product key references&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*F01FEC&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Office services&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SYSTEM\CurrentControlSet\Services'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #008000;">#$regKey.DeleteSubKeyTree('ose')</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800000;">&quot;Removing Uninstall keys&quot;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span><span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800080;">$subName</span> <span style="color: pink;">=</span> <a href="about:blank"><span style="color: #000080;">$_</span></a><br />
&nbsp; <span style="color: #800080;">$subKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$regKey</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800080;">$subName</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #800080;">$value</span> <span style="color: pink;">=</span> <span style="color: #800080;">$subKey</span>.GetValue<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;UninstallString&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #0000FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #800080;">$value</span> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*Office Setup Controller\Setup.*&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;Deleting Uninstall key $subName&quot;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($subName)</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #800080;">$subKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$reg</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$reg</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'ClassesRoot'</span><span style="color: pink;">,</span> <span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Installer\Features'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*F01FEC&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Installer\Products'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*F01FEC&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Installer\UpgradeCodes'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*F01FEC&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Installer\Win32Assemblies'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span>.GetSubKeyNames<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span> <a href="about:blank"><span style="color: #000080;">$_</span></a> <span style="color: #FF0000;">-like</span> <span style="color: #800000;">&quot;*Office12*&quot;</span> <span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">ForEach</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #800000;">&quot;Deleting subkey $_&quot;</span><br />
&nbsp; <span style="color: #008000;">#$regKey.DeleteSubKeyTree($_)</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$reg</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<br />
<span style="color: #800000;">&quot;Deleting UserInfo&quot;</span><br />
<span style="color: #800080;">$reg</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span>Microsoft.Win32.RegistryKey<span style="color: #000000;">&#93;</span>::OpenRemoteBaseKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'CurrentUser'</span><span style="color: pink;">,</span> <span style="color: #800080;">$server</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$regKey</span> <span style="color: pink;">=</span> <span style="color: #800080;">$reg</span>.OpenSubKey<span style="color: #000000;">&#40;</span><span style="color: #800000;">'Software\Microsoft\Office\Common'</span><span style="color: pink;">,</span> <span style="color: #800080;">$true</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #008000;">#$regKey.DeleteSubKeyTree('UserInfo')</span><br />
<span style="color: #800080;">$regKey</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<br />
<span style="color: #800080;">$reg</span>.Close<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></td></tr></tbody></table></div><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2010/03/29/powershell-remote-registry-and-fixing-an-office-2007-install/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PowerShell; Checking a Job Status</title>
		<link>http://jon.netdork.net/2009/12/21/powershell-checking-a-job-status?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=powershell-checking-a-job-status</link>
		<comments>http://jon.netdork.net/2009/12/21/powershell-checking-a-job-status#comments</comments>
		<pubDate>Mon, 21 Dec 2009 19:22:05 +0000</pubDate>
		<dc:creator>Jonathan Angliss</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://jon.netdork.net/?p=559</guid>
		<description><![CDATA[With Windows 2008, the task scheduler is setup differently than earlier. You can use the Com object Schedule.Service to access information about tasks. First we need to get the Com object to talk to the server. This is easily done with a &#8220;new object&#8221;. $st = new-object -com&#40;&#34;Schedule.Service&#34;&#41; $st.connect&#40;&#41; The first line gets you the [...]]]></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%2F2009%2F12%2F21%2Fpowershell-checking-a-job-status">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fjon.netdork.net%2F2009%2F12%2F21%2Fpowershell-checking-a-job-status&amp;source=j_angliss&amp;style=normal&amp;service=bit.ly&amp;hashtags=Microsoft,PowerShell,Work" height="61" width="50" />
			</a>
		</div><p>With Windows 2008, the task scheduler is setup differently than earlier.  You can use the Com object Schedule.Service to access information about tasks.</p>

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

<p>First we need to get the Com object to talk to the server.  This is easily done with a &#8220;new object&#8221;.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$st</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">new-object</span> <span style="color: pink;">-</span>com<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Schedule.Service&quot;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #800080;">$st</span>.connect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div></div>

<p>The first line gets you the com object, the second connects to the server.  You can put in a server address to query remote servers too.</p>

<p>As 2008 uses task folders, you have to get the folder to search inside.  If you don&#8217;t organize, this can be &#8220;\&#8221; or you can use the folder name.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$folder</span> <span style="color: pink;">=</span> <span style="color: #800080;">$st</span>.getFolder<span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;\&quot;</span><span style="color: #000000;">&#41;</span></div></div>

<p>Then get the tasks from that folder.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$tasks</span> <span style="color: pink;">=</span> <span style="color: #800080;">$folder</span>.GetTasks<span style="color: #000000;">&#40;</span><span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span></div></div>

<p>$tasks is now an array of tasks, which you can walk through, or query.  Now we have a task listing, we can figure out the state of each task using a foreach.</p>

<div class="codecolorer-container powershell default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="powershell codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #800080;">$tasks</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">select</span> Name<span style="color: pink;">,</span>State</div></div>

<p>The state column is based on the values <a href="http://msdn.microsoft.com/en-us/library/aa383617%28VS.85%29.aspx" title="MSDN; TASK_STATE Enumeration">here</a>.  So we know that 4 is running for example.</p>

<p>I used this information to build a script that allowed my <a href="http://www.nagios.org" title="Nagios">Nagios</a> server to manage alerts based on our build server running the build process.</p><!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://jon.netdork.net/2009/12/21/powershell-checking-a-job-status/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
