<?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>Huddled Masses &#187; Twitter</title>
	<atom:link href="http://huddledmasses.org/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Twitter in your console prompt: using PS Jobs for background work (Update)</title>
		<link>http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/</link>
		<comments>http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 05:28:53 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Background Jobs]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PoshTweet]]></category>
		<category><![CDATA[Prompt]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1294</guid>
		<description><![CDATA[Some of you may realize I&#8217;m a bit of an odd personality: when I get an idea, I don&#8217;t want to let it go until I at least prove it&#8217;s possible &#8230; but then I have a tendency to abandon things or leave them for others to finish. Having said all that: here&#8217;s my latest [...]]]></description>
			<content:encoded><![CDATA[	<p>Some of you may realize I&#8217;m a bit of an odd personality: when I get an idea, I don&#8217;t want to let it go until I at least prove it&#8217;s possible &#8230; but then I have a tendency to abandon things or leave them for others to finish. Having said all that: here&#8217;s my latest one-off script module.  I&#8217;m <em>probably</em> done playing with it, and it&#8217;s <em>definitely</em> not a finished, release-quality project &#8230; but it works, it does something neat, and maybe you could learn something from it.</p>

	<p>Dave Winer was blogging about wanting a <a href="http://www.scripting.com/stories/2009/11/06/twitterNeedsACommandline.html">web-based command-line app for twitter</a>, and although I&#8217;m not a big fan of web apps, it <strong>did</strong> make me thing about how it would be interesting to have a command-line twitter application&#8230;</p>

	<p>So, I grabbed a TwitterLib.dll from <a href="http://code.google.com/p/wittytwitter/">Witty</a>, which in turn depends on <a href="http://code.google.com/p/tweetsharp/">TweetSharp</a> &#8230; stuffed all of those into a new folder &#8220;Twitter&#8221; inside my Documents\WindowsPowerShell\Modules, and started experimenting in the console:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">resolve-<span style="font-style: normal;">path</span></span> <span style="color: #009900;">&quot;.\TwitterLib.dll&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Constructor</span></span> TwitterLib.<span style="color: #003366;">TwitterNet</span><br />
<span style="color: #666666; font-style: italic;"># I see a constructor that takes a string user name and a SecureString password, so I'll need:</span><br />
<span style="color: #660033; font-weight: bold;">$twitterCred</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><br />
<span style="color: #660033; font-weight: bold;">$twitter</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> TwitterLib.<span style="color: #003366;">TwitterNet</span> <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">UserName</span>.<span style="color: #003366;">TrimStart</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\&quot;</span><span style="color: #333;">&#41;</span>, <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">Password</span><br />
<span style="color: #660033; font-weight: bold;">$twitter</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Member</span></span><br />
<span style="color: #666666; font-style: italic;"># There's quite a few neat things there, let's try:</span><br />
<span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetFriendsTimeline</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>Incidentally, I&#8217;m using <a href="http://poshcode.org/1368">Get-Constructor</a> (it&#8217;s on PoshCode) to enumerate the constructors, and the output of that <code>GetFriendsTimeline</code> method was <strong>really</strong> verbose. Way more information than I wanted, but, it did have the information I needed.</p>

	<p>Now, what I wanted was to have the most recent tweets show up as part of my prompt, but I couldn&#8217;t be waiting around while it fetched it every time, so the next thing I did was write a little <code>Start-Job</code> script to run that stuff in a background thread, where I could just <code>Receive-Job</code> to get the data when I was ready for it.  I also had to implement something to make sure I wasn&#8217;t going to get the same tweets over and over again (multiple calls to GetFriendsTimeline() return the most recent 20 tweets or so, without regard for whether you&#8217;ve seen them or not):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Job</span></span> <span style="color: #000066;">-Name</span> <span style="color: #009900;">&quot;Twitter&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$twitterCred</span>, <span style="color: #660033; font-weight: bold;">$assemblyPath</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$assemblyPath</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$twitter</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> TwitterLib.<span style="color: #003366;">TwitterNet</span> <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">UserName</span>.<span style="color: #003366;">TrimStart</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\&quot;</span><span style="color: #333;">&#41;</span>, <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">Password</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetFriendsTimeline</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">tee</span> <span style="color: #000066;">-var</span> cache <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033;">where</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$cache</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">id</span> <span style="color: #000066;">-gt</span> <span style="color: #660033; font-weight: bold;">$cache</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Id</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033;">ft</span> id, @<span style="color: #333;">&#123;</span>n<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;ScreenName&quot;</span>;e<span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">User</span>.<span style="color: #003366;">ScreenName</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span>, text <span style="color: #000066;">-wrap</span> <span style="color: #000066;">-auto</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">out-<span style="font-style: normal;">string</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033;">sleep</span> <span style="color: #cc66cc;">45</span> <span style="color: #666666; font-style: italic;"># or else you start getting rejected</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Arg</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><span style="color: #333;">&#41;</span>, <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">resolve-<span style="font-style: normal;">path</span></span> <span style="color: #009900;">&quot;.\TwitterLib.dll&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>With that done, I can easily fetch all the tweets since the last time I fetched them by just calling <code>Receive-Job Twitter</code>.  The one thing you&#8217;ll notice in that is that I had to add a Hash in the Format-Table to get the &#8220;ScreenName&#8221; property to equal the users actual ScreenName.  The reason for that is that the TwitterLib.TwitterUser class isn&#8217;t properly serializable &#8212; so when I tried doing the Format-Table stuff on the receiving end, I was getting &#8220;TwitterLib.TwitterUser&#8221; as the value for the User property when I did <code>Receive-Job</code> later. To fix it, I extracted the information I wanted into a string property while the class was still in the &#8220;remote&#8221; runspace &#8212;thus it would serialize easier &#8212; at first I did it just the way it&#8217;s written there, but eventually I realized I should still return the actual object, so I changed it to use <code>Add-Member</code>.</p>

	<p>In the end, the module I&#8217;ll release is somewhat more complete: it includes a few functions for tweeting and replying and following and even unfollowing, and I wrote a Format file to hide the extra data, and added my own <span class="caps">URL</span> un-shortener to resolve long URLs for display.  If I was going to use this full time, I would need to get some support for creating a search that would be permanent too (ie: so I could pull in anything &#8220;public&#8221; about PowerShell &#8212; I think I&#8217;d probably try using the new Bing twitter interface for that.</p>

	<p>If anyone wants to play with it, I&#8217;ve put it up here <a href="http://huddledmasses.org/downloads/PoshTweet.zip">for download [new: 11/9/2009]</a>, and you&#8217;re welcome to use it however you like.  I actually tweaked the source code to the Witty TwittlerLib a bit (I&#8217;ll make that available shortly), adding methods for removing friends, and for getting a user by their username instead of by their ID.  I also happened to notice, while I was mucking around in the source, there&#8217;s already support in TwitterLib and TweetSharp for several cool things:</p>

	<ul>
		<li>Posting an image via a service using a System.IO.FileInfo (this is what PowerShell returns from <code>Get-ChildItem</code> (aka: dir or ls), so it&#8217;s trivial to implement).</li>
		<li>OAuth from the desktop app (this is a little complicated, but since you can cache it, it would let you avoid asking for the password the way I do right now).</li>
	</ul>
	<ul>
		<li>Search using Twitter&#8217;s <span class="caps">API</span> (I suspect Bing&#8217;s search is rather better, even without real-time <span class="caps">AJAX</span> results).</li>
	</ul>

	<h3><span class="caps">UPDATE</span> [11/9/09]</h3>

	<p>Ok, I polished this up a little more while I was using it, and ended up with a pretty nice client &#8212; it uses <a href="http://growlforwindows.com/">Growl for Windows</a> to pop up notices (if it&#8217;s available), and resolves shortened urls, and it now caches better, and has functions for searching/filtering that cache, and opening links from posts, etc. That is &#8230; it&#8217;s basically usable as a client now  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> .  The download includes those two dependent modules, but not <a href="http://growlforwindows.com/">Growl for Windows</a>. </p>

	<p>I also renamed it, after I finally remembered where I had heard <a href="http://twitter.com/powertwitter">PowerTwitter</a> before.  I&#8217;ve registered &#8220;PoshTweet&#8221; with twitter as an app name to make sure it was unique, so maybe I&#8217;ll add OAuth support just so it can show up property and advertise itself.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	
<a href='http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/ipmo/' title='Import-Module'><img width="150" height="80" src="http://joelbennett.net/wordpress/wp-content/uploads/2009/11/ipmo-150x80.png" class="attachment-thumbnail" alt="Import-Module prompts for login info" title="Import-Module" /></a>
<a href='http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/new-tweet/' title='New-Tweet'><img width="150" height="80" src="http://joelbennett.net/wordpress/wp-content/uploads/2009/11/new-tweet-150x80.png" class="attachment-thumbnail" alt="New-Tweet is used to send tweets" title="New-Tweet" /></a>
<a href='http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/get-tweet/' title='Get-Tweet'><img width="150" height="79" src="http://joelbennett.net/wordpress/wp-content/uploads/2009/11/get-tweet-150x79.png" class="attachment-thumbnail" alt="Get-Tweet is used to retrieve cached tweets" title="Get-Tweet" /></a>


	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">&lt;# Starting with TwitterLib.dll from http://code.google.com/p/wittytwitter/<br />
&nbsp; &nbsp;ToDo:<br />
&nbsp; &nbsp;* Add persistent search support (via bing?) and a search command<br />
&nbsp; &nbsp;* Add Block commands<br />
&nbsp; &nbsp;* Consider using new TweetSharp lib when they release one not dependent on extension methods. TwitterLib is *really* rough.<br />
#&gt;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$twitterCred</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Credential</span></span><span style="color: #333;">&#41;</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$interval</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">60</span> <span style="color: #333;">&#41;</span><br />
&nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">StrictMode</span></span> <span style="color: #000066;">-Version</span> Latest<br />
<span style="color: #666666; font-style: italic;"># ((1 / $interval) + (2 / ($interval * 3))) * 3,600 &nbsp;... must be less than 150</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$interval</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">/</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$interval</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">3600</span><span style="color: #333;">&#41;</span> <span style="color: #000066;">-ge</span> <span style="color: #cc66cc;">150</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;Your interval is set too short, you should set it over 40&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;$PsScriptRoot\TwitterLib.dll&quot;</span> <span style="color: #333;">&#41;</span> <br />
<span style="color: #660033; font-weight: bold;">$global</span>:twitter <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> TwitterLib.<span style="color: #003366;">TwitterNet</span> <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">UserName</span>.<span style="color: #003366;">TrimStart</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\&quot;</span><span style="color: #333;">&#41;</span>, <span style="color: #660033; font-weight: bold;">$twitterCred</span>.<span style="color: #003366;">Password</span><br />
<span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">ClientName</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PoshTweet&quot;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Job</span></span> Twitter <span style="color: #000066;">-EA</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Stop-<span style="font-style: normal;">Job</span></span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Job</span></span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Job</span></span> <span style="color: #000066;">-Name</span> <span style="color: #009900;">&quot;Twitter&quot;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Management</span>.<span style="color: #003366;">Automation</span>.<span style="color: #003366;">PSCredential</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Cred</span>,<span style="color: #660033; font-weight: bold;">$ScriptRoot</span>, <span style="color: #660033; font-weight: bold;">$interval</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;$ScriptRoot\TwitterLib.dll&quot;</span> <span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$twitter</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> TwitterLib.<span style="color: #003366;">TwitterNet</span> <span style="color: #660033; font-weight: bold;">$Cred</span>.<span style="color: #003366;">UserName</span>.<span style="color: #003366;">TrimStart</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;\&quot;</span><span style="color: #333;">&#41;</span>, <span style="color: #660033; font-weight: bold;">$Cred</span>.<span style="color: #003366;">Password</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># $twitter.ClientName = &quot;PoshTweet&quot;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## This part depends on HttpRest. If it's not present, it just won't work...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Module</span></span> <span style="color: #000066;">-List</span> HttpRest <span style="color: #000066;">-EA</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> HttpRest<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$isgd</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?is.gd/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$xrl</span> &nbsp; &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?xrl.us/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$snip</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?(?:snurl|snipr|snipurl)\.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$twurl</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?twurl.nl/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$tiny</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?tinyurl.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$shrink</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?shrinkster.com/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$bitly</span> &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?bit.ly/([^?/ ]*)\b&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$trim</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;(?:https?://)?tr.im/([^?/ ]*)\b&quot;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">function</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$string</span>, <span style="color: #660033; font-weight: bold;">$matches</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">scriptblock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$getBlock</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$matches</span>.<span style="color: #003366;">Count</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-ge</span> <span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">--</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$string</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$string</span>.<span style="color: #003366;">Remove</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$matches</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Index</span>, <span style="color: #660033; font-weight: bold;">$matches</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Length</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Insert</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$matches</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Index</span>, <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$matches</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">groups</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">value</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #660033; font-weight: bold;">$getBlock</span> <span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">output</span></span> <span style="color: #660033; font-weight: bold;">$string</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$old</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$url</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$isgd</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp; <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//is.gd/{0}-&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #009900;">&quot;//*[@id='main']/*[local-name() = 'p']/*[local-name() = 'a']/@href&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$xrl</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp;<span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//metamark.net/api/rest/simple&quot;</span> &nbsp;@<span style="color: #333;">&#123;</span>short_url<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$snip</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp; <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//snipurl.com/resolveurl&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#123;</span>id<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$twurl</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//tweetburner.com/links/$_&quot;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #009900;">&quot;//div[4]/p/a/@href&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$tiny</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp; <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//tinyurl.com/preview.php&quot;</span> &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#123;</span>num<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #009900;">&quot;//a[@id='redirecturl']/@href&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$shrink</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//shrinkster.com/Track.aspx&quot;</span> &nbsp; &nbsp; @<span style="color: #333;">&#123;</span>AddressID<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> TEXT <span style="color: #009900;">&quot;//*[@id='tdOriginalURL']&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$trim</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//api.tr.im/api/trim_destination.xml&quot;</span> @<span style="color: #333;">&#123;</span>trimpath<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> Text <span style="color: #009900;">&quot;//trim/destination&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># bitly's is horrid, 'cause it requires an apiKey, and returns invalid xml</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> Replace<span style="color: #66cc66;">-</span>Matches <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #660033; font-weight: bold;">$bitly</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Http</span></span> GET <span style="color: #009900;">&quot;http<span style="color: #000099; font-weight: bold;">`:</span>//api.bit.ly/expand&quot;</span> @<span style="color: #333;">&#123;</span>version <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;2.0.1&quot;</span>; login<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;jaykul&quot;</span>; apiKey<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;R_05c31e25dd38fb6113044336ae23a441&quot;</span>; format<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;xml&quot;</span>; shortUrl<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Http</span></span> Text <span style="color: #66cc66;">|%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-replace</span> <span style="color: #009900;">&quot;.*longUrl\&gt;(.*)\&lt;/longUrl.*&quot;</span>,<span style="color: #009900;">''</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$url</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">filter</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## And this, depends on Growl. If it's not working, we just won't do it...</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Module</span></span> <span style="color: #000066;">-List</span> Growl <span style="color: #000066;">-EA</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> Growl<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">GrowlType</span></span> PoshTweet NewTweet <span style="color: #000066;">-AppIcon</span> <span style="color: #009900;">&quot;$ScriptRoot\PoshTweetSq.png&quot;</span> <span style="color: #000066;">-Icon</span> <span style="color: #009900;">&quot;$ScriptRoot\PoshTweetSq.png&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$urlpat</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;(?:http:|ftp:)//[^ ]+&quot;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">function</span> PostProcess <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>, Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$status</span><br />
,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$LastId</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$status</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Member</span></span> NoteProperty Tweet <span style="color: #000066;">-Value</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">Text</span> <span style="color: #333;">&#41;</span> <span style="color: #000066;">-Input</span> <span style="color: #660033; font-weight: bold;">$status</span> <span style="color: #000066;">-Passthru</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$LastId</span>.<span style="color: #003366;">Value</span> <span style="color: #000066;">-lt</span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">id</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$LastId</span>.<span style="color: #003366;">Value</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">id</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Module</span></span> Growl <span style="color: #000066;">-EA</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">TimeLine</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;DirectMessages&quot;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$urlpat</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">Tweet</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Expand</span> Value<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$url</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://twitter.com/inbox&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$url</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://twitter.com/{0}/status/{1}&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">ScreenName</span>, <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">Id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Growl</span></span> PoshTweet NewTweet <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">FullName</span> <span style="color: #660033; font-weight: bold;">$status</span>.<span style="color: #003366;">Tweet</span> <span style="color: #000066;">-Url</span> <span style="color: #660033; font-weight: bold;">$url</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Output</span></span> <span style="color: #660033; font-weight: bold;">$status</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Initialize the &quot;last&quot; values so we don't get spammed at the begining</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$LastTweet</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">RetrieveTimeline</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;Friends&quot;</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Empty</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-Last</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Expand</span> Id<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$LastReply</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">RetrieveTimeline</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;Replies&quot;</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Empty</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-Last</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Expand</span> Id<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$LastDm</span> &nbsp; &nbsp;<span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetDirectMessages</span><span style="color: #333;">&#40;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-Last</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Expand</span> Id<br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># initially, shows all tweets, then, only new ones</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetFriendsTimeline</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$lastTweet</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> PostProcess <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$LastTweet</span><span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># check for these two only every 3rd time</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">++</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># initially shows nothing, then new dms</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetDirectMessages</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$lastDm</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToTweetCollection</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> PostProcess <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$lastDm</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$twitter</span>.<span style="color: #003366;">GetReplies</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$lastReply</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> PostProcess <span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ref</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$lastReply</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033;">sleep</span> <span style="color: #660033; font-weight: bold;">$interval</span> <span style="color: #666666; font-style: italic;"># Twitter allows 150 updates per hour</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Arg</span> <span style="color: #660033; font-weight: bold;">$twitterCred</span>, <span style="color: #660033; font-weight: bold;">$PSScriptRoot</span>, <span style="color: #660033; font-weight: bold;">$interval</span><br />
<br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> Variable:TwitterOriginalPrompt <span style="color: #000066;">-EA</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Global</span>:TwitterOriginalPrompt <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Function</span>:<span style="color: #660033;">Prompt</span><span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$global</span>:LastTweets <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Collections</span>.<span style="color: #003366;">Generic</span>.<span style="color: #003366;">List</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">PSObject</span><span style="color: #333;">&#93;</span></span><br />
<span style="color: #666699; font-weight: bold;">function</span> Global:<span style="color: #660033;">Prompt</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Array</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$newTweets</span> <span style="color: #66cc66;">=</span> <span style="color: #660033;">rcjb</span> twitter<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$newTweets</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$index</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$global</span>:LastTweets.<span style="color: #003366;">Count</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$newTweets</span><span style="color: #333;">&#91;</span>$<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$newTweets</span>.<span style="color: #003366;">Count</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span>..0<span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$tweet</span>.<span style="color: #003366;">Index</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$index</span><span style="color: #66cc66;">++</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:LastTweets.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$newTweets</span> <span style="color: #66cc66;">|</span> Sort<span style="color: #66cc66;">-</span>Object DateCreated <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-Stream</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">+=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$tweet</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span> <span style="color: #000066;">-ne</span> <span style="color: #009900;">&quot; &quot;</span><span style="color: #333;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">write-<span style="font-style: normal;">host</span></span> <span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #000066;">-Fore</span> $<span style="color: #333;">&#40;</span> <span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#123;</span><span style="color: #009900;">&quot;DarkGray&quot;</span><span style="color: #333;">&#125;</span> default <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Gray&quot;</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$TwitterOriginalPrompt</span> @args <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Tweet</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>DefaultParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;WhereTweet&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Search&quot;</span>,Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$ByUser</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Search&quot;</span>,Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$WithText</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;Search&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Int</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Index</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$false</span>,ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;WhereTweet&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Where</span><span style="color: #66cc66;">=</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#125;</span><br />
, <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$First</span>, <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">Switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Last</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">BEGIN</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PSCmdlet</span>.<span style="color: #003366;">ParameterSetName</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Search&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$clauses</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$ByUser</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#123;</span> &nbsp;<span style="color: #660033; font-weight: bold;">$clauses</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">'$ByUser -contains $_.ScreenName'</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$WithText</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp;<span style="color: #333;">&#123;</span> &nbsp;<span style="color: #660033; font-weight: bold;">$clauses</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">'$_.Tweet -match ($WithText -join &quot;|&quot;)'</span> &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Index</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> &nbsp;<span style="color: #660033; font-weight: bold;">$clauses</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">'$Index -contains $_.Index'</span> &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Where</span> <span style="color: #66cc66;">=</span> <span style="color: #660033;">iex</span> <span style="color: #009900;">&quot;{ $($clauses -join ' -and ' ) }&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">ScriptBlock</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$RealPredicate</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$args</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Where</span> <span style="color: #660033; font-weight: bold;">$Where</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> &nbsp; <br />
<span style="color: #666699; font-weight: bold;">Process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$First</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$LastTweets</span>.<span style="color: #003366;">FindFirst</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$RealPredicate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">elseif</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$Last</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$LastTweets</span>.<span style="color: #003366;">FindLast</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$RealPredicate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$LastTweets</span>.<span style="color: #003366;">FindAll</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$RealPredicate</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Url</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>SupportsShouldProcess<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">ForEach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$link</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Url</span></span> <span style="color: #660033; font-weight: bold;">$msg</span>.<span style="color: #003366;">Tweet</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PSCmdlet</span>.<span style="color: #003366;">ShouldProcess</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$link</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Process</span></span> <span style="color: #660033; font-weight: bold;">$link</span> <span style="color: #000066;">-Confirm</span>:<span style="color: #660033; font-weight: bold;">$False</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #660033; font-weight: bold;">$null</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$urlpat</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">regex</span><span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;(?:http:|ftp:)//[^ ]+&quot;</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Url</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$urlpat</span>.<span style="color: #003366;">Matches</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$msg</span> <span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-Expand</span> Value <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<br />
<br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Reply</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$index</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$OFS</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot; &quot;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$source</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Tweet</span></span> <span style="color: #000066;">-Index</span> <span style="color: #660033; font-weight: bold;">$index</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;$args&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #000066;">-notmatch</span> <span style="color: #660033; font-weight: bold;">$source</span>.<span style="color: #003366;">ScreenName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$tweet</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;@$($source.ScreenName) $tweet&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">AddTweet</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$tweet</span>, <span style="color: #660033; font-weight: bold;">$source</span>.<span style="color: #003366;">Id</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Tweet</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$OFS</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot; &quot;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">AddTweet</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;$args&quot;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Friend</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">FollowUser</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Friend</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">UnfollowUser</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Friend</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$userName</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$userNaem</span>.<span style="color: #003366;">Count</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">GetFriends</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">GetUser</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">FriendTweet</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$userName</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:twitter.<span style="color: #003366;">GetUserTimeline</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$user</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#.Synopsis </span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Calculates a relative text version of a duration</span><br />
<span style="color: #666666; font-style: italic;">#.Description</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;Generates a string approximation of a timespan, like &quot;x minutes&quot; or &quot;x days.&quot; Note this method does not add &quot;about&quot; to the front, nor &quot;ago&quot; to the end unless you pass them in.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Span</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;A TimeSpan to convert to a string</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Before</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;A DateTime representing the start of a timespan.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter After</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;A DateTime representing the end of a timespan.</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Prefix</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The prefix string, pass &quot;about&quot; to render: &quot;about 4 minutes&quot;</span><br />
<span style="color: #666666; font-style: italic;">#.Parameter Postfix</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp;The postfix string, like &quot;ago&quot; to render: &quot;about 4 minutes ago&quot;</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">ConvertTo-<span style="font-style: normal;">RelativeTimeString</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span>DefaultParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;TwoDates&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;TimeSpan&quot;</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$span</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;TwoDates&quot;</span>,Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>,ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Alias<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;DateCreated&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$before</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ParameterSetName<span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;TwoDates&quot;</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>, Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$after</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$prefix</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;&quot;</span><br />
,<br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">2</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">String</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;&quot;</span><br />
&nbsp;<br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">PROCESS</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PSCmdlet</span>.<span style="color: #003366;">ParameterSetName</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;TwoDates&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$span</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$after</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$before</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;$(<br />
&nbsp; &nbsp;switch($span.TotalSeconds) {<br />
&nbsp; &nbsp; &nbsp; {$_ -le 1} &nbsp; &nbsp; &nbsp;{ &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> a second <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; {$_ -le 60} &nbsp; &nbsp; { &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$span</span>.<span style="color: #003366;">Seconds</span><span style="color: #333;">&#41;</span> seconds <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break }<br />
&nbsp; &nbsp; &nbsp; {$_ -le 120} &nbsp; &nbsp;{ &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> a minute <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break }<br />
&nbsp; &nbsp; &nbsp; {$_ -le 2700} &nbsp; { &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$span</span>.<span style="color: #003366;">Minutes</span><span style="color: #333;">&#41;</span> minutes <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } # 45 minutes or less<br />
&nbsp; &nbsp; &nbsp; {$_ -le 5400} &nbsp; { &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> an hour <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } # 45 minutes to 1.5 hours <br />
&nbsp; &nbsp; &nbsp; {$_ -le 86400} &nbsp;{ &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$span</span>.<span style="color: #003366;">Hours</span><span style="color: #333;">&#41;</span> hours <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } # less than a day<br />
&nbsp; &nbsp; &nbsp; {$_ -le 172800} { &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> <span style="color: #cc66cc;">1</span> day <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } # less than two days<br />
&nbsp; &nbsp; &nbsp; default &nbsp; &nbsp; &nbsp; &nbsp; { &quot;</span><span style="color: #660033; font-weight: bold;">$prefix</span> $<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$span</span>.<span style="color: #003366;">Days</span><span style="color: #333;">&#41;</span> days <span style="color: #660033; font-weight: bold;">$postfix</span> <span style="color: #009900;">&quot;; break } <br />
&nbsp; &nbsp;}<br />
&nbsp; &nbsp;)&quot;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> tweet <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Tweet</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> follow <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Friend</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> unfollow <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Friend</span></span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> nt <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Tweet</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> nr <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Reply</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> <span style="color: #333399; font-weight: bold; font-style: italic;">gt</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Tweet</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> gft <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">FriendTweet</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> af <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Friend</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> rf <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Friend</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> sau <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Url</span></span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">TwitterUser</span></span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Friend</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">TwitterFriend</span></span> <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Friend</span></span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">TwitterFriend</span></span> <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Friend</span></span><br />
<br />
<br />
<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">ModuleMember</span></span> <span style="color: #000066;">-Function</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Reply</span></span>, <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Tweet</span></span>, <span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Friend</span></span>, <span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">Friend</span></span>, <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Friend</span></span>, <span style="color: #0066cc; font-style: italic;">Resolve-<span style="font-style: normal;">URL</span></span>, <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">Tweet</span></span>, <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">FriendTweet</span></span>, <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Url</span></span>, <span style="color: #0066cc; font-style: italic;">ConvertTo-<span style="font-style: normal;">RelativeTimeString</span></span> <span style="color: #000066;">-Alias</span> <span style="color: #66cc66;">*</span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/twitter-in-your-console-prompt-using-ps-jobs-for-background-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert Twitter users into FriendFeed Imaginary Friends!</title>
		<link>http://huddledmasses.org/convert-twitter-users-into-friendfeed-imaginary-friends/</link>
		<comments>http://huddledmasses.org/convert-twitter-users-into-friendfeed-imaginary-friends/#comments</comments>
		<pubDate>Sat, 16 May 2009 20:15:47 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Application programming interface]]></category>
		<category><![CDATA[Automation]]></category>
		<category><![CDATA[FriendFeed]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Twitter]]></category>
		<category><![CDATA[WatiN]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://joelbennett.net/?p=1170</guid>
		<description><![CDATA[So a lot of people seem to be taking the latest missteps by Twitter&#8217;s management (and the accompanying admission of bad design) as an opportunity to try out some alternatives. Many of them seem to be coming over to FriendFeed (which has been better than Twitter for a long time, but nevermind that) ... so [...]]]></description>
			<content:encoded><![CDATA[	<p>So a lot of people seem to be taking the <a href="http://blog.twitter.com/2009/05/whoa-feedback.html">latest missteps</a> by Twitter&#8217;s management (and <a href="http://blog.twitter.com/2009/05/replies-kerfuffle.html">the accompanying admission of bad design</a>) as an opportunity to try out some alternatives. Many of them seem to be coming over to FriendFeed (which has been better than Twitter for a long time, but nevermind that) ... so I thought I&#8217;d update and release a PowerShell 2.0 script I wrote to create <a href="http://friendfeed.com/settings/imaginary">imaginary friends</a> out of your friends that stay on Twitter.</p>

	<p>The first part of it is a WatiN script (that automates your browser) called <code>New-ImaginaryFriend</code> which takes three parameters: a name for the imaginary friend, a url for an avatar for the friend, and a HashTable&#8230;  Of course, we sort-of cheat by using the HashTable &#8230; it&#8217;s basically a bunch of key-value pairs of remote services and user names. You can use it to add twitter ID&#8217;s like <code>twitter=&#34;jsnover&#34;</code> or blogs like <code>blog=&#34;http://HuddledMasses.org/&#34;</code> etc.  You can even add multiple sources (eg: twitter + diigo, two blogs, etc) to a single new imaginary friend  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='8)' class='wp-smiley' /> ...</p>

	<p>This script is done using WatiN because the FriendFeed <span class="caps">API</span> doesn&#8217;t support creating imaginary friends yet, and as a result it&#8217;s slow, and requires IE (and doesn&#8217;t seem to work very well with IE8 &#8212; at least, I couldn&#8217;t get it to set the avatars using IE 8 on Windows 7, so I commented out the avatar part of the next-to-last line).</p>

	<p>The other part of the script is a pair of functions: the first is <code>Get-FriendFeedFriends</code> which retrieves profile information for all your friends in a slick format that includes all their services and such &#8230; you may find other uses for this later  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> , the second is <code>Get-TwitterFriends</code> &#8230; Both have an <code>-Exclude</code> parameter so you can pass it a list of people to ignore.</p>

	<p>When you put these three functions together, you can just import the FriendFeed module, and start creating friends (don&#8217;t forget <em>this</em> version of the scripts only works with IE6 or IE7 for the purpose of avatars, as WatiN can&#8217;t seem to set the file upload value in IE8 yet).</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> FriendFeed<br />
<span style="color: #666666; font-style: italic;">## Get any twitter friends who aren't on friendfeed</span><br />
<span style="color: #666666; font-style: italic;">## Make sure you use FriendFeed's built in &quot;add all your twitter friends&quot; first</span><br />
<span style="color: #660033; font-weight: bold;">$twits</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">TwitterFriends</span></span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Nickname</span> jaykul<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-Exclude</span> $<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">FriendFeedFriends</span></span> jaykul <span style="color: #66cc66;">|</span> <span style="color: #660033;">select</span> <span style="color: #000066;">-expand</span> twitter<span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Add them to friend feed</span><br />
<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$twit</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$twits</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">ImaginaryFriend</span></span> <span style="color: #660033; font-weight: bold;">$twit</span>.<span style="color: #003366;">name</span> @<span style="color: #333;">&#123;</span>twitter<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$twit</span>.<span style="color: #003366;">screen_name</span><span style="color: #333;">&#125;</span> <span style="color: #660033; font-weight: bold;">$twit</span>.<span style="color: #003366;">profile_image_url</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>You can <a href="/downloads/FriendFeedModule+Dependencies.zip">download all of the required modules at once</a> (<a href="/downloads/FriendFeedModule+Dependencies.7z">7z</a>), or grab the latest versions of them from PoshCode: <a href="http://poshcode.org/1110">FriendFeed</a>, <a href="http://poshcode.org/1107">HttpRest</a>, and  <a href="http://poshcode.org/1108">WatiN</a> &#8230; but if you do that, you&#8217;ll still need to get the binaries separately  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':-/' class='wp-smiley' /> </p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/2f4a4fcd-2d3b-4c5e-b812-d1c5f2b5624b/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=2f4a4fcd-2d3b-4c5e-b812-d1c5f2b5624b" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/convert-twitter-users-into-friendfeed-imaginary-friends/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PoshCode now Twitters</title>
		<link>http://huddledmasses.org/poshcode-now-twitters/</link>
		<comments>http://huddledmasses.org/poshcode-now-twitters/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 04:29:09 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Status]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1122</guid>
		<description><![CDATA[Well, over the weekend I stole a few moments from thinking about PoshCode 2.0 to think about PoshCode 1.0 &#8230; and I added two things: Feedback I&#8217;ve replaced the misused &#8220;comment&#8221; box on the website with a full GetSatisfaction.com widget, so you can get your voice heard and get feedback (in the past it&#8217;s been [...]]]></description>
			<content:encoded><![CDATA[	<p>Well, over the weekend I stole a few moments from thinking about <a href="http://PoshCode.org">PoshCode</a> 2.0 to think about PoshCode 1.0 &#8230; and I added two things:</p>

	<h2>Feedback</h2>

	<p>I&#8217;ve replaced the misused &#8220;comment&#8221; box on the website with a full GetSatisfaction.com widget, so you can get your voice heard and get feedback (in the past it&#8217;s been impossible for me to respond to comments).</p>

	<h2>Twitter</h2>

	<p>PoshCode now posts new contributions to twitter on it&#8217;s own <a href="http://Twitter.com/PoshCode">PoshCode account</a>.  Feel free to follow along.  Of course, the information on there is severely truncated, by comparison with the <a href="http://www.poshcode.org/feed">PoshCode <span class="caps">RSS</span> feed</a> &#8230; and only includes the link to the website, whereas the <span class="caps">RSS</span> feed <em>also</em> includes direct download links &#8230; but I figured some of you would appreciate it anyway, and I aim to please.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':D' class='wp-smiley' />   </p>

	<p>Of course, now in PoshCode 2 I&#8217;ll have to make sure we have users enter their twitter id&#8217;s in the profile page so we can be sure to cite you properly. Oh what a tangled web we weave&#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/poshcode-now-twitters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerBot 2.0 &#8211; An IRC bot in under 40 lines of PowerShell</title>
		<link>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/</link>
		<comments>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 20:10:34 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Bot]]></category>
		<category><![CDATA[Chat]]></category>
		<category><![CDATA[ChatterBot]]></category>
		<category><![CDATA[IRC]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=865</guid>
		<description><![CDATA[There are a few programming projects that just have to be re-written in each and every programming language. You might think it&#8217;s a waste of time, but because they&#8217;re used primarily by programmers and geeks, and because using them usually involves heavily modifying them, everyone wants one that was written in the language (s)he&#8217;s most [...]]]></description>
			<content:encoded><![CDATA[	<p>There are a few programming projects that just <em>have</em> to be re-written in each and every programming language.  You might think it&#8217;s a waste of time, but because they&#8217;re used primarily by programmers and geeks, and because using them usually involves heavily modifying them, everyone wants one that was written in the language (s)he&#8217;s most familiar with, and writing them can be a good learning experience.  One such project is the <span class="caps">IRC</span> bot.</p>

	<p>I wrote a bot in PowerShell awhile back that we called PowerBot &#8230; the script using <span class="caps">XMPP</span> (Jabber), and could connect to <span class="caps">IRC</span> through a gateway.  Although it could do the thing a bot should be able to do (join channels, respond to messages, etc), and had bonus features like announcing new posts from PoshCode.org, and following channel members on Twitter &#8230; it was technically a Jabber bot, not an <span class="caps">IRC</span> bot (and thus, a little slow to respond on <span class="caps">IRC</span>).</p>

	<p>In any case, I&#8217;ve created a <strong>very simple</strong> framework for people to write PowerShell <span class="caps">IRC</span> decided to rectify that now by publishing a new bot based on the <a href="http://smartirc4net.meebey.net/">SmartIrc4Net</a> library. Of course, it&#8217;s not a &#8220;pure&#8221; PowerShell bot (since I&#8217;m using an <span class="caps">IRC</span> library), but this is .Net after all, and we don&#8217;t really believe in rewriting things that already work just fine. SmartIrc4net is a very nice bit of work, and it&#8217;s a single dll you just have to plop in a folder with the script, and it&#8217;s <span class="caps">LGPL</span> (2.1) so you can reuse it at will with very few restrictions.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<h2>The Script, Explained</h2>

	<p>The full script is <a href="http://poshcode.org/728">available on PoshCode</a> but I wanted to go through and explain a few pieces of it, so I&#8217;ll break it up in pieces here.  Incidentally, if you run this command on that version of the script, you&#8217;ll see there are exactly 39 lines of code, including loading the irc library, and the initial Hello World command.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">gc</span> PowerBot.<span style="color: #003366;">ps1</span> <span style="color: #66cc66;">|</span>?<span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-notmatch</span> <span style="color: #009900;">&quot;^\s*#&quot;</span> <span style="color: #000066;">-and</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">length</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">measure-<span style="font-style: normal;">object</span></span> <span style="color: #000066;">-line</span> <span style="color: #000066;">-word</span></div>

	<p>The first section of the script loads the assembly and contains the Start-PowerBot script. I&#8217;ve wrapped things onto multiple lines here to fit in my blog a bit better and for readability:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Add-Type -path $ProfileDir\Libraries\Meebey.SmartIrc4net.dll</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$ProfileDir\Libraries\Meebey.SmartIrc4net.dll&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$server</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;irc.freenode.net&quot;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$channels</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;#PowerShell&quot;</span><span style="color: #333;">&#41;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$nick</span> &nbsp; &nbsp; <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">'You must provide a nickname'</span><span style="color: #333;">&#41;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$password</span><br />
, <span style="color: #660033; font-weight: bold;">$realname</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PowerShell Bot&quot;</span><br />
, <span style="color: #660033; font-weight: bold;">$port</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">6667</span><br />
<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$global</span>:irc<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:irc <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Meebey.<span style="color: #003366;">SmartIrc4net</span>.<span style="color: #003366;">IrcClient</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">ActiveChannelSyncing</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #666666; font-style: italic;"># $irc will track channels for us</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># $irc.Encoding = [Text.Encoding]::UTF8</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnError</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Error</span></span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">ErrorMessage</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnQueryMessage</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span>PrivateMessage<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnChannelMessage</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span>ChannelMessage<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Connect</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$server</span>, <span style="color: #660033; font-weight: bold;">$port</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Login</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$nick</span>, <span style="color: #660033; font-weight: bold;">$realname</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #660033; font-weight: bold;">$nick</span>, <span style="color: #660033; font-weight: bold;">$password</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## $channels | % { $irc.RfcJoin( $_ ) }</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$channel</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$channels</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">RfcJoin</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$channel</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Resume-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #666666; font-style: italic;"># Shortcut so starting this thing up only takes one command</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>The most important thing to notice here is that the SmartIrc4Net library is event driven, so you just have to <strong>Add</strong> scriptblock event handlers to things, and it will call them when something happens. You can see I&#8217;ve handled the error condition directly in the scriptblock, but for the other handlers I&#8217;ve called out functions which I will specify later. </p>

	<p>In this simple bot I&#8217;ve only handled errors, private messages (a QueryMessage is a message sent directly to the bot, instead of to the channel), and channel messages. The &#8220;channel&#8221; is where you all hang out and chat &#8212; you young&#8217;uns may know this as a &#8220;chat room&#8221; ... but on <span class="caps">IRC</span> we still like to pretend we&#8217;re on <span class="caps">HAM</span> radio, so we have channels and go by weird aliases like &#8220;Gaurhoth&#8221; and &#8220;Gnopeg&#8221; and &#8220;SmellyHippy&#8221; and consider it impolite to ask what people&#8217;s real names are (we&#8217;ll probably kick-ban you if you ask &#8220;A/S/L?&#8221;).</p>

	<p>You do <span class="caps">NOT</span> need a password for the <code>$irc.login</code> call, but if you have registered your bot&#8217;s nick (which I highly recommend if your <span class="caps">IRC</span> network supports it), then you should pass it&#8217;s password to the function.  Finally, the Resume-Powerbot call at the end is a call to our simplest function.  This is essentially the &#8220;message loop&#8221; of our program:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Note that PowerBot stops listening if you press a key ...</span><br />
<span style="color: #666666; font-style: italic;">## You'll have to re-run Resume-Powerbot to get him to listen again</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resume-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">KeyAvailable</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">ListenOnce</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Stop-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;If people listened to themselves more often, they would talk less.&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">RfcQuit</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Disconnect</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p><code>Resume-PowerBot</code> is the simplest possible loop: it will exit if you press any key while the PowerShell window has focus.  You might want to beef up the loop, and add the ability to type <strong>while</strong> the script is running (see <a href="http://poshcode.org/730">my demo script</a>).  Putting that together with this would let you implement a simple interactive PowerShell <span class="caps">IRC</span> client &#8230; which is scriptable to the Nth degree.</p>

	<p>When I call <code>ListenOnce($false)</code> I pass the optional boolean parameter <code>$false</code> to specify that I don&#8217;t want ListenOnce to block &#8212; otherwise, <code>ListenOnce()</code> will block until it actually receives a message: it&#8217;s basically like the difference between writing <code>if($Host.UI.RawUI.KeyAvailable){$Host.UI.RawUI.ReadKey()}</code> and just writing <code>$Host.UI.RawUI.ReadKey()</code> &#8230;</p>

	<p><code>Stop-PowerBot</code> actually sends a quite message and then disconnects from <span class="caps">IRC</span>, and if you call it, you&#8217;ll need to call Start-PowerBot before you can actually use the $irc variable again&#8230;</p>

	<h3>Event handlers</h3>

	<p><strong>Event handlers in PowerShell are really very frustrating.</strong>  Instead of just passing the arguments to the event handler, thus allowing your event handler to match &#8220;any&#8221; event signature, PowerShell takes exactly two arguments to it&#8217;s event handlers: the first is the <code>$this</code> parameter which represents what is triggering the event, and the second is the <code>$_</code> parameter which represents the EventArgs.  In any case, although that is sometimes limiting when developer use non-standard events, it does represent the usual CLR-compliant event signature, and luckily, most of the events in SmartIrc4net match it.</p>

	<p>The two handlers I&#8217;ve written in this demo handle private (Query) messages and public channel messages.  They are virtually identical, except that they send the return message differently  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> PrivateMessage <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">Data</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Verbose $($Data | Out-String)</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$command</span>, <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">MessageArray</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#93;</span> <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">510</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #666699; font-weight: bold;">From</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$nick</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">SendMessage</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Message&quot;</span>, <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #666699; font-weight: bold;">From</span>, <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> ChannelMessage <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">Data</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Verbose $($Data | Out-String)</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$command</span>, <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">MessageArray</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#93;</span> <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">510</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">Channel</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$nick</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">SendMessage</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Message&quot;</span>, <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">Channel</span>, <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Finally, the actual commands for this bot are stored as a hash.  You &#8220;register&#8221; a command by adding a key to the hash, where the key is a single word that matches the hash (it has to be the first word in the message) and the value is a scriptblock that will be executed.  Generally, you just need to return a string (or an object) that you want to reply with, but you can, obviously, take any action you want.</p>

	<p>Unlike the event handlers, the powerbot commands receive parameters: the first is a convenience parameter: it is the rest of the message that came after the keyword.  The second parameter is the full &#8220;Data&#8221; member from SmartIrc4Net, and includes all the text that was in the message.  Although the first parameter is a little redundant, most commands only need to handle the first parameter, so I&#8217;m keeping it there.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## A simple Hello World -like command to get you started:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;Hello&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span>,<span style="color: #660033; font-weight: bold;">$Data</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;Hello, $($Data.Nick)!&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## A scary feature that lets you use the bot as a mannequin:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;!Echo&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span>,<span style="color: #660033; font-weight: bold;">$Data</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;$Query&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Executing PowerShell cmdlets in response to a spoken command:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;!Get-Help&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$help</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">help</span></span> <span style="color: #660033; font-weight: bold;">$Query</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> Name,Synopsis,Syntax<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">array</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;You're going to need to be more specific, I know all about $((($help | % { $_.Name })[0..($help.Length-2)] -join ', ') + ' and even ' + $help[-1].Name)&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span>.<span style="color: #003366;">Synopsis</span>,<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span>.<span style="color: #003366;">Syntax</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #cc66cc;">1000</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #009900;">&quot;RemoveEmptyEntries&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span>0..3<span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;I couldn't find the help file for '$Query', sorry. &nbsp;I probably don't have that snapin loaded.&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/3550472e-9b2b-4b73-8fbf-50db2862da86/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=3550472e-9b2b-4b73-8fbf-50db2862da86" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

