<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>James O&#039;Neill&#039;s Blog</title>
	<atom:link href="http://jamesone111.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamesone111.wordpress.com</link>
	<description>PowerShell , Photography, Virtualization and anything else I find worth writing about</description>
	<lastBuildDate>Fri, 08 Feb 2013 22:37:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jamesone111.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>James O&#039;Neill&#039;s Blog</title>
		<link>http://jamesone111.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jamesone111.wordpress.com/osd.xml" title="James O&#039;Neill&#039;s Blog" />
	<atom:link rel='hub' href='http://jamesone111.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Getting SkyDrive to sync the way I want (like Mesh)</title>
		<link>http://jamesone111.wordpress.com/2013/02/08/getting-skydrive-to-sync-the-way-i-want-like-mesh/</link>
		<comments>http://jamesone111.wordpress.com/2013/02/08/getting-skydrive-to-sync-the-way-i-want-like-mesh/#comments</comments>
		<pubDate>Fri, 08 Feb 2013 22:37:31 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1860</guid>
		<description><![CDATA[A few days ago Geekwire ran a story entitled “Microsoft, Let’s be friends” It began Dear Microsoft, Can we just be friends again? Please? It’s been exactly five years now since I left you. During our time together, I poured all the emotion and energy I had into the products I helped build for you. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1860&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A few days ago <a href="http://www.geekwire.com/2013/microsoft-friends/">Geekwire ran a story entitled “Microsoft, Let’s be friends”</a> It began </p>
<blockquote><p><em>Dear Microsoft,       <br />Can we just be friends again? Please?        <br />It’s been exactly five years now since I left you. During our time together, I poured all the emotion and energy I had into the products I helped build for you. </em></p>
</blockquote>
<p>Whilst it is only slightly more than two years since I left Microsoft, that grabbed my interest.&#160; The author goes though some of the products in which he invested emotional capital but saw Microsoft kill off. </p>
<ul>
<li><em>For 14 years, I used Microsoft Money fanatically &#8230; And then you </em><a href="http://support.microsoft.com/kb/2118008"><em>killed it</em></a></li>
<li><em>[I] fell in love with FrontPage, … But you threw it under the bus for no apparent reason. </em></li>
<li><em>I bought my wife a Zune for her birthday … you gave up and now Zune is in the graveyard, may it rest in peace. Meanwhile, I feel like a sucker … again.</em></li>
<li><em>Microsoft Digital Image Suite was the best image editing package ever to have existed for consumers. Yes, better than Photoshop Elements. … I want you to know that you truly broke my heart when you buried this product.</em></li>
</ul>
<p><a href="http://jamesone111.files.wordpress.com/2013/02/image.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;float:right;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" align="right" src="http://jamesone111.files.wordpress.com/2013/02/image_thumb.png?w=244&#038;h=193" width="244" height="193" /></a>I never used MS-Money. FrontPage passed me by. Being in a small area which Redmond calls “rest of the world”,&#160; I never got to buy a Zune. But I felt the heartbreak at the loss of Digital Image suite. It was one of the few non Xbox things I ever bought on staff purchase and I still use it. But my present pain is for <strong>Windows Live Mesh</strong>. Since I left Microsoft this product has quietly kept half a dozen key folders backed up to the cloud and replicated to all the computers I use.     <br />Microsoft have mailed me twice about their producticidal plans for Mesh. Worryingly, they say 40% of Mesh users are using SkyDrive meaning 60% are <em>not.</em> I never used Mesh’s remote access. But it can sync folders from all over my hard disk and SkyDrive needs folders to share a common root in order to sync them.&#160; I could move some of the folders I sync out of “My Documents” into “SkyDrive” but PowerShell (for example) insists on having its profile folder in a specific location. <strong> In short “out of the box” SkyDrive can’t do what Mesh can. </strong></p>
<p>With the the death of Mesh now only a week away, I decided to try something I’ve been meaning to do for ages. <strong>Create a </strong><a href="http://en.wikipedia.org/wiki/NTFS_symbolic_link"><strong>symbolic link</strong></a><strong> from the Skydrive folder to the “proper” folders</strong> where my files reside. </p>
<p><a href="http://jamesone111.files.wordpress.com/2013/02/image1.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2013/02/image_thumb1.png?w=914&#038;h=264" width="914" height="264" /></a></p>
<p>It&#8217;s pretty easy. Creating Links requires an Administrative command prompt (unless you change the system policy on the machine), and you need a <strong>/d </strong>to tell <strong>MkLink</strong> it is a directory not a file – then it is a question of the name of the link and the place it links to and – cue drum roll – you have a link. </p>
<p><a href="http://jamesone111.files.wordpress.com/2013/02/image2.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2013/02/image_thumb2.png?w=419&#038;h=72" width="419" height="72" /></a>The folder appears under SkyDrive, and right away the SkyDrive client starts syncing in the background. Maybe it is designed not to hog bandwidth or maybe it’s plain slow but on my computer it took a fair while to copy everything. </p>
<p><a href="http://jamesone111.files.wordpress.com/2013/02/image3.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2013/02/image_thumb3.png?w=515&#038;h=424" width="515" height="424" /></a></p>
<p>Repeat for as necessary for the other folders which need to be sync’d</p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/windows-7/'>Windows 7</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1860&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2013/02/08/getting-skydrive-to-sync-the-way-i-want-like-mesh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2013/02/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2013/02/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2013/02/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2013/02/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting to the data in Adobe Lightroom&#8211;with or without PowerShell</title>
		<link>http://jamesone111.wordpress.com/2012/08/09/getting-to-the-data-in-adobe-lightroomwith-or-without-powershell/</link>
		<comments>http://jamesone111.wordpress.com/2012/08/09/getting-to-the-data-in-adobe-lightroomwith-or-without-powershell/#comments</comments>
		<pubDate>Thu, 09 Aug 2012 07:01:24 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Databases / SQL]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1846</guid>
		<description><![CDATA[Some Adobe software infuriates me (Flash), I don’t like their PDF reader and use Foxit instead, apps which use Adobe-Air always seem to leak memory. But I love Lightroom .&#160; It does things right – like installations – which other Adobe products get wrong. It maintains a “library” of pictures and creates virtual folders of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1846&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Some Adobe software infuriates me (Flash), I don’t like their PDF reader and use <a href="http://www.foxitsoftware.com/">Foxit</a> instead, apps which use Adobe-Air always seem to leak memory. But<strong> I love Lightroom </strong>.&#160; It does things right – like installations – which other Adobe products get wrong. It maintains a “library” of pictures and creates virtual folders of images ( “collections” ) but it maintains metadata in the images files so data stays with pictures when they are copied somewhere else – something some other programs still get badly wrong. My workflow with Lightroom goes something like this. </p>
<ol>
<li>If I expect to <strong>manipulate the image</strong> at all I set the cameras to<strong> save in RAW</strong>, DNG format not JPG (with my scuba diving camera I use <a href="http://chdk.wikia.com/wiki/Downloads">CHDK</a> to get the ability to save in DNG) </li>
<li>Shoot pictures – delete any where the camera was pointing at the floor, lens cap was on, studio flash didn’t fire etc. But otherwise <strong>don’t edit in the camera</strong>. </li>
<li><strong>Copy everything to the computer</strong> – usually I create a folder for a set of pictures and put DNG files into a “RAW” subfolder. I keep full memory cards in filing sleeves meant for 35mm slides.. </li>
<li>Using PowerShell I <strong>replace the IMG prefix</strong> with something which tells me what the pictures are but keeps the camera assigned image number.&#160; </li>
<li><strong>Import Pictures into Lightroom</strong> – manipulate them and export to the parent folder of the “RAW” one. Make any prints from inside Lightroom. <strong>Delete “dud” images from the Lightroom catalog. </strong></li>
<li><strong>Move dud images</strong> out of the RAW folder to their own folder.<strong> Backup everything</strong>. Twice. [I’ve only recently learnt to export the Lightroom catalog information to keep the manipulations with the files] </li>
<li>Remove RAW images from my hard disk </li>
</ol>
<p>There is one major pain. How do I know which files I have deleted in Lightroom ? I don’t want to delete them from the hard-disk I want to<u> move</u> them later. It turns out Lightroom uses a SQL Lite database and there is a free <a href="http://www.ch-werner.de/sqliteodbc/">Windows ODBC driver for SQL Lite available for download</a>.&#160; With this in place one can create a ODBC data source – point it at a Lightroom catalog and poke about with data. Want a complete listing of your Lightroom data in Excel? ODBC is the answer. But let me issue these warnings:</p>
<ul>
<li>Lightroom locks the database files exclusively – you can’t use the ODBC driver and Lightroom at the same time. If something else is holding the files open, Lightroom won’t start. </li>
<li>The ODBC driver can run UPDATE queries to change the data: do I need to say that is dangerous ? Good. </li>
<li>There’s no support for this. If it goes wrong, expect Adobe support to say “You did <strong>WHAT</strong> ?” and start asking about your backups. Don’t come to me either. You can work from a copy of the data if you don’t want to risk having to fall back to one of the backups Lightroom makes automatically </li>
</ul>
<p>&#160;&#160; I was interested in 4 sets of data shown in the following diagrams. Below is<strong> image information with the Associated metadata</strong>, and file information. Lightroom stores images (Adobe_Images table) IPTC and EXIF metadata link to images – their “image” field joins to the “id_local” primary key in images. Images have a “root file” (in the AgLibraryFile table) which links to a library folder (AgLibraryFolder) which is expressed as a path from a root folder (AgLibraryRootFolder table). The link always goes to the “id_local” field I could get<strong> information about the folders</strong> imported into the catalog just by querying these last two tables (Outlined in red) </p>
<p><a href="http://jamesone111.files.wordpress.com/2012/08/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/08/image_thumb.png?w=954&#038;h=564" width="954" height="564" /></a></p>
<p>The SQL to fetch this data looks like this for just the folders    <br /><code><span style="color:#8b0000;">SELECT RootFolder.absolutePath || Folder.pathFromRoot as FullName        <br />FROM&#160;&#160; AgLibraryFolder&#160;&#160;&#160;&#160; Folder         <br />JOIN&#160;&#160; AgLibraryRootFolder RootFolder O</span></code><code><span style="color:#8b0000;">N&#160; RootFolder.id_local = Folder.rootFolder        <br />ORDER BY FullName&#160; </span></code></p>
<p>SQLlite is one of the dialects of SQL which doesn’t accept AS in the FROM part of a SELECT statement . Since I run this in PowerShell I also put a where clause in which inserts a parameter. To get all the metadata the query looks like this    <br /><code><span style="color:#8b0000;">SELECT&#160;&#160;&#160; rootFolder.absolutePath || folder.pathFromRoot || rootfile.baseName || '.' || rootfile.extension AS fullName,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LensRef.value AS Lens,&#160;&#160;&#160;&#160; image.id_global,&#160;&#160;&#160;&#160;&#160;&#160; colorLabels,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Camera.Value&#160;&#160;&#160;&#160;&#160;&#160; AS cameraModel,         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fileFormat,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fileHeight,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fileWidth,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; orientation ,         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></code><code><span style="color:#8b0000;">captureTime,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dateDay,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dateMonth,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; dateYear,        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; hasGPS ,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; gpsLatitude,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; gpsLongitude,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; flashFired,         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span></code><code><span style="color:#8b0000;">focalLength,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; isoSpeedRating ,&#160;&#160;&#160;&#160;&#160;&#160; caption,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; copyright        <br />FROM&#160;&#160;&#160;&#160;&#160; AgLibraryIPTC&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; IPTC         <br />JOIN&#160;&#160;&#160;&#160;&#160; Adobe_images&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; image&#160;&#160;&#160;&#160;&#160; ON&#160;&#160;&#160;&#160;&#160; image.id_local = IPTC.image         <br />JOIN&#160;&#160;&#160;&#160;&#160; AgLibraryFile&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; rootFile&#160;&#160; ON&#160;&#160; rootfile.id_local = image.rootFile         <br />JOIN&#160;&#160;&#160;&#160;&#160; AgLibraryFolder&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; folder&#160;&#160;&#160;&#160; ON&#160;&#160;&#160;&#160; folder.id_local = rootfile.folder         <br /></span></code><code><span style="color:#8b0000;">JOIN&#160;&#160;&#160;&#160;&#160; AgLibraryRootFolder&#160;&#160;&#160;&#160;&#160;&#160;&#160; rootFolder ON rootFolder.id_local = folder.rootFolder        <br />JOIN&#160;&#160;&#160;&#160;&#160; AgharvestedExifMetadata&#160;&#160;&#160; metadata&#160;&#160; ON&#160;&#160;&#160;&#160;&#160; image.id_local = metadata.image         <br />LEFT JOIN AgInternedExifLens&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; LensRef&#160;&#160;&#160; ON&#160;&#160;&#160; LensRef.id_Local = metadata.lensRef         <br />LEFT JOIN AgInternedExifCameraModel&#160; Camera&#160;&#160;&#160;&#160; ON&#160;&#160;&#160;&#160; Camera.id_local = metadata.cameraModelRef         <br />ORDER BY FullName </span></code></p>
<p>Note that since some images don’t have a camera or lens logged the joins to those tables needs to be a LEFT join not an inner join. Again the version I use in PowerShell has a Where clause which inserts a parameter. </p>
<p>OK so much for file data – the other data I wanted was about collections. The list of collections is in just one table (AgLibraryCollection) so very easy to query, and but I also wanted to know the images in each collection. </p>
<p>&#160;<a href="http://jamesone111.files.wordpress.com/2012/08/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/08/image_thumb1.png?w=954&#038;h=564" width="954" height="564" /></a></p>
<p>Since one image can be in many collections,and each collection holds many images AgLibraryCollectionImage is a table to provide a many to relationship. Different tables might be attached to AdobeImages depending on what information one wants from about the images in a collection, I’m interested only in mapping files on disk to collections in Lightroom, so I have linked to the file information and I have a query like this. </p>
<p><code><span style="color:#8b0000;">SELECT&#160;&#160; Collection.name AS CollectionName ,        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RootFolder.absolutePath || Folder.pathFromRoot || RootFile.baseName || '.' || RootFile.extension AS FullName         <br />FROM&#160;&#160;&#160;&#160; AgLibraryCollection Collection         <br />JOIN&#160;&#160;&#160;&#160; AgLibraryCollectionimage cimage&#160;&#160;&#160;&#160; ON collection.id_local = cimage.Collection         <br />J</span></code><code><span style="color:#8b0000;">OIN&#160;&#160;&#160;&#160; Adobe_images&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Image&#160;&#160;&#160;&#160;&#160; ON&#160;&#160;&#160;&#160;&#160; Image.id_local = cimage.image        <br /></span></code><code><span style="color:#8b0000;">JOIN&#160;&#160;&#160;&#160; AgLibraryFile&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; RootFile&#160;&#160; ON&#160;&#160; Rootfile.id_local = image.rootFile        <br />JOIN&#160;&#160;&#160;&#160; AgLibraryFolder&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Folder&#160;&#160;&#160;&#160; ON&#160;&#160;&#160;&#160; folder.id_local = RootFile.folder         <br />JOIN&#160;&#160;&#160;&#160; AgLibraryRootFolder&#160;&#160;&#160;&#160;&#160; RootFolder ON RootFolder.id_local = Folder.rootFolder         <br />ORDER BY CollectionName, FullName </span></code></p>
<p>Once I have an ODBC driver (or an OLE DB driver) I have a ready-made PowerShell template for getting data from the data source. So I wrote functions to let me do :    <br /><code>Get-LightRoomItem -ListFolders -include $pwd </code>    <br />To List folders, below the current one, which are in the LightRoom Library     <br /><code>Get-LightRoomItem&#160; -include &quot;dive&quot; </code>    <br />To list files in LightRoom Library where the path contains&#160; &quot;dive&quot; in the folder or filename     <br /><code>Get-LightRoomItem | Group-Object -no -Property &quot;Lens&quot; | sort count | ft -a count,name </code>    <br />To produce a summary of lightroom items by lens used. And     <br /><code>$paths = (Get-LightRoomItem -include &quot;$pwd%dng&quot; | select -ExpandProperty path)&#160; ;&#160;&#160; dir *.dng |      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where {$paths -notcontains $_.FullName} | move -Destination scrap -whatif </code>    <br />&#160; Stores paths of lightroom items in the current folder ending in .DNG in $paths;&#160; then gets files in the current folder and moves those which are not in $paths (i.e. in Lightroom.) specifying&#160; -Whatif allows the files to be confirmed before being moved. </p>
<p><code>Get-LightRoomCollection</code> to list all collections     <br /><code>Get-LightRoomCollectionItem -include musicians | copy -Destination e:\raw\musicians</code>&#160;&#160;&#160; to copies the original files in the “musicians” collection to another disk </p>
<p>I’ve <a href="http://sdrv.ms/MzOqm4">shared the PowerShell code on Skydrive</a></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/databases-sql/'>Databases / SQL</a>, <a href='http://jamesone111.wordpress.com/category/photography/'>Photography</a>, <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1846&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/08/09/getting-to-the-data-in-adobe-lightroomwith-or-without-powershell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/08/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/08/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>The cloud, passwords, and problems of trust and reliance</title>
		<link>http://jamesone111.wordpress.com/2012/08/07/the-cloud-passwords-and-problems-of-trust-and-reliance/</link>
		<comments>http://jamesone111.wordpress.com/2012/08/07/the-cloud-passwords-and-problems-of-trust-and-reliance/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 21:02:29 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Privacy]]></category>
		<category><![CDATA[Security and Malware]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1840</guid>
		<description><![CDATA[In recent days a story has been emerging of a guy called Mat Honan. Mat got hacked, the hackers wanted his twitter account simply because he had a three letter twitter name. Along the way they wiped his Google mail account and (via Apple’s iCloud) his iPhone, iPad and his Macbook. Since he relied on [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1840&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In recent days a story has been emerging of a guy called <a href="http://www.wired.com/gadgetlab/2012/08/apple-amazon-mat-honan-hacking/all/">Mat Honan</a>. <strong>Mat got hacked</strong>, the hackers wanted his twitter account simply because he had a three letter twitter name. Along the way they <strong>wiped his Google mail account and (via Apple’s iCloud) his iPhone, iPad and his Macbook</strong>. Since he relied on stuff being backed up in the cloud he lost irreplaceable family photos, and lord only knows what else. There are two possible reactions <a href="http://en.wikipedia.org/wiki/Schadenfreude">Schadenfreude</a> – “Ha, ha I don’t rely on Google or Apple look what happens to people who do” , “What an idiot, not having a backup”, or “There but for the grace of God goes any of us”. </p>
<p>Only people who’ve never lost data can feel unsympathetic to Mat and <strong>I’ve lost data.</strong> I’ve known tapes which couldn’t be read on a new unit after the old one was destroyed in a fire. I’ve learnt by way of a disk crash that a server wasn’t running it’s backups correctly. I’ve gone back to optical media which couldn’t be read. My backup drive failed a while back – though fortunately everything on it existed somewhere else, making a new backup showed me in just how many places. I’ve had memory cards fail in the camera before I had copied the data off them and I had some photos which existed only on a laptop and a memory card which were in the same bag that got stolen (the laptop had been backed up the day before the photos were taken). The spare memory card I carry on my key-ring failed recently, and I carry that because I’ve turned up to shoot photos with no memory card in the camera – never close the door on the camera with the battery or memory card out. I treat memory cards like film and just buy more and keep the old cards as a backstop copy. So <strong>my data practices look like a mixture of paranoia and superstition and I know, deep down, that nothing is infallible. </strong></p>
<p>For many of us everything we have in the cloud comes down to one password. I don’t mean that we logon everywhere with “Secret1066!”&#160; (no, not my password). But most of us have one or perhaps two email address which we use when we register.&#160; I have one password which I use on many, many sites which require me to create an identity but that identity doesn’t secure anything meaningful to me. It doesn’t meet the rules of some sites (and I get increasingly cross with sites which define their own standards for passwords) and on those sites I will set a one off password. Like “2dayisTuesday!” when I come to use the site again I’ll just ask them to reset my password. <strong>Anything I have in the cloud is only as secure as my email password.&#160; <br /></strong>There are <strong>Some hints</strong> here, first: <strong>any site which can mail you your current password doesn’t encrypt it properly </strong>the proper way to store passwords is as something computed from the password so it is only possible to tell if the right password was entered not what the password is. And second, these computations are case sensitive and set no maximum password length, so <strong>any site which is case insensitive or limits password length probably doesn’t have your details properly secured.&#160; </strong>Such sites are out there – <a href="http://www.novainfosecportal.com/2012/08/01/tesco-just-the-tip-of-the-plaintext-password-iceberg/">Tesco</a> for example – and if we want to use them we have to put up with their security. However if they get hacked (and you do have to ask , if they can’t keep passwords securely, what other weaknesses are there ?) your user name , email and password are in the hands of the hackers, so you had better use different credentials anywhere security matters – which of course means on your mailbox. </p>
<p>So your email password is the one password to rule them all and obviously needs to be secure. But there is a weak link, and that seems to be where the people who hacked Mat found a scary loophole. The easiest way into someone’s mailbox might be to get an administrator to reset the password over the phone – not to guess or brute force it. The only time I had my password reset at Microsoft the new one was left on my voicemail – so I had to be able to login to that. If the provider texts the password to a mobile phone or resets it (say) to the town where you born (without saying what it is) that offers a level of protection; but – be honest – do you know what it takes to get someone at your provider to reset your password, or what the protocol is ?&#160; In Mat’s case the provider was Apple – for whom the hacker knew an exploitable weakness &#8211; but it would be naive to think that Apple was uniquely vulnerable.</p>
<p>Mat’s pain may show the risk in having <strong>only a mailbox providers password reset policy to keep a hacker out</strong> of your computer and/or your (only) backup. One can build up a fear of other things that stop you having access to either computer or backup without knowing how realistic they are.&#160; I like knowing that my last few phones could be wiped easily but would I want remote wipe of a laptop ? When my laptop was stolen there wasn’t any need to wipe it remotely as it had full volume encryption with Microsoft’s bitlocker (saving me a difficult conversation with corporate security) and after this story I’ll stick to that. Cloud storage does give me <u>off-site</u> backup and that’s valuable – it won’t be affected if I have a fire or flood at home &#8211; but I will continue to put my faith in traditional <u>off-line backup</u> and I’ve just ordered more disk capacity for that.</p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/privacy/'>Privacy</a>, <a href='http://jamesone111.wordpress.com/category/security-and-malware/'>Security and Malware</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1840&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/08/07/the-cloud-passwords-and-problems-of-trust-and-reliance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Rotating pictures from my portfolio on the Windows 7 Logon screen</title>
		<link>http://jamesone111.wordpress.com/2012/07/31/rotating-pictures-from-my-portfolio-on-the-windows-7-logon-screen/</link>
		<comments>http://jamesone111.wordpress.com/2012/07/31/rotating-pictures-from-my-portfolio-on-the-windows-7-logon-screen/#comments</comments>
		<pubDate>Tue, 31 Jul 2012 12:15:21 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1838</guid>
		<description><![CDATA[In the last 3 posts I outlined my Get-IndexedItem function for accessing windows Search. The more stuff I have on my computers the harder it is to find a way of classifying it so it fits into hierarchical folders : the internet would be unusable without search, and above a certain number of items local [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1838&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In the last 3 posts I outlined my <code>Get-IndexedItem</code> function for accessing windows Search. The more stuff I have on my computers the harder it is to find a way of classifying it so it fits into hierarchical folders : the internet would be unusable without search, and above a certain number of items local stuff is too.&#160; Once I got search I start leaving most mail in my Inbox and outlook uses search to find what I want; I have one “book” in Onenote with a handful of sections and if I can’t remember where I put something, search comes to the rescue. <strong>I take the time to tag photos</strong> so that I don’t have to worry too much about finding a folder structure to put them in. So I’ll tag <strong>geographically</strong>&#160; (I only have a few pictures from India – one three week trip, so India gets one tag but UK pictures get divided by County , and in counties with many pictures I put something like Berkshire/Reading. Various tools will make a hierarchy with Berkshire then Newbury, Reading etc) <strong>People get tagged by name</strong> – Friends and Family being a prefix to group those and so on. I use <strong>Windows’ star ratings </strong>to tag pictures I like – whether I took them or not – and Windows “use top rated pictures” for the Desktop background picks those up. I also have a tag of<strong> “Portfolio”</strong></p>
<p>Ages ago I wrote about <a href="http://jamesone111.wordpress.com/2009/05/07/customizing-the-windows-7-logon-screen-no-additional-tools-required/">Customizing the Windows 7 logon screen</a>. So I had the idea “Why not find pictures with the Portfolio tag, and make them logon backgrounds.”&#160; <a href="http://jamesone111.wordpress.com/2010/07/07/working-with-the-image-module-for-powershell-part-3-gps-and-other-data/">Another old post covers PowerShell tools for manipulating images</a> so I could write a script to do it, and use <strong>Windows scheduled tasks</strong> to run that script each time I unlocked the computer so that the next time I went to the logon screen I would have a different picture. That was the genesis of <code>Get-IndexedItem. </code>And I’ve added it, together with the New-LogonBackground to the image module <a href="http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Image-module-caa4405a"><strong>download on the Technet Script Center</strong></a><strong> </strong></p>
<p>If you read that old post you’ll see one of the things we depend on is setting a registry key so the function checks that registry key is set and writes a warning if it isn’t: </p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Get-ItemProperty</span> <span style="color:#8a2be2;">HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background        <br />&#160;&#160;&#160;&#160;&#160; </span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">oembackground</span> <span style="color:#a9a9a9;">-ne</span> <span style="color:#800080;">1</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Warning</span> <span style="color:#8b0000;">&quot;Registry Key OEMBackground under        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background needs to be set to 1&quot;</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; Write-Warning</span> <span style="color:#8b0000;">&quot;Run AS ADMINISTRATOR with -SetRegistry to set the key and try again.&quot;</span>       <br /><span style="color:#000000;">}</span></code> </p>
<p>So if the registry value isn’t set to 1, the function prints a warning which tells the user to run with <code>–SetRegistry</code>. After testing this multiple times – I found changing <strong>windows theme resets the value</strong> – and forgetting to run PowerShell with elevated permissions, I put in a try / catch to pick this up and say “Run Elevated”. Just as a side note here I always find when I write try/catch it doesn’t work and it takes me a moment to remember catch works on <em>terminating</em> errors and the command you want to catch must usually needs <code>–ErrorAction stop </code></p>
<p><code><span style="color:#00008b;"></span></code></p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$SetRegistry</span> <span style="color:#000000;">)</span> <span style="color:#000000;">{</span>       <br /><span style="color:#00008b;">&#160; try</span><span style="color:#000000;">{ </span><span style="color:#0000ff;">Set-ItemProperty</span> <span style="color:#000080;">-Name</span> <span style="color:#8a2be2;">oembackground</span> <span style="color:#000080;">-Value</span> <span style="color:#800080;">1</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8a2be2;">Stop `        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000080;">-PATH</span> <span style="color:#8b0000;">&quot;HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Background&quot;</span>&#160;</span>       <br /><span style="color:#000000;">&#160;&#160; }</span>       <br /><span style="color:#00008b;">&#160; catch</span> <span style="color:#008080;">[System.Security.SecurityException]</span><span style="color:#000000;">{</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160; Write-Warning</span> <span style="color:#8b0000;">&quot;Permission Denied - you need to run as administrator&quot;</span>       <br /><span style="color:#000000;">&#160; }</span>       <br /><span style="color:#00008b;">&#160;&#160; return</span>       <br /><span style="color:#000000;">}</span> </code></p>
<p><code></code>The function also tests that it can write to the directory where the images are stored, since this doesn’t normally have user access: if it can’t write a file, it tells the user to set the permissions. Instead of using <code>try/catch</code> here I use <code>$?</code> to see if the previous command was successful     <br /><code><span style="color:#0000ff;">Set-content</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8b0000;">&quot;Silentlycontinue&quot;</span> <span style="color:#000080;">-Path</span> <span style="color:#8b0000;">&quot;$env:windir\System32\oobe\Info\Backgrounds\testFile.txt&quot;</span> `       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000080;">-Value</span> <span style="color:#8b0000;">&quot;This file was created to test for write access. It is safe to remove&quot;</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">-not</span> <span style="color:#ff4500;">$?</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#0000ff;">write-warning</span> <span style="color:#8b0000;">&quot;Can't create files in $env:windir\System32\oobe\Info\Backgrounds please set permissions and try again&quot;</span><span style="color:#000000;">        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#00008b;">return        <br /></span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">else</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#0000ff;">Remove-Item</span> <span style="color:#000080;">-Path</span> <span style="color:#8b0000;">&quot;$env:windir\System32\oobe\Info\Backgrounds\testFile.txt&quot;</span><span style="color:#000000;">}</span>       <br /></code></p>
<p>The next step is to find the size of the monitor. Fortunately, there is a WMI object for that, but <strong>not all monitor sizes are supported as bitmap sizes</strong> so the function takes <code>–Width</code> and <code>–Height</code> parameters. If these aren’t specified it gets the value from WMI and allows for a couple of special cases – my testing has not been exhaustive, so other resolutions may need special handling. The Width and height determine the filename for the bitmap, and later the function check the aspect ratio – so it doesn’t try to crop a portrait image to fit landscape monitor. </p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">-not</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$width</span> <span style="color:#a9a9a9;">-and</span> <span style="color:#ff4500;">$height</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160; $mymonitor</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">Get-WmiObject</span> <span style="color:#8a2be2;">Win32_DesktopMonitor</span> <span style="color:#000080;">-Filter</span> <span style="color:#8b0000;">&quot;availability = '3'&quot;</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">select</span> <span style="color:#000080;">-First</span> <span style="color:#800080;">1</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160; $width</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$height</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$mymonitor</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ScreenWidth</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$mymonitor</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ScreenHeight</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$width</span> <span style="color:#a9a9a9;">-eq</span> <span style="color:#800080;">1366</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$width</span> <span style="color:#a9a9a9;">=</span> <span style="color:#800080;">1360</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$width</span> <span style="color:#a9a9a9;">-eq</span> <span style="color:#800080;">1920</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-and</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$height</span> <span style="color:#a9a9a9;">-eq</span> <span style="color:#800080;">1080</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$width</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$height</span> <span style="color:#a9a9a9;">=</span> <span style="color:#800080;">1360</span><span style="color:#a9a9a9;">,</span><span style="color:#800080;">768</span><span style="color:#000000;">}</span>       <br /><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#000000;">@(</span><span style="color:#8b0000;">&quot;768x1280&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;900x1440&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;960x1280&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1024x1280&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1280x1024&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1024x768&quot;</span> <span style="color:#a9a9a9;">,</span> <span style="color:#8b0000;">&quot;1280x960&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1600x1200&quot;</span><span style="color:#a9a9a9;">,</span>       <br /><span style="color:#8b0000;">&#160;&#160;&#160;&#160;&#160; &quot;1440x900&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1920x1200&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1280x768&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;1360x768&quot;</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-notcontains</span> <span style="color:#8b0000;">&quot;$($width)x$($height)&quot;</span> <span style="color:#000000;">)</span> </code><code><span style="color:#000000;">{</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160; write-warning</span> <span style="color:#8b0000;">&quot;Screen resolution is not one of the defaults. You may need to specify width and height&quot;</span>       <br /><span style="color:#000000;">}</span>       <br /></code><code><span style="color:#ff4500;">$MonitorAspect</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Width</span> <span style="color:#a9a9a9;">/</span> <span style="color:#ff4500;">$height</span>       <br /><span style="color:#ff4500;">$SaveName</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;$env:windir\System32\oobe\Info\Backgrounds\Background$($width)x$height.jpg&quot;</span>       <br /></code></p>
<p>The next step is to get the image – <code>Get-Image</code> is part of the <a href="http://jamesone111.wordpress.com/2010/07/07/working-with-the-image-module-for-powershell-part-3-gps-and-other-data/">PowerShell tools for manipulating images</a> .</p>
<p> <code><span style="color:#ff4500;">$myimage</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">Get-IndexedItem</span> <span style="color:#000080;">-path</span> <span style="color:#ff4500;">$path</span> <span style="color:#000080;">-recurse</span> <span style="color:#000080;">-Filter</span> <span style="color:#8b0000;">&quot;Kind=Picture&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;keywords='$keyword'&quot;</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#8b0000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &quot;store=File&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;width &gt;= $width &quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;height &gt;= $height &quot;</span> <span style="color:#a9a9a9;">|</span>     <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where-object</span> <span style="color:#000000;">{</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">width</span> <span style="color:#a9a9a9;">-gt</span> <span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">height</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-eq</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$width</span> <span style="color:#a9a9a9;">-gt</span> <span style="color:#ff4500;">$height</span><span style="color:#000000;">)</span><span style="color:#000000;">}</span> <span style="color:#a9a9a9;">|      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#0000ff;">get-random</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">get-image</span></code>
<p><code>Get-Indexed</code> item looks for files in folder specified by <code>–Path</code> parameter – which defaults to <code>[system.environment]::GetFolderPath( [system.environment+specialFolder]::MyPicture </code>- the approved way to find the &quot;my pictures&quot; folder <code>-recurse</code> tells it to look in sub-folders and it looks for a file with keywords which match the <code>–Keyword</code> Parameter (which defaults to “Portfolio”). It filters out pictures which are smaller than the screen and then <code>where-object</code> filters the list down to those with have the right aspect ratio. Finally one image is selected at random and piped into <code>Get-Image</code>.</p>
<p>If this is successful , the function logs what it is doing to the event log. I set up a new log source “PSLogonBackground” in the application log by running PowerShell as administrator and using the command    <br /><code><span style="color:#006400;"><font color="#000000">New-EventLog -Source PSLogonBackground -LogName application          <br /></font></span></code>Then my script can use that as a source – since I don’t want to bother the user if the log isn’t configured I use <code>-ErrorAction silentlycontinue</code> here<code>      <br /><span style="color:#0000ff;">write-eventlog</span> <span style="color:#000080;">-logname</span> <span style="color:#8a2be2;">Application</span> <span style="color:#000080;">-source</span> <span style="color:#8a2be2;">PSLogonBackground</span> <span style="color:#000080;">-eventID</span> <span style="color:#800080;">31365</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8a2be2;">silentlycontinue `        <br /><span style="color:#000080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -message</span> <span style="color:#8b0000;">&quot;Loaded $($myImage.FullName) [ $($myImage.Width) x $($myImage.Height) ]&quot;</span> </span>      <br /></code></p>
<p><a href="http://jamesone111.files.wordpress.com/2012/07/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/07/image_thumb.png?w=804&#038;h=102" width="804" height="102" /></a></p>
<p>The next thing the function does is to apply <em>cropping</em> and <em>scaling</em> image filters from the original image module as required to get the image to the right size.&#160; When it has done that it tries to save the file, by applying a <em>conversion</em> filter and saving the result. The initial JPEG quality is passed as a parameter if the file is too big, the function loops round reducing the jpeg quality until the file fits into the 250KB limit and logs the result to the event log. </p>
<p><code><span style="color:#0000ff;">Set-ImageFilter</span> <span style="color:#000080;">-filter</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Add-ConversionFilter</span> <span style="color:#000080;">-typeName</span> <span style="color:#8b0000;">&quot;JPG&quot;</span> <span style="color:#000080;">-quality</span> <span style="color:#ff4500;">$JPEGQuality</span> –<span style="color:#000080;">passthru</span><span style="color:#000000;">)</span> <span style="color:#000080;">-image</span> <span style="color:#ff4500;">$myimage</span> <span style="color:#000080;">-Save</span> <span style="color:#ff4500;">$saveName</span>       <br /></code><code><span style="color:#ff4500;">$item</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">get-item</span> <span style="color:#ff4500;">$saveName</span> </code><code>     <br /><span style="color:#00008b;">while</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$item</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">length</span> <span style="color:#a9a9a9;">-ge</span> <span style="color:#800080;">250kb</span> <span style="color:#a9a9a9;">-and</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$JPEGQuality</span> <span style="color:#a9a9a9;">-ge</span> <span style="color:#800080;">15</span><span style="color:#000000;">)</span> <span style="color:#000000;">)</span> <span style="color:#000000;">{</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $JPEGQuality</span> –<span style="color:#a9a9a9;">=</span> <span style="color:#800080;">5</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160; Write-warning</span> <span style="color:#8b0000;">&quot;File too big - Setting Quality to $Jpegquality and trying again&quot;</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160; Set-ImageFilter</span> <span style="color:#000080;">-filter</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Add-ConversionFilter</span> <span style="color:#000080;">-typeName</span> <span style="color:#8b0000;">&quot;JPG&quot;</span> <span style="color:#000080;">-quality</span> <span style="color:#ff4500;">$JPEGQuality</span> <span style="color:#000080;">-passThru</span><span style="color:#000000;">)</span> <span style="color:#000080;">-image</span> <span style="color:#ff4500;">$myimage</span> <span style="color:#000080;">-Save</span> <span style="color:#ff4500;">$saveName</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $item</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">get-item</span> <span style="color:#ff4500;">$saveName</span>       <br /><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$item</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">length</span> <span style="color:#a9a9a9;">-le</span> <span style="color:#800080;">250KB</span><span style="color:#000000;">)</span> <span style="color:#000000;">{        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#0000ff;">write-eventlog</span> <span style="color:#000080;">-logname</span> <span style="color:#8a2be2;">Application</span> <span style="color:#000080;">-source</span> <span style="color:#8a2be2;">PSLogonBackground</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8a2be2;">silentlycontinue `        <br /><span style="color:#000080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -eventID</span> <span style="color:#800080;">31366</span> <span style="color:#000080;">-message</span> <span style="color:#8b0000;">&quot;Saved $($Item.FullName) : size $($Item.length)&quot;</span>         <br /></span>&#160;<span style="color:#000000;">}</span></code></p>
<p><a href="http://jamesone111.files.wordpress.com/2012/07/image1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/07/image_thumb1.png?w=514&#038;h=193" width="514" height="193" /></a></p>
<p>That’s it. If you <a href="http://gallery.technet.microsoft.com/scriptcenter/PowerShell-Image-module-caa4405a">download the module</a><strong></strong><strong>&#160;</strong> remove the “Internet block” on the zip file and expand the files into \users\YourUserName\windowsPowerShell\modules, and try running New-logonbackground&#160; (with –Verbose to get extra information if you wish).     <br />If the permissions on the folder have been set, the registry key is set,&#160; pressing [Ctrl]+[Alt]+[Del] should reveal a new image.&#160; YOU might want to use a different keyword or a different path or start by trying to use a higher JPEG quality in which case you can run it with parameters as needed. </p>
<p>Then it is a matter of setting up the scheduled task: here are the settings from my scheduler</p>
<p><a href="http://jamesone111.files.wordpress.com/2012/07/image2.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/07/image_thumb2.png?w=496&#038;h=411" width="496" height="411" /></a></p>
<p><a href="http://jamesone111.files.wordpress.com/2012/07/image3.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/07/image_thumb3.png?w=496&#038;h=411" width="496" height="411" /></a></p>
<p><a href="http://jamesone111.files.wordpress.com/2012/07/image4.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/07/image_thumb4.png?w=496&#038;h=411" width="496" height="411" /></a></p>
<p>The program here is the full path to Powershell.exe and the parameters box contains   <br /><code>-noprofile -windowstyle hidden -command &quot;Import-Module Image; New-logonBackground&quot;</code></p>
<p>Lock, unlock and my background changes. Perfect. It’s a nice talking point and a puzzle – sometimes people like the pictures (although someone said one of a graveyard was morbid) – and sometimes they wonder how the background they can see is not only <em>not </em>the standard one but <em>not </em>the one they saw previously. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/photography/'>Photography</a>, <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1838&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/07/31/rotating-pictures-from-my-portfolio-on-the-windows-7-logon-screen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/07/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/07/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/07/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/07/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/07/image_thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Windows index to search from PowerShell: Part three. Better function output</title>
		<link>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/#comments</comments>
		<pubDate>Sat, 30 Jun 2012 10:53:31 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1821</guid>
		<description><![CDATA[Note: this was originally written for the Hey,Scripting guy blog where it appeared as the 27 June 2012 episode. The code is available for download . I have some more index related posts coming up so I wanted to make sure everything was in one place In part one, I introduced a function which queries [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1821&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>Note: this was originally written for the </em><a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/27/customizing-powershell-output-from-windows-search.aspx">Hey,Scripting guy blog</a><em></em><em> where it appeared as the 27 June 2012 episode. </em><a href="http://gallery.technet.microsoft.com/scriptcenter/Get-IndexedItem-PowerShell-5bca2dae"><em>The code is available for download</em></a><em> . I have some more index related posts coming up so I wanted to make sure everything was in one place</em><br />
<hr />In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-one-building-a-query-from-user-input/">part one,</a> I introduced a function which queries the Windows Index using filter parameters like
<ul>
<li>&quot;Contains(*,&#8217;Stingray&#8217;)&quot; </li>
<li>&quot;System.Keywords = &#8216;Portfolio&#8217; &quot; </li>
<li>&quot;System.Photo.CameraManufacturer LIKE &#8216;CAN%&#8217; &quot; </li>
<li>&quot;System.image.horizontalSize &gt; 1024&quot; </li>
</ul>
<p> In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/">part two,</a> I showed how these parameters could be simplified to
<ul>
<li>Stingray (A word on its own becomes a <i>contains</i> term) </li>
<li>Keyword=Portfolio (Keyword, without the S is an alias for System.Keywords and quotes will be added automatically)) </li>
<li>CameraManufacturer=CAN* (* will become %, and = will become LIKE, quotes will be added and CameraManufacturer will be prefixed with System.Photo) </li>
<li>Width &gt; 1024 (Width is an alias or System.image.horizontalsize, and quotes are not added around numbers). </li>
</ul>
<p>There is one remaining issue. PowerShell is designed so that one command&#8217;s output becomes another&#8217;s input. This function isn&#8217;t going to do much with Piped input: I can&#8217;t see another command spitting out search terms for this one, nor can I multiple paths being piped in. But the majority of items found by a search will be <i>files</i>: and so it should be possible to treat them like files, piping them into copy-item or whatever.     <br />The following was my first attempt at transforming the data rows into something more helpful</p>
<p><code><span style="color:#ff4500;">$Provider</span><span style="color:#a9a9a9;">= </span><span style="color:#8b0000;">&quot;Provider=Search.CollatorDSO; Extended Properties=’Application=Windows’;&quot;</span>       <br /><span style="color:#ff4500;">$adapter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.oledb.oleDBDataadapter</span> <span style="color:#000080;">-argument</span> <span style="color:#ff4500;">$SQL</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Provider</span>       <br /><span style="color:#ff4500;">$ds</span>&#160;&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.dataset</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$adapter</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Fill</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$ds</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>       <br />&#160;&#160;&#160;&#160; </code><code><span style="color:#000000;">{</span> <span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$ds</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Tables</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;^file:&quot;</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$obj</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">psobject</span> <span style="color:#000080;">-Property</span> <span style="color:#000000;">@{</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;^file:&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;&quot;</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\/&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;\&quot;</span><span style="color:#000000;">)</span><span style="color:#000000;">}</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$obj</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">psobject</span> <span style="color:#000080;">-Property</span> <span style="color:#000000;">@{</span><span style="color:#000000;">Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span><span style="color:#000000;">}</span></code><code><span style="color:#000000;">}</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Add-Member</span> <span style="color:#000080;">-force</span> <span style="color:#000080;">-Input</span> <span style="color:#ff4500;">$obj</span> <span style="color:#000080;">-Name</span> <span style="color:#8b0000;">&quot;ToString&quot;</span> <span style="color:#000080;">-MemberType</span> <span style="color:#8b0000;">&quot;scriptmethod&quot;</span> <span style="color:#000080;">-Value</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$this</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">path</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$prop</span> <span style="color:#00008b;">in</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Get-Member</span> <span style="color:#000080;">-InputObject</span> <span style="color:#ff4500;">$row</span> <span style="color:#000080;">-MemberType</span> <span style="color:#8a2be2;">property</span> <span style="color:#a9a9a9;">|</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; where-object</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;$($_.name)&quot;</span> <span style="color:#a9a9a9;">-isnot</span> <span style="color:#008080;">[system.dbnull]</span> <span style="color:#000000;">}</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span> <span style="color:#0000ff;">Add-Member</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8b0000;">&quot;SilentlyContinue&quot;</span> <span style="color:#000080;">-InputObject</span> <span style="color:#ff4500;">$obj</span> <span style="color:#000080;">-MemberType</span> <span style="color:#8a2be2;">NoteProperty ` </span>      <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -Name</span> <span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$prop</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">name</span> <span style="color:#a9a9a9;">-split</span> <span style="color:#8b0000;">&quot;\.&quot;</span> <span style="color:#000000;">)</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">-1</span><span style="color:#a9a9a9;">]</span><span style="color:#000000;">)</span> <span style="color:#000080;">-Value</span> <span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;$($prop.name)&quot;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$prop</span> <span style="color:#00008b;">in</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$PropertyAliases</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Keys</span> <span style="color:#a9a9a9;">|</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Where-Object</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;$($propertyAliases.$_)&quot;</span> <span style="color:#a9a9a9;">-isnot</span> <span style="color:#008080;">[system.dbnull]</span> <span style="color:#000000;">}</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; { </span><span style="color:#0000ff;">Add-Member</span> <span style="color:#000080;">-ErrorAction</span> <span style="color:#8b0000;">&quot;SilentlyContinue&quot;</span> <span style="color:#000080;">-InputObject</span> <span style="color:#ff4500;">$obj</span> <span style="color:#000000;">` </span><span style="color:#000080;">-MemberType</span> <span style="color:#8a2be2;">AliasProperty `</span>&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000080;">-Name</span> <span style="color:#ff4500;">$prop</span> <span style="color:#000000;">` </span><span style="color:#000080;">-Value</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$propertyAliases</span><span style="color:#a9a9a9;">.</span><span style="color:#ff4500;">$prop</span> <span style="color:#a9a9a9;">-split</span> <span style="color:#8b0000;">&quot;\.&quot;</span> <span style="color:#000000;">)</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">-1</span><span style="color:#a9a9a9;">]</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $obj</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }        <br /></span></code><code><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160; </span><span style="color:#000000;">}</span>       <br /></code>This is where the function spends most of its time, looping through the data creating a custom object for each row; non-file items are given a path property which holds the System.ItemURL property; for files the ItemUrl is processed into normal format (rather than file:c/users/james format) – <b>in many cases the item can be piped into another command successfully if it just has a <i>Path</i> property.</b></p>
<p>Then, for each property (database column) in the row a member is added to the custom object with a shortened version of the property name and the value (assuming the column isn&#8217;t empty).    <br />Next, alias properties are added using the definitions in $PropertyAliases.     <br />Finally some standard members get added. In this version I&#8217;ve pared it down to a single method, because <b>several things expect to be able to get the path for a file by calling its tostring()</b> method. </p>
<p>When I had all of this working I tried to get clever. I added aliases for all the properties which normally appear on a System.IO.FileInfo object and even tried fooling PowerShell&#8217;s formatting system into treating my file items as a file object, something that only needs one extra line of code    <br /><code><span style="color:#ff4500;">$Obj</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">psobject</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">typenames</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">insert</span><span style="color:#000000;">(</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span> <span style="color:#8b0000;">&quot;SYSTEM.IO.FILEINFO&quot;</span><span style="color:#000000;">)</span></code>     <br />Pretending a custom object is actually another type seems dangerous, but everything I tried seemed happy provided the right properties were present. The formatting worked except for the &quot;Mode&quot; column. I found the method which that calculates .Mode for FILEINFO objects, but it needs a <i>real</i> FILEINFO object. It was easy enough to get one – I had the path and it only needs a call to Get‑Item but I realized that if I was getting a FILEINFO object anywhere in the process, then it made more sense to add extra properties to that object and dispense with the custom object. I added an extra switch -NoFiles to supress this behaviour     <br />So the code then became     <br /><code><span style="color:#ff4500;">$Provider </span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">&quot;Provider=Search.CollatorDSO; Extended Properties=’Application=Windows’;&quot;</span>       <br /><span style="color:#ff4500;">$adapter </span>&#160;<span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.oledb.oleDBDataadapter</span> <span style="color:#000080;">-argument</span> <span style="color:#ff4500;">$SQL</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Provider</span>       <br /><span style="color:#ff4500;">$ds</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.dataset</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$adapter</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Fill</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$ds</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>       <br />&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span> <span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$ds</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Tables</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span><span style="color:#000000;">)        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#000000;">{</span> <span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;^file:&quot;</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-and</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">-not</span> <span style="color:#ff4500;">$NoFiles</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$obj</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">Get-item</span> <span style="color:#000080;">-Path</span> <span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;^file:&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;&quot;</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\/&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;\&quot;</span><span style="color:#000000;">)</span><span style="color:#000000;">}</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$obj</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">psobject</span> <span style="color:#000080;">-Property</span> <span style="color:#000000;">@{</span><span style="color:#000000;">Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$row</span><span style="color:#a9a9a9;">.</span><span style="color:#8b0000;">&quot;System.ItemUrl&quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Add-Member</span> <span style="color:#000080;">-force</span> <span style="color:#000080;">-Input</span> <span style="color:#ff4500;">$obj</span> <span style="color:#000080;">-Name</span> <span style="color:#8b0000;">&quot;ToString&quot;</span> <span style="color:#000080;">-MemberType</span> <span style="color:#8b0000;">&quot;scriptmethod&quot;</span> <span style="color:#000080;">-Value</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$this</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">path</span><span style="color:#000000;">}</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color:#000000;">ForEach... </span></code>&#160; <br />The initial code was 36 lines, making the user input more friendly took it to 60 lines, and the output added about another 35 lines, bring the total to 95.     <br />There were 4 other kinds of output I wanted to produce:</p>
<ul>
<li><b>Help</b>. I added comment-based-help with plenty of examples. It runs to 75 lines making it the biggest constituent in the finished product.       <br />In addition I have 50 lines that are comments or blank for readability as insurance against trying to understand what those regular expressions do in a few months&#8217; time – but there are only 100 lines of actual code. </li>
<li><b>A</b> –list <b>switch</b> which lists the long and short names for the fields (including aliases) </li>
<li>Support for the –Debug <b>switch</b> – because so many things might go wrong, I have write‑debug $SQL immediately before I carry out the query, and to enable it that I have       <br />[CmdletBinding()] before I declare the parameters. </li>
<li><b>A </b>–Value<b> switch</b> which uses the <a href="http://msdn.microsoft.com/en-us/library/bb231281(v=VS.85).aspx">GROUP ON… OVER… search syntax</a> so I can see what the possible values are in a column.       <br />GROUP ON queries are unusual because they fill the dataset with TWO tables.       <br />GROUP ON System.kind OVER ( SELECT STATEMENT) will produce a something like this as the first table. </li>
</ul>
<p> <code>SYSTEM.KIND&#160;&#160; Chapter    <br />-----------&#160;&#160; -------     <br />communication 0     <br />document&#160;&#160;&#160;&#160;&#160; 1     <br />email&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 2     <br />folder&#160;&#160;&#160;&#160;&#160;&#160;&#160; 3     <br />link&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 4     <br />music&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 5     <br />picture&#160;&#160;&#160;&#160;&#160;&#160; 6     <br />program&#160;&#160;&#160;&#160;&#160;&#160; 7     <br />recordedtv&#160;&#160;&#160; 8</code>
<p>The second table is the normal data suitably sorted. In this case it has all the requested fields grouped by kind plus one named &quot;Chapter&quot;, which ties into the first table. I&#8217;m not really interested in the second table but the first helps me know if I should enter &quot;Kind=image&quot;, &quot;Kind=Photo&quot; or &quot;Kind=Picture&quot;</p>
<p>I have a Select-List function which I use in my <a href="http://psconfig.codeplex.com">configurator</a> and <a href="http://pshyperv.codeplex.com/">Hyper-V library</a>, and with this I can choose which recorded TV program to watch, first selecting by title, and then if there is more than one episode, by episode.     <br /><code><span style="color:#ff4500;">$t</span><span style="color:#a9a9a9;">=</span><span style="color:#000000;">(</span><span style="color:#0000ff;">Get-IndexedItem</span> <span style="color:#000080;">-Value</span> <span style="color:#8b0000;">&quot;title&quot;</span> <span style="color:#000080;">-filter</span> <span style="color:#8b0000;">&quot;kind=recordedtv&quot;</span> <span style="color:#000080;">-recurse</span> <span style="color:#a9a9a9;">|</span>       <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#0000ff;">Select-List</span> <span style="color:#000080;">-Property</span> <span style="color:#8a2be2;">title</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">title</span>       <br /><span style="color:#0000ff;">start</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Get-IndexedItem</span> <span style="color:#000080;">-filter</span> <span style="color:#8b0000;">&quot;kind=recordedtv&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;title='$t'&quot;</span> <span style="color:#000080;">-path</span> <span style="color:#a9a9a9;">|        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#0000ff;">Select-List</span> <span style="color:#000080;">-Property</span> <span style="color:#8a2be2;">ORIGINALBROADCASTDATE</span><span style="color:#a9a9a9;">,</span><span style="color:#8a2be2;">PROGRAMDESCRIPTION</span><span style="color:#000000;">)</span></code></p>
<p><code><span style="color:#000000;"></span>      <br /></code>In a couple of follow up posts I’ll show some of the places I use Get-IndexedItem. But for now feel free to <a href="http://gallery.technet.microsoft.com/scriptcenter/Get-IndexedItem-PowerShell-5bca2dae">download the code</a> and experiment with it. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1821&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Windows index to search from PowerShell:Part Two &#8211; Helping with user input</title>
		<link>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/#comments</comments>
		<pubDate>Sat, 30 Jun 2012 10:46:36 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1819</guid>
		<description><![CDATA[Note: this was originally written for the Hey,Scripting guy blog where it appeared as the 26 June 2012 episode. The code is available for download . I have some more index related posts coming up so I wanted to make sure everything was in one place In part one I developed a working PowerShell function [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1819&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>Note: this was originally written for the <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/26/search-windows-index-with-powershell-helping-with-input.aspx">Hey,Scripting guy blog</a></em><em></em><em> where it appeared as the 26 June 2012 episode. </em><a href="http://gallery.technet.microsoft.com/scriptcenter/Get-IndexedItem-PowerShell-5bca2dae"><em>The code is available for download</em></a><em> . I have some more index related posts coming up so I wanted to make sure everything was in one place</em> </p>
<hr />In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-one-building-a-query-from-user-input/">part one</a> I developed a working PowerShell function to query the Windows index. It outputs <i>data rows</i> which isn&#8217;t the ideal behaviour and I&#8217;ll address that in <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/">part three</a>; in this part I&#8217;ll address another drawback: <b>search terms passed as parameters to the function must be &quot;SQL-Ready&quot;</b>. I think that makes for a <b>bad user experience</b> so I&#8217;m going to look at the half dozen bits of logic I added to allow my function to process input which is a little more human. <i>Regular expressions</i> are the way to recognize text which must be changed, and I&#8217;ll pay particular attention to those as I know I lot of people find them daunting.<br />
<h3>Replace * with %</h3>
<p>SQL statements use % for wildcard, but selecting files at the command prompt traditionally uses *. It&#8217;s a simple matter to replace – but for the need to &quot;escape&quot; the* character, replacing * with % would be as simple as a –replace statement gets:    <br /><code>$Filter = $Filter -replace &quot;\*&quot;,&quot;%&quot;      <br /></code>For some reason I&#8217;m never sure if the camera maker is Canon or Cannon so I&#8217;d rather search for Can*… or rather Can%, and that replace operation will turn &quot;CameraManufacturer=Can*&quot; into &quot;CameraManufacturer=Can%&quot;. It&#8217;s worth noting that –<em>replace</em> is just as happy to process an array of strings in $<em>filter</em> as it is to process one. </p>
<p>Searching for a term across all fields uses &quot;CONTAINS (*,&#8217;Stingray&#8217;)&quot;, and if the -replace operation changes* to % <i>inside</i> a CONTAINS() the result is no longer a valid SQL statement. So the regular expression needs to be a little more sophisticated, using a &quot;negative look behind&quot;     <br /><code>$Filter = $Filter -replace &quot; &quot;(?&lt;!\(\s*)\*&quot;,&quot;%&quot;</code></p>
<p>In order to filter out cases like CONTAINS(*… , the new regular expression qualifies <i>&quot;Match on *</i>&quot;,with a look behind &#8211; &quot;(?&lt;!\(\s*)&quot; &#8211; which says &quot;<i>if it isn’t immediately preceded by an opening bracket and any spaces</i>&quot;. In regular expression syntax (?= x) says &quot;<i>look ahead for x</i>&quot; and (?&lt;= x) says &quot;<i>Look behind for x</i>&quot; (?!= x) is “<i>look ahead for anything EXCEPT x” and </i>(?&lt;!x) is <i>“look behind for anything EXCEPT x” </i>these will see a lot of use in this function. Here (?&lt;! ) is being used, open bracket needs to be escaped so is written as \( and \s* means 0 or more spaces. </p>
<h3>Convert &quot;orphan&quot; search terms into &#8216;contains&#8217; conditions.</h3>
<p>A term that needs to be wrapped as a &quot;CONTAINS&quot; search can be identified by the absence of quote marks, = , &lt; or &gt; signs or the LIKE, CONTAINS or FREETEXT search predicates. When these are present the search term is left alone, otherwise it goes into CONTAINS, like this.    <br /><code><span style="color:#ff4500;">$filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$filter</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">ForEach-Object</span> <span style="color:#000000;">{</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span>&#160; <span style="color:#000000;">(</span><span style="color:#ff4500;">$_</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;'|=|&lt;|&gt;|like|contains|freetext&quot;</span><span style="color:#000000;">)</span>&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </code><code><span style="color:#000000;">{</span><span style="color:#ff4500;">$_</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160; else</span>&#160;&#160; <span style="color:#000000;">{</span><span style="color:#8b0000;">&quot;Contains(*,'$_')&quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#000000;">}</span><span style="color:#000000;">)</span> </code></p>
<h3>Put quotes in if the user omits them. </h3>
<p>The next thing I check for is omitted quote marks. I said I wanted to be able to use Can*, and we’ve seen it changed to Can% but the search term needs to be transformed into &quot;CameraManufacturer=&#8217;Can%&#8217; &quot;. Here is a –replace operation to do that.    <br /><code><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\s*(=|&lt;|&gt;|like)\s*([^'\d][^\s']*)$&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">' $1 ''$2'' '</span></code>     <br />This is a more complex regular expression which takes a few moments to understand<br />
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="178">
<p><b>Regular expression</b></p>
</td>
<td valign="top" width="276">
<p><b>Meaning</b></p>
</td>
<td valign="top" width="239">
<p><b>Application </b></p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p><b><u>\s*</u></b>(=|&lt;|&gt;|like)<b><u>\s*</u></b>               <br />([^'\d][^\s']*)$</p>
</td>
<td valign="top" width="276">
<p>Any spaces (or none)</p>
</td>
<td valign="top" width="239">&#160;</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*<b>(=|&lt;|&gt;|like)</b>\s*               <br />([^'\d][^s']*)$</p>
</td>
<td valign="top" width="276">
<p>= or &lt; or &gt; or &quot;Like&quot; </p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer<b><u>=</u></b>Can% </p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*(=|&lt;|&gt;|like)\s*              <br />(<b><u>[^'\d]</u></b>[^\s']*)$</p>
</td>
<td valign="top" width="276">
<p>Anything which is NOT a &#8216; character              <br />or a digit</p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer=<b><u>C</u></b>an%</p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*(=|&lt;|&gt;|like)\s*              <br />([^'\d]<b><u>[^\s']*</u></b>)$</p>
</td>
<td valign="top" width="276">
<p>Any number of non-quote,              <br />non-space characters (or none)</p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer=C<b><u>an%</u></b></p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*(=|&lt;|&gt;|like)\s*              <br />([^'\d][^\s']*)<b><u>$</u></b></p>
</td>
<td valign="top" width="276">
<p>End of line</p>
</td>
<td valign="top" width="239">
<p><i></i></p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*<b><u>(</u></b>=|&lt;|&gt;|like<b><u>)</u></b>\s*               <br /><b><u>(</u></b>[^'\d][^\s']*<b><u>)</u></b>$</p>
</td>
<td valign="top" width="276">
<p>Capture the enclosed sections              <br />as matches</p>
</td>
<td valign="top" width="239">
<p>$Matches[0]= &quot;=Can%&quot;              <br />$Matches[1]= &quot;=&quot;               <br />$Matches[2]= &quot;Can%&quot;</p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>&#8216; $1 &#8221;$2&#8221; &#8217;0</p>
</td>
<td valign="top" width="276">
<p>Replace Matches[0] (&quot;=Can%&quot;)              <br />with an expression which uses the               <br />two submatches &quot;=&quot; and &quot;can%&quot;. </p>
</td>
<td valign="top" width="239">
<p>= &#8216;Can%&#8217; </p>
</td>
</tr>
</tbody>
</table>
<p>Note that the expression which is being inserted uses $1 and $2 to mean matches[1] and[2] – if this is wrapped in double quote marks PowerShell will try to evaluate these terms before they get to the regex handler, so the replacement string must be wrapped in single quotes. But the desired replacement text <i>contains</i> single quote marks, so they need to be doubled up.</p>
<h3>Replace &#8216;=&#8217; with &#8216;like&#8217; for Wildcards</h3>
<p>So far, =Can* has become =&#8217;Can%&#8217;, which is good, but SQL needs &quot;LIKE&quot; instead of &quot;=&quot; to evaluate a wildcard. So the next operation converts &quot;CameraManufacturer = &#8216;Can%&#8217; &quot;into &quot;CameraManufacturer LIKE &#8216;Can%&#8217; &quot;:    <br /><code><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\s*=\s*(?='.+%'\s*$)&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot; LIKE &quot;</span></code> </p>
<p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="178">
<p>Regular expression</p>
</td>
<td valign="top" width="276">
<p>Meaning</p>
</td>
<td valign="top" width="239">
<p>Application </p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p><b><u>\s*=\s*</u></b>(?=&#8217;.+%&#8217;\s*$)</p>
</td>
<td valign="top" width="276">
<p>= sign surrounded by any spaces </p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer<b><u> = </u></b>&#8216;Can%&#8217;</p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*=\s*(?=<b><u>&#8216;</u></b>.+%&#8217;\s*$)</p>
</td>
<td valign="top" width="276">
<p>A quote character </p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer = <b><u>&#8216;</u></b>Can%&#8217;</p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*=\s*(?=&#8217;<b><u>.+</u></b>%&#8217;\s*$)</p>
</td>
<td valign="top" width="276">
<p>Any characters (at least one)</p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer = &#8216;<b><u>Can</u></b>%&#8217;</p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*=\s*(?=&#8217;.+<b><u>%&#8217;</u></b>\s*$)</p>
</td>
<td valign="top" width="276">
<p>% character followed by &#8216; </p>
</td>
<td valign="top" width="239">
<p>CameraManufacturer = &#8216;Can<b><u>%&#8217;</u></b></p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*=\s*(?=&#8217;.+%&#8217;<b><u>\s*$</u></b>)</p>
</td>
<td valign="top" width="276">
<p>Any spaces (or none)              <br />followed by end of line</p>
</td>
<td valign="top" width="239">
<p><i></i></p>
</td>
</tr>
<tr>
<td valign="top" width="178">
<p>\s*=\s*<b><u>(?=</u></b>&#8216;.+%&#8217;\s*$<b><u>)</u></b></p>
</td>
<td valign="top" width="276">
<p>Look ahead for the enclosed expression but don&#8217;t include it in the match</p>
</td>
<td valign="top" width="239">
<p>$Matches[0] = &quot;=&quot;              <br />(but only if &#8216;Can%&#8217; is present)</p>
</td>
</tr>
</tbody>
</table>
<h3>Provide Aliases</h3>
<p>The steps above reconstruct &quot;WHERE&quot; terms to build syntactically correct SQL, but what if I get confused and enter “<em>CameraMaker”</em> instead of “<em>CameraManufacturer”</em> or “<em>Keyword”</em> instead of “<em>Keyword<u>s</u></em>” ? I need <i>Aliases</i> – and they should work anywhere in the SQL statement &#8211; not just in the &quot;WHERE&quot; clause but in &quot;ORDER BY&quot; as well.     <br />I defined a hash table (a.k.a. a &quot;dictionary&quot;, or an &quot;associative array&quot;) near the top of the script to act as a single place to store the aliases with their associated full canonical names, like this:     <br /><code><span style="color:#ff4500;">$PropertyAliases</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">@{</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; Width&#160;&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.Image.HorizontalSize&quot;</span><span style="color:#000000;">;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; Height</span>&#160;&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.Image.VerticalSize&quot;</span><span style="color:#000000;">;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; Name</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.FileName&quot;</span><span style="color:#000000;">;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; Extension</span>&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.FileExtension&quot;</span><span style="color:#000000;">;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; Keyword&#160;&#160;&#160;&#160; </span><span style="color:#a9a9a9;">= </span><span style="color:#8b0000;">&quot;System.Keywords&quot;</span><span style="color:#000000;">;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; CameraMaker </span><span style="color:#a9a9a9;">= </span><span style="color:#8b0000;">&quot;System.Photo.CameraManufacturer&quot;</span>       <br /><span style="color:#000000;">}</span>       <br /></code>Later in the script, once the SQL statement is built, a loop runs through the aliases replacing each with its canonical name:     <br /><code><span style="color:#ff4500;">$PropertyAliases</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Keys</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">ForEach-Object</span> <span style="color:#000000;">{</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160; $SQL</span><span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;(?&lt;=\s)$($_)(?=\s*(=|&gt;|&lt;|,|Like))&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$PropertyAliases</span><span style="color:#a9a9a9;">[</span><span style="color:#ff4500;">$_</span><span style="color:#a9a9a9;">]</span>       <br /><span style="color:#000000;">}</span>       <br /></code>A hash table has .<i>Keys</i> and .<i>Values</i> properties which return what is on the left and right of the equals signs respectively. $hashTable.keyName or $hashtable[keyName] will return the value, so $_ will start by taking the value &quot;width&quot;, and its replacement will be $PropertyAliases[&quot;width&quot;] which is &quot;System.Image.HorizontalSize&quot;, on the next pass through the loop, &quot;height&quot; is replaced and so on. To ensure it matches on a field name and not text being searched for, the regular expression stipulates the name must be preceded by a space and followed by &quot;=&quot;or &quot;like&quot; and so on.<br />
<table border="1" cellspacing="0" cellpadding="0" width="899">
<tbody>
<tr>
<td valign="top" width="298">
<p><b>Regular expression</b></p>
</td>
<td valign="top" width="335">
<p><b>Meaning</b></p>
</td>
<td valign="top" width="264">
<p><b>Application </b></p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p>(?&lt;=\s)<b><u>Width</u></b>(?=\s*(=|&gt;|&lt;|,|Like))</p>
</td>
<td valign="top" width="335">
<p>The literal text &quot;Width&quot;</p>
</td>
<td valign="top" width="264">
<p><b><u>Width</u></b> &gt; 1024</p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p>(?&lt;=<b><u>\s</u></b>)Width(?=\s*(=|&gt;|&lt;|,|Like))</p>
</td>
<td valign="top" width="335">
<p>A Space</p>
</td>
<td valign="top" width="264">
<p><b><i></i></b></p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p><b><u>(?&lt;=</u></b>\s<b>)</b>Width(?=\s*(=|&gt;|&lt;|,|Like))</p>
</td>
<td valign="top" width="335">
<p>Look behind for the enclosed expression              <br />but don&#8217;t include it in the match</p>
</td>
<td valign="top" width="264">
<p>$Matches[0] = &quot;Width&quot;              <br />(but only if a leading space is present)</p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p>(?&lt;=\s)Width(?=<b><u>\s*</u></b>(=|&gt;|&lt;|,|Like))</p>
</td>
<td valign="top" width="335">
<p>any spaces (or none)</p>
</td>
<td valign="top" width="264">
<p><i></i></p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p>(?&lt;=\s)Width(?=\s*<b><u>(=|&gt;|&lt;|,|Like)</u></b>)</p>
</td>
<td valign="top" width="335">
<p>The literal text &quot;Like&quot;, or any of the characters comma, equals, greater than or less than</p>
</td>
<td valign="top" width="264">
<p>Width <b><u>&gt;</u></b> 1024</p>
</td>
</tr>
<tr>
<td valign="top" width="298">
<p>(?&lt;=\s)Width<b><u>(?=</u></b>\s*(=|&gt;|&lt;|,|Like)<b><u>)</u></b></p>
</td>
<td valign="top" width="335">
<p>Look ahead for the enclosed expression              <br />but don&#8217;t include it in the match</p>
</td>
<td valign="top" width="265">
<p>$Matches[0] = &quot;Width&quot;              <br />(but only if &quot; &gt;&quot; is present)</p>
</td>
</tr>
</tbody>
</table>
<h3>If the prefix is omitted put the correct one in.</h3>
<p>This builds on the ideas we&#8217;ve seen already. I want the list of fields and prefixes to be easy to maintain, so just after I define my aliases I define a list of field types    <br /><code><span style="color:#ff4500;">$FieldTypes</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Photo&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Image&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Music&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Media&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;RecordedTv&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Search&quot;</span></code>     <br />For each type I define two variables, a prefix and a fieldslist : the names must be FieldtypePREFIX and FieldTypeFIELDS – the reason for this will become clear shortly but here is what they look like     <br /><code><span style="color:#ff4500;">$SystemPrefix</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.&quot;</span>       <br /><span style="color:#ff4500;">$SystemFields</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;ItemName|ItemUrl&quot;</span>       <br /><span style="color:#ff4500;">$PhotoPrefix</span>&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.Photo.&quot;</span>       <br /><span style="color:#ff4500;">$PhotoFields</span>&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;cameramodel|cameramanufacturer|orientation&quot;</span>       <br /></code>In practice the field lists are much longer – system contains 25 fieldnames not just the two shown here. The lists are written with &quot;|&quot; between the names so they become a regular expression meaning<i> &quot;ItemName or ItemUrl Or …&quot;.</i> The following code runs after aliases have been processed     <br /><code><span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$type</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$FieldTypes</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>       <br /><span style="color:#ff4500;">&#160;&#160; $fields</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Fields&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span>       <br /><span style="color:#ff4500;">&#160;&#160; $prefix</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Prefix&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span>&#160; <br /><span style="color:#ff4500;">&#160;&#160; $sql</span>&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;(?&lt;=\s)(?=($Fields)\s*(=|&gt;|&lt;|,|Like))&quot;</span> <span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Prefix</span>       <br /><span style="color:#000000;">}</span>       <br /></code>I can save repeating code by using Get-Variable in a loop to get $<u>system</u>Fields, $<u>photo</u>Fields and so on, and if I want to add one more field, or a whole type I only need to change the variable declarations at the start of the script. The regular expression in the replace works like this:<br />
<table border="1" cellspacing="0" cellpadding="0" width="949">
<tbody>
<tr>
<td valign="top" width="258">
<p><b>Regular expression</b></p>
</td>
<td valign="top" width="325">
<p><b>Meaning</b></p>
</td>
<td valign="top" width="364">
<p><b>Application </b></p>
</td>
</tr>
<tr>
<td valign="top" width="259">
<p><b><u>(?&lt;=\s)</u></b>(?=(cameramanufacturer|               <br />orientation)\s*(=|&gt;|&lt;|,|Like))&quot;</p>
</td>
<td valign="top" width="325">
<p>Look behind for a space              <br />but don&#8217;t include it in the match</p>
</td>
<td valign="top" width="363">&#160;</td>
</tr>
<tr>
<td valign="top" width="260">
<p>(?&lt;=\s)(?=(<b><u>cameramanufacturer|                  <br />orientation</u></b>)\s*(=|&gt;|&lt;|,|Like))&quot;</p>
</td>
<td valign="top" width="325">
<p>The literal text &quot;orientation&quot; or &quot;cameramanufacturer&quot; </p>
</td>
<td valign="top" width="363">
<p><b><u>CameraManufacturer</u></b> LIKE &#8216;Can%&#8217;</p>
</td>
</tr>
<tr>
<td valign="top" width="260">
<p>(?&lt;=\s)(?=(cameramanufacturer|              <br />orientation)<b><u>\s*</u></b>(=|&gt;|&lt;|,|Like))&quot;</p>
</td>
<td valign="top" width="325">
<p>any spaces (or none)</p>
</td>
<td valign="top" width="363">&#160;</td>
</tr>
<tr>
<td valign="top" width="260">
<p>(?&lt;=\s)(?=(cameramanufacturer|              <br />orientation)\s*<b><u>(=|&gt;|&lt;|,|Like)</u></b>)&quot;</p>
</td>
<td valign="top" width="325">
<p>The literal text &quot;Like&quot;, or any of the characters comma, equals, greater than or less than</p>
</td>
<td valign="top" width="363">
<p>CameraManufacturer <b><u>LIKE</u></b> &#8216;Can%&#8217;</p>
</td>
</tr>
<tr>
<td valign="top" width="260">
<p>(?&lt;=\s)<b><u>(?=</u></b>(cameramanufacturer|               <br />orientation)\s*(=|&gt;|&lt;|,|Like)<b><u>)</u></b>&quot;</p>
</td>
<td valign="top" width="325">
<p>Look ahead for the enclosed expression              <br />but don&#8217;t include it in the match</p>
</td>
<td valign="top" width="363">
<p>$match[0] is the point between the leading space and &quot;CameraManufacturer LIKE&quot; but doesn&#8217;t include either.</p>
</td>
</tr>
</tbody>
</table>
<p><b>We get the effect of an &quot;insert&quot; operator by using </b>‑replace <b>with a regular expression that finds a place in the text but doesn&#8217;t select any of it.      <br /></b>This part of the function allows &quot;CameraManufacturer LIKE &#8216;Can%&#8217;&quot; to become &quot;System.Photo CameraManufacturer LIKE &#8216;Can%&#8217; &quot; in a WHERE clause.     <br />I also wanted &quot;CameraManufacturer&quot; in an ORDER BY clause to become &quot;System.Photo CameraManufacturer&quot;. Very sharp-eyed readers may have noticed that I look for a Comma after the fieldname as well as &lt;,&gt;,=, and LIKE. I modified the code which appeared in part one so that when an ORDER BY clause is inserted it is followed by a trailing comma like this:     <br /><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$orderby</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; ORDER BY &quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; , &quot;</span> <span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span> <span style="color:#8b0000;">&quot;,&quot;</span><span style="color:#000000;">}</span></code> </p>
<p>the new version will work with this regular expression but the extra comma will cause a SQL error and so it must be removed later.    <br />When I introduced the SQL I said the SELECT statement looks like this:</p>
<p> <code>SELECT System.ItemName, System.ItemUrl,&#160;&#160;&#160;&#160;&#160; System.FileExtension, System.FileName, System.FileAttributes, System.FileOwner,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160; System.ItemType, System.ItemTypeText , System.KindText,&#160;&#160;&#160;&#160; System.Kind,&#160;&#160;&#160;&#160; System.MIMEType,&#160;&#160;&#160;&#160;&#160;&#160; System.Size</code>
<p>Building this clause from the field lists simplifies code maintenance, and as a bonus anything declared in the field lists will be retrieved by the query as well as accepted as input by its short name. The SELECT clause is prepared like this:    <br /><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$First</span><span style="color:#000000;">)</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT TOP $First &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$type</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$FieldTypes</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span><span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Fields&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\|&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;, &quot;</span> <span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span> <span style="color:#8b0000;">&quot;, &quot;</span><span style="color:#000000;">}</span></code> </p>
<p>This replaces the &quot;|&quot; with a comma and puts a comma after each set of fields. This means there is a comma between the last field and the FROM – which allows the regular expression to recognise field names, but it will break the SQL , so it is removed after the prefixes have been inserted (just like the one for ORDER BY).    <br />This might <i>seem</i> inefficient, but when I checked the time it took to run the function and get the results but not output them it was typically about 0.05 seconds (50ms) on my laptop – it takes more time to output the results.     <br />Combining all the bits in this part with the bits in part one turns my 36 line function into about a 60 line one as follows</p>
<p><code><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">Get-IndexedItem</span><span style="color:#000000;">{</span>       <br /><span style="color:#00008b;">Param</span> <span style="color:#000000;">(</span> <span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Where&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Include&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">,</span>       <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Sort&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$OrderBy</span><span style="color:#a9a9a9;">,</span>       <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Top&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$First</span><span style="color:#a9a9a9;">,</span>       <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [String]</span><span style="color:#ff4500;">$Path</span><span style="color:#a9a9a9;">,</span>       <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Switch]</span><span style="color:#ff4500;">$Recurse</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160; )</span>       <br /><span style="color:#ff4500;">$PropertyAliases</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">@{</span><span style="color:#000000;">Width</span> <span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">&quot;System.Image.HorizontalSize&quot;</span><span style="color:#000000;">;</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Height</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.Image.VerticalSize&quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#ff4500;">$FieldTypes&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Photo&quot;</span>       <br /><span style="color:#ff4500;">$PhotoPrefix</span>&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.Photo.&quot;</span>       <br /><span style="color:#ff4500;">$PhotoFields</span>&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;cameramodel|cameramanufacturer|orientation&quot;</span>       <br /><span style="color:#ff4500;">$SystemPrefix</span>&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;System.&quot;</span>       <br /><span style="color:#ff4500;">$SystemFields</span>&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;ItemName|ItemUrl|FileExtension|FileName&quot;</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$First</span><span style="color:#000000;">)</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT TOP $First &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$type</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$FieldTypes</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span><span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Fields&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\|&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;, &quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">+</span><span style="color:#8b0000;">&quot;, &quot;</span> <span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;\\\\([^\\]+)\\.&quot;</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; FROM $($matches[1]).SYSTEMINDEX WHERE &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; FROM SYSTEMINDEX WHERE &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Filter</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\*&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;%&quot;</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-replace</span><span style="color:#8b0000;">&quot;\s*(=|&lt;|&gt;|like)\s*([^'\d][^\s']*)$&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">' $1 ''$2'' '</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\s*=\s*(?='.+%'\s*$)&quot;</span> <span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot; LIKE &quot;</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $Filter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">ForEach-Object</span> <span style="color:#000000;">{</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$_</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;'|=|&lt;|&gt;|like|contains|freetext&quot;</span><span style="color:#000000;">)</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$_</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; else</span> <span style="color:#000000;">{</span><span style="color:#8b0000;">&quot;Contains(*,'$_')&quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160; }</span><span style="color:#000000;">)</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160; $SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; AND &quot;</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; }</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span><span style="color:#000000;">)</span>       <br />&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;\w{4}:&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;file:&quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#ff4500;">$Path</span><span style="color:#000000;">}</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160; $Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\\&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;/&quot;</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;WHERE\s$&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; AND &quot;</span> <span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Recurse</span><span style="color:#000000;">)</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; SCOPE = '$Path' &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; DIRECTORY = '$Path' &quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;WHERE\s*$&quot;</span><span style="color:#000000;">)</span>       <br /></code><code><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span> <span style="color:#0000ff;">Write-warning</span> <span style="color:#8b0000;">&quot;You need to specify either a path , or a filter.&quot;</span> <span style="color:#000000;">;</span> <span style="color:#00008b;">return</span> <span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; ORDER BY &quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; , &quot;</span> <span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span> <span style="color:#8b0000;">&quot;,&quot;</span><span style="color:#000000;">}</span>       <br /><span style="color:#ff4500;">$PropertyAliases</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Keys</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">ForEach-Object</span>&#160; <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; {</span> <span style="color:#ff4500;">$SQL</span><span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-replace</span><span style="color:#8b0000;">&quot;(?&lt;=\s)$($_)(?=\s*(=|&gt;|&lt;|,|Like))&quot;</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$PropertyAliases</span><span style="color:#a9a9a9;">.</span><span style="color:#ff4500;">$_</span> <span style="color:#000000;">}</span>       <br /><span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$type</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$FieldTypes</span><span style="color:#000000;">)</span>       <br />&#160;&#160;&#160; </code><code><span style="color:#000000;">{</span><span style="color:#ff4500;">$fields</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Fields&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160; $prefix</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">get-variable</span> <span style="color:#8b0000;">&quot;$($type)Prefix&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">value</span>       <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160; $SQL</span>&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;(?&lt;=\s)(?=($Fields)\s*(=|&gt;|&lt;|,|Like))&quot;</span> <span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Prefix</span>       <br /><span style="color:#000000;">&#160;&#160;&#160; }</span>       <br /><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\s*,\s*FROM\s+&quot;</span> <span style="color:#a9a9a9;">,</span> <span style="color:#8b0000;">&quot; FROM &quot;</span>       <br /><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\s*,\s*$&quot;</span> <span style="color:#a9a9a9;">,</span> <span style="color:#8b0000;">&quot;&quot;</span>       <br /><span style="color:#ff4500;">$Provider</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">&quot;Provider=Search.CollatorDSO;&quot;</span><span style="color:#a9a9a9;">+</span> <span style="color:#8b0000;">&quot;Extended Properties=’Application=Windows’;&quot;</span>       <br /><span style="color:#ff4500;">$Adapter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.oledb.oleDBDataadapter</span> <span style="color:#000080;">-argument</span> <span style="color:#ff4500;">$SQL</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Provider</span>       <br /><span style="color:#ff4500;">$DS</span>&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.dataset</span>       <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Adapter</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Fill</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$DS</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$DS</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Tables</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span> <span style="color:#000000;">}</span>       <br /><span style="color:#000000;">}</span>       </p>
<p></code>In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/">part 3</a> I’ll finish the function by turning my attention to output </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1819&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Using the Windows index to search from PowerShell: Part one: Building a query from user input.</title>
		<link>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-one-building-a-query-from-user-input/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-one-building-a-query-from-user-input/#comments</comments>
		<pubDate>Sat, 30 Jun 2012 10:43:40 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1817</guid>
		<description><![CDATA[Note: this was originally written for the Hey,Scripting guy blog where it appeared as the 25 June 2012 episode. The code is available for download . I have some more index related posts coming up so I wanted to make sure everything was in one place I&#8217;ve spent some time developing and honing a PowerShell [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1817&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>Note: this was originally written for the </em><a href="http://blogs.technet.com/b/heyscriptingguy/archive/2012/06/25/build-a-query-to-search-the-windows-index-from-powershell.aspx"><em>Hey,Scripting guy blog</em></a><em> where it appeared as the 25 June 2012 episode. </em><a href="http://gallery.technet.microsoft.com/scriptcenter/Get-IndexedItem-PowerShell-5bca2dae"><em>The code is available for download</em></a><em> . I have some more index related posts coming up so I wanted to make sure everything was in one place</em><br />
<hr />I&#8217;ve spent some time developing and honing a PowerShell function that gets information from the Windows Index– the technology behind the search that is integrated into explorer in Windows 7 and Vista. The Index can be queried using SQL and my function builds the SQL query from user input, executes it and receives rows of data for all the matching items. In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-three-better-function-output/">Part three</a>, I&#8217;ll look at why rows of data aren&#8217;t the best thing for the function to return and what the alternatives might be. <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/">Part two</a> will look at making user input easier – I don&#8217;t want to make an understanding SQL a prerequisite for using the function. In this part I&#8217;m going to explore the query process. </p>
<p>We&#8217;ll look at how at how the query is built in a moment, for now please accept that a ready-to-run query stored in the variable $SQL. Then it only takes a few lines of PowerShell to prepare and run the query</p>
<p> <code><span style="color:#ff4500;">$Provider</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">&quot;Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;&quot;</span>     <br /><span style="color:#ff4500;">$adapter</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.oledb.oleDBDataadapter</span> <span style="color:#000080;">-argument</span> <span style="color:#ff4500;">$sql</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Provider</span>     <br /><span style="color:#ff4500;">$ds&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">system.data.dataset</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$adapter</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Fill</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$ds</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$ds</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Tables</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span> <span style="color:#000000;">}</span>     <br /></code>
<p>The data is fetched using oleDBDataAdapter and DataSet objects; the adapter is created specifying a &quot;provider&quot; which says <i>where</i> the data will come from and a SQL statement which says <i>what</i> is being requested. The query is run when the adapter is told to <i>fill </i>the dataset. The .fill() method returns a number, indicating how many data rows were returned by the query – if this is non-zero, my function returns the first table in the dataset. PowerShell sees each data row in the table as a separate object; and these objects have a property for each of the table&#8217;s columns, so a search might return something like this:</p>
<p> <code>
<p>SYSTEM.ITEMNAME : DIVE_1771+.JPG      <br />SYSTEM.ITEMURL : file:C:/Users/James/pictures/DIVE_1771+.JPG       <br />SYSTEM.FILEEXTENSION : .JPG       <br />SYSTEM.FILENAME : DIVE_1771+.JPG       <br />SYSTEM.FILEATTRIBUTES : 32       <br />SYSTEM.FILEOWNER : Inspiron\James       <br />SYSTEM.ITEMTYPE : .JPG       <br />SYSTEM.ITEMTYPETEXT : JPEG Image       <br />SYSTEM.KINDTEXT : Picture       <br />SYSTEM.KIND : {picture}       <br />SYSTEM.MIMETYPE : image/jpeg       <br />SYSTEM.SIZE : 971413</p>
<p> </code>
<p>There are <u>lots</u> of fields to choose from, so the list might be longer. The SQL query to produce it looks something like this. </p>
<p><code>SELECT System.ItemName, System.ItemUrl,&#160;&#160;&#160;&#160;&#160;&#160;&#160; System.FileExtension,      <br />&#160;&#160;&#160;&#160;&#160;&#160; System.FileName, System.FileAttributes, System.FileOwner,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160; System.ItemType, System.ItemTypeText ,&#160; System.KindText,&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160; System.Kind,&#160;&#160;&#160;&#160; System.MIMEType,&#160;&#160;&#160;&#160;&#160;&#160; System.Size       <br />FROM&#160;&#160; SYSTEMINDEX       <br />WHERE&#160; System.Keywords = 'portfolio' AND Contains(*,'stingray')</code> </p>
<p>In the finished version of the function, the SELECT clause has 60 or so fields; the FROM and WHERE clauses might be more complicated than in the example and an ORDER BY clause might be used to sort the data.    <br />The clauses are built using parameters which are declared in my function like this: </p>
<p> <code><span style="color:#00008b;">Param</span> <span style="color:#000000;">(</span> <span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Where&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Include&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">,</span>     <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Sort&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$orderby</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Top&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$First</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [String]</span><span style="color:#ff4500;">$Path</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Switch]</span><span style="color:#ff4500;">$Recurse</span>     <br /><span style="color:#000000;">)</span></code>
<p>In my functions I try to <b>use names already used in PowerShell</b>, so here I use -Filter and -First but I also define aliases for SQL terms like WHERE and TOP. These parameters build into the complete SQL statement, starting with the SELECT clause which uses -First</p>
<p> <code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$First</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT TOP $First &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">else&#160;&#160;&#160;&#160;&#160;&#160; </span>&#160;<span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; System.ItemName, System.ItemUrl &quot;</span> <span style="color:#006400;"># and the other 58 fields </span>    <br /></code>
<p>If the user specifies –First 1 then $SQL will be &quot;SELECT TOP 1 <em>fields</em>&quot;; otherwise it&#8217;s just &quot;SELECT <em>fields</em>&quot;. After the fields are added to $SQL, the function adds a FROM clause. Windows Search can interrogate remote computers, so if the -path parameter is a UNC name in the form \\computerName\shareName the SQL FROM clause becomes FROM computerName.SYSTEMINDEX otherwise it is FROM SYSTEMINDEX to search the local machine.     <br />A regular expression can recognise a UNC name and pick out the computer name, like this:</p>
<p> <code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;\\\\([^\\]+)\\.&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; FROM $($matches[1]).SYSTEMINDEX WHERE &quot;</span>     <br /><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">else</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; FROM SYSTEMINDEX WHERE &quot;</span><span style="color:#000000;">}</span>     <br /></code>
<p>The regular expression in the first line of the example breaks down like this<br />
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top" width="146">
<p>Regular expression</p>
</td>
<td valign="top" width="298">
<p>Meaning</p>
</td>
<td valign="top" width="249">
<p>Application </p>
</td>
</tr>
<tr>
<td valign="top" width="146">
<p><b><u>\\\\</u></b>([^\\]+)\\.</p>
</td>
<td valign="top" width="298">
<p>2 \ characters: &quot;\&quot; is the escape character, so each one needs to be written as \\</p>
</td>
<td valign="top" width="249">
<p><b><u>\\</u></b>computerName\shareName </p>
</td>
</tr>
<tr>
<td valign="top" width="146">
<p>\\\\(<b><u>[^\\]+</u></b>)\\.</p>
</td>
<td valign="top" width="298">
<p>Any non-\ character, repeated at least once </p>
</td>
<td valign="top" width="249">
<p>\\<b><u>computerName</u></b>\shareName </p>
</td>
</tr>
<tr>
<td valign="top" width="146">
<p>&quot;\\\\([^\\]+)<b><u>\\.</u></b>&quot;</p>
</td>
<td valign="top" width="298">
<p>A \,followed by <i>any</i> character </p>
</td>
<td valign="top" width="249">
<p>\\computerName<b><u>\s</u></b>hareName </p>
</td>
</tr>
<tr>
<td valign="top" width="146">
<p>&quot;\\\\<b><u>(</u></b>[^\\]+<b><u>)</u></b>\\.&quot;</p>
</td>
<td valign="top" width="298">
<p>Capture the section which is enclosed by the brackets as a match</p>
</td>
<td valign="top" width="249">
<p>$matches[0] =\\computerName\s              <br />$matches[1] =<b><u>computerName</u></b></p>
</td>
</tr>
</tbody>
</table>
<p>I allow the function to take different parts of the WHERE clause as a comma separated list, so that    <br /><code>-filter &quot;System.Keywords = 'portfolio'&quot;,&quot;Contains(*,'stingray')&quot; </code>    <br />is equivalent to     <br /><code>-filter &quot;System.Keywords = 'portfolio' AND Contains(*,'stingray')&quot;</code> </p>
<p>Adding the filter just needs this:</p>
<p> <code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Filter</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; AND &quot;</span><span style="color:#000000;">}</span></code>
<p>The folders searched can be restricted. A &quot;SCOPE&quot; term limits the query to a folder and all of its subfolders, and a &quot;DIRECTORY&quot; term limits it to the folder <u>without</u> subfolders. If the request is going to a remote server the index is smart enough to recognise a UNC path and return just the files which are accessible via that path. If a -Path parameter is specified, the function extends the WHERE clause, and the –Recurse switch determines whether to use SCOPE or DIRECTORY, like this: </p>
<p> <code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span><span style="color:#000000;">)</span><span style="color:#000000;">{</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;\w{4}:&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;file:&quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">resolve-path</span> <span style="color:#000080;">-path</span> <span style="color:#ff4500;">$Path</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">providerPath</span>     <br /><span style="color:#000000;">&#160;&#160;&#160;&#160; }</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;WHERE\s*$&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; AND &quot;</span> <span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Recurse</span><span style="color:#000000;">)</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; SCOPE = '$Path' &quot;</span> <span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160;&#160; else</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; DIRECTORY = '$Path' &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#000000;">}</span>     <br /></code>
<p>In these SQL statements, paths are specified in the form file:c:/users/james which isn&#8217;t how we normally write them (and the way I recognise UNC names won&#8217;t work if they are written as file://ComputerName/shareName). This is rectified by the first line inside the If ($Path) {} block, which checks for 4 &quot;word&quot; characters followed by a colon. Doing this will prevent &#8216;File:&#8217; being inserted if any protocol has been specified –the same search syntax works against HTTP:// (though not usually when searching on your workstation), MAPI:// (for Outlook items) and OneIndex14:// (for OneNote items). If a file path has been given I ensure it is an absolute one – the need to support UNC paths forces the use of .ProviderPath here. It turns out there is no need to convert \ characters in the path to /, provided the file: is included.    <br />After taking care of that, the operation -notmatch &quot;WHERE\s*$&quot; sees to it that an &quot;AND&quot; is added if there is anything other than spaces between WHERE and the end of the line (in other words if any conditions specified by –filter have been inserted). If neither -Path nor -filter was specified there will be a dangling WHERE at the end of the SQL statement .Initially I removed this with a ‑Replace but then I decided that I didn&#8217;t want the function to respond to a lack of input by returning the whole index so I changed it to write a warning and exit. With the WHERE clause completed, final clause in the SQL statement is ORDER BY, which &#8211; like WHERE – joins up a multi-part condition.</p>
<p> <code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;WHERE\s*$&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#0000ff;">&#160;&#160;&#160;&#160; Write-warning</span> <span style="color:#8b0000;">&quot;You need to specify either a path, or a filter.&quot;</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160;&#160; Return</span>     <br /><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$orderby</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; ORDER BY &quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; , &quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">}</span>     <br /></code>
<p>When the whole function is put together it takes 3 dozen lines of PowerShell to handle the parameters, build and run the query and return the result. Put together they look like this:</p>
<p> <code><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">Get-IndexedItem</span><span style="color:#000000;">{</span>     <br /><span style="color:#00008b;">Param</span> <span style="color:#000000;">(</span> <span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Where&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;Include&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">,</span>     <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Sort&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$OrderBy</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#a9a9a9;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [</span><span style="color:#00bfff;">Alias</span><span style="color:#000000;">(</span><span style="color:#8b0000;">&quot;Top&quot;</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$First</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [String]</span><span style="color:#ff4500;">$Path</span><span style="color:#a9a9a9;">,</span>     <br /><span style="color:#008080;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; [Switch]</span><span style="color:#ff4500;">$Recurse</span>     <br /><span style="color:#000000;">)</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$First</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT TOP $First &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">else</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;SELECT &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; System.ItemName, System.ItemUrl &quot;</span> <span style="color:#006400;"># and the other 58 fields</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;\\\\([^\\]+)\\.&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#ff4500;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; $SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot;FROM $($matches[1]).SYSTEMINDEX WHERE &quot;</span>     <br /><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">else</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; FROM SYSTEMINDEX WHERE &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Filter</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#ff4500;">$Filter</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; AND &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;\w{4}:&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">&quot;file:&quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#ff4500;">$Path</span><span style="color:#000000;">}</span>     <br /><span style="color:#ff4500;">&#160;&#160;&#160; $Path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Path</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">&quot;\\&quot;</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">&quot;/&quot;</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-notmatch</span> <span style="color:#8b0000;">&quot;WHERE\s*$&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; AND &quot;</span> <span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Recurse</span><span style="color:#000000;">)</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; SCOPE = '$Path' &quot;</span> <span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160; else</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#000000;">{</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; DIRECTORY = '$Path' &quot;</span><span style="color:#000000;">}</span>     <br /><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">&quot;WHERE\s*$&quot;</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>     <br /><span style="color:#0000ff;">&#160;&#160;&#160; Write-Warning</span> <span style="color:#8b0000;">&quot;You need to specify either a path or a filter.&quot;</span>     <br /><span style="color:#00008b;">&#160;&#160;&#160; Return</span>     <br /><span style="color:#000000;">}</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$SQL</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">&quot; ORDER BY &quot;</span> <span style="color:#a9a9a9;">+</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$OrderBy</span> <span style="color:#a9a9a9;">-join</span> <span style="color:#8b0000;">&quot; , &quot;</span> <span style="color:#000000;">)</span> <span style="color:#000000;">}</span>     <br /><span style="color:#ff4500;">$Provider </span><span style="color:#a9a9a9;">= </span><span style="color:#8b0000;">&quot;Provider=Search.CollatorDSO;Extended Properties=’Application=Windows’;&quot;</span>     <br /><span style="color:#ff4500;">$Adapter </span>&#160;<span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">system.data.oledb.oleDBDataadapter</span> <span style="color:#000080;">-argument</span> <span style="color:#ff4500;">$SQL</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$Provider</span>     <br /><span style="color:#ff4500;">$DS</span>&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">system.data.dataset</span>     <br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Adapter</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Fill</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$DS</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$DS</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Tables</span><span style="color:#a9a9a9;">[</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">]</span> <span style="color:#000000;">}</span>     <br /><span style="color:#000000;">}</span>     <br /></code>
<p>The -Path parameter is more user-friendly as a result of the way I handle it, but I&#8217;ve made it a general rule that <b>you shouldn&#8217;t expect the user to know too much of the underlying syntax</b> ,and at the moment the function requires too much knowledge of SQL: I don&#8217;t want to type </p>
<p> <code>Get-Indexed-Item –Filter &quot;Contains(*,'Stingray')&quot;, &quot;System.Photo.CameraManufacturer Like 'Can%'&quot; </code>
<p>and it seems unreasonable to expect anyone else to do so. I came up with this list of things the function should do for me. </p>
<ul>
<li>Don&#8217;t require the user to know whether a search term is prefixed with SYSTEM. SYSTEM.DOCUMENT, SYSTEM.IMAGE or SYSTEM.PHOTO. <u>If the prefix is omitted put the correct one in</u>. </li>
<li>Even without the prefixes some fieldnames are awkward for example &quot;HorizontalSize&quot; and &quot;VerticalSize&quot; instead of width and height. <u>Provide aliases</u> </li>
<li>Literal text in searches needs to be enclosed in single quotes, <u>insert quotes if the user omits them. </u></li>
<li>A free text search over all fields is written as Contains(*,&#8217;searchTerm&#8217;) , <u>convert &quot;orphan&quot; search terms into <i>contains</i> conditions.</u> </li>
<li>SQL uses % (not *) for a wild card – <u>replace* with % in filters</u> to cope with users putting the familiar *<u></u> </li>
<li>SQL requires the LIKE predicate(not =) for wildcards : <u>replace = with like for Wildcards</u> </li>
</ul>
<p>In <a href="https://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershellpart-two-helping-with-user-input/">Part two</a>, I&#8217;ll look at how I do those things. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1817&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/30/using-the-windows-index-to-search-from-powershell-part-one-building-a-query-from-user-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Lonesome George</title>
		<link>http://jamesone111.wordpress.com/2012/06/25/lonesome-george/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/25/lonesome-george/#comments</comments>
		<pubDate>Mon, 25 Jun 2012 15:15:17 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1815</guid>
		<description><![CDATA[At Easter I was in the Galapagos Islands; work had taken me to Ecuador diving in the Galapagos was too good an opportunity to miss. Mainland Ecuador was a country I knew little about and two weeks working in the capital (Quito, just South of the Equator, and 10,000 feet up in the Andes) doesn’t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1815&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>At Easter I was in the Galapagos Islands; work had taken me to Ecuador diving in the Galapagos was too good an opportunity to miss. Mainland Ecuador was a country I knew little about and two weeks working in the capital (Quito, just South of the Equator, and 10,000 feet up in the Andes) doesn’t qualify me as an expert. The client there was a good one to work with, and what I saw of the city (a bunch of Taxi rides and a bus tour on the one day we weren’t working) mean I’d go back if asked. Travel wasn’t good and the return flights so bad that I’ve vowed never to fly with Iberia again. Flying to the islands the plane had a problem which meant if it landed it couldn’t take off again so having got within sight of the islands we had to go all the way back to Quito and get another plane. Down at sea level the heat was ferocious, the transportation scary and the insect bites the worst I’ve had. But the diving… different kinds of Sharks (including a close encounter with a group of Hammerheads), Seal Lions, Turtles, Rays (including a Manta encounter on the very first dive which set the tone) – I’d put up with a lot for that. And if some search engine has steered you here, I dived with Scuba Iguana, and if I manage to go back I’ll dive with them again. </p>
<p>The Scuba place was pretty much next door to the Darwin station: home of giant tortoises and a tortoise breading programme. Galapagos comes from the Spanish word for saddle because the shape of the giant tortoise’s shell looked like a traditional saddle. I also learnt that some languages – including Spanish – don’t have distinct words for Tortoise and (Marine) Turtles.&#160; The sex of tortoises is determined by the temperature at which the eggs incubate and the breeding programme gathers eggs, incubates them to get extra females, and looks after the baby tortoises keeping them safe human introduced species (like rats) which feed on eggs and baby tortoises. Each island’s tortoises are different so the eggs and hatchlings are marked up so they go back to right island. But there is no breeding programme for Pinta island (also named Abingdon island by the Royal Navy. According to a story told by Stephen Fry on QI, sailors ate giant tortoises and found them very good.)&#160; A giant Tortoise was found on Pinta; but a search over several years failed to find a second. So he – Lonesome George, was brought to the Darwin station in the 1970s. No one knows for sure how he was then. All efforts to find a mate for him failed: so George lived out his final decades as the only known example of&#160; <em>Geochelone nigra abingdoni&#160; </em>the Pinta Galapagos tortoise.     <br />On Easter Sunday I walked up see George and the giants from other islands who live at the station. George was keeping out of the sun; he shared an enclosure and I wondered what he made of the other species – if somewhere in that ancient reptile brain lurked a memory of others just like him, a template into which the other tortoises didn’t quite fi. </p>
<p>Later in trip I was asked to help with some work on survey being prepared about Hammerhead sharks. I was told they estimated as having a 20% chance of becoming extinct in the next 100 years. This statistic is quite hard to digest: my chances of being extinct in 100 years are close to 100%: so my contribution to the survey was suggest that telling people if things continue as they are the chances of seeing Hammerheads on a dive in 5 years will be X amount less than today. It’s not fair, but we care more about some species than others and I hope there will still be Hammerheads for my children to see in a few years. Sadly they won’t get the chance to see the Pinta Tortoise.</p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1815&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/25/lonesome-george/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Windows Phone 8. Some Known Knowns, and Known Unknowns</title>
		<link>http://jamesone111.wordpress.com/2012/06/22/windows-phone-8-some-known-knowns-and-known-unknowns/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/22/windows-phone-8-some-known-knowns-and-known-unknowns/#comments</comments>
		<pubDate>Fri, 22 Jun 2012 10:23:24 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1812</guid>
		<description><![CDATA[Earlier this week Microsoft held its Windows Phone Summit where it made a set of announcements about the next generation of Windows Phone – Windows phone 8 in summary these were Hardware Support for Multi-core processors , Additional Screen resolutions, Removable memory cards, NFC Software The Windows 8 core is now the base OS, Support [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1812&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Earlier this week Microsoft held its <a href="http://channel9.msdn.com/Events/Windows-Phone/Summit">Windows Phone Summit</a> where it made a set of announcements about the next generation of Windows Phone – Windows phone 8 in summary these were</p>
<ul>
<li><strong>Hardware</strong> Support for Multi-core processors , Additional Screen resolutions, Removable memory cards, NFC </li>
<li><strong>Software</strong> The Windows 8 core is now the base OS, Support for native code written in C or C++ (meaning better games), IE 10, new mapping (from Nokia), a speech API which apps can use. Better business-oriented features, VOIP support, a new “Wallet” experience and in-app payments, and a new start screen. </li>
</ul>
<p>This summit came hot on the heels of the <a href="https://jamesone111.wordpress.com/2012/06/19/microsofts-new-surface-all-it-needs-is-love/"><em>Surface </em>tablet launch</a>, which seemed to be a decision by Microsoft that making hardware for Windows 8 was too important to be left to the hardware makers. The first thing I noted about phone announcement was the lack of a Microsoft branded phone. I’ve said that Microsoft should make phones itself since before the first Windows Mobile devices appeared – when Microsoft was talking about “Stinger” internally; I never really bought any of the reasons given for not doing so. But I’d be astounded if Nokia didn’t demand that Microsoft promise not to make a phone (whether there’s a binding agreement or just an understanding between Messrs Elop and Ballmer we don’t know). Besides Nokia Microsoft has 3 other device makers on-board: HTC have devices for every Microsoft mobile OS since 2000, but also have a slew of devices for Android, Samsung were a launch partner for Phone 7 but since then have done more with Android ; LG were in the line up for the Windows Phone 7 launch and are replaced by Huawei.&#160; What these 3&#160; feel about Nokia mapping technology is a matter of guesswork but depends on branding and licensing terms. </p>
<p>There are some things we think we know, but actually they are things we know that we don’t know. </p>
<ul>
<li><strong>Existing 7.x phones will not run Windows Phone 8 but will get an upgrade to Windows 7.8.</strong> I have an HTC Trophy which I bought in November 2010 and it has gone from 7.0 to 7.5 and I’ll be quite happy to get 7.8. on a 2 year old phone. Someone who just bought a Nokia Lumia might not feel quite so pleased.&#160; <strong>What will be in 7.8 ?</strong> The new start screen has been shown. But will it have IE10 ? Will it have the new mapping and Speech capabilities. The Wallet, In-app-payments ?&#160; This matters because…. </li>
<li><strong>Programs specifically targeting Windows Phone 8 won’t run on 7.</strong> Well doh! Programs targeting Windows 7 don’t run XP. But what programs will need to target the new OS ? Phone 7 programs are .NET programs and since .NET compiles to an intermediate language not to CPU instructions, a program which runs on Windows 8-RT (previous called Windows on ARM) <strong><em>should </em></strong>go straight onto a Windows 8-intel machine (but not vice versa), and <strong>Phone 7 programs will run on Phone 8.</strong> An intriguing comment at the launch says the Phone 8 emulator runs on Hyper-V; unless Hyper-V has started translating between different CPU instruction sets this means the Emulated phone has an Intel CPU but it doesn’t matter because it is running .NET intermediate language not binary machine code.<strong> </strong>So <strong>how many new programs will be 8-specific</strong> ? – Say Skype uses the VOIP support and in-app payments for calling credit. Will users with old phones be stuck with the current version of Skype? Do they get a new version where those features don’t light up. Or do they get the same experience as someone with a new phone. If the only things which are Phone 8 specific are apps which need Multiple cores (or other newly supported hardware) there would never have been any benefit upgrading a single core phone from 7 to 8.&#160;&#160; </li>
<li><strong>Business support. </strong>This covers many possibilities, but what is in and what is out ? Will the encryption send keys back to base as bit-locker does ? Will there be support for Direct-Access ? Corporate wireless security ? Will adding Exchange server details auto-configure the phone to corporate settings (like the corporate app-store) . Will it be possible to block updates ? This list goes on and on. </li>
</ul>
<p>It’s going to interesting to see what this does for Microsoft and for Nokia’s turn-round. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1812&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/22/windows-phone-8-some-known-knowns-and-known-unknowns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Microsoft&#8217;s new surface. All it needs is love.</title>
		<link>http://jamesone111.wordpress.com/2012/06/19/microsofts-new-surface-all-it-needs-is-love/</link>
		<comments>http://jamesone111.wordpress.com/2012/06/19/microsofts-new-surface-all-it-needs-is-love/#comments</comments>
		<pubDate>Tue, 19 Jun 2012 16:44:07 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[General musings]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1807</guid>
		<description><![CDATA[People who buy Apple stuff worry me. Their attachment to one brand of equipment is somewhere between addiction and religious fervour. It is Love. For over 20 years I’ve used Microsoft stuff, because it is simply a better way to get a job done. Some of the people in my office use Macs and can’t [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1807&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>People who buy Apple stuff worry me. Their attachment to one brand of equipment is somewhere between addiction and religious fervour. It is <strong>Love. </strong></p>
<p>For over 20 years I’ve used Microsoft stuff, because it is simply a better way to get a job done. Some of the people in my office use Macs and can’t do their job without creating a virtual PC running Windows. It doesn’t reduce their love for their Macs. As the saying goes: <b>The opposite of love is not hate, it&#8217;s indifference. </b>And its hard to feel anything but indifferent to the products of any of the major PC makers. The Dell in front of me it is well made, well specified and does everything I ask and more. But Love it ? Most PC users will tell you <a href="http://search.dilbert.com/search?w=21039">loving a computer is crazy</a> (which is why Apple folk are so disturbing).&#160; </p>
<p><img style="display:inline;float:left;" title="surface_01" alt="" align="left" src="https://mountainss.files.wordpress.com/2012/06/surface_01.jpg?w=267&#038;h=253&#038;h=150" width="267" height="150" /></p>
<p>I think Microsoft’s new surface tablets are trying to create a Windows machine which people – if they can’t actually Love – feel more than indifferent about.&#160; Surface is two Machines: one uses an ARM processor and runs the new Windows RT, so won’t run all your existing software. The other uses an Intel chip – and is thicker and heavier to give it 1/3 more battery life (roughly 130cc and 225g more to get 40 Watt Hours instead of 30.), but that means it should work with existing software and USB devices. I can plug in a mouse and keyboard, and attach two monitors via display port and have a system just like the one I have today. Unplug it and I can use it iPad style or take the “touch Cover” keyboard * and write documents or use the Pen to annotate documents if that’s what I choose. The ARM version has office built in, but no pen and a different video connector (so probably only 1 screen). Even with its smaller battery it will probably run for longer (though like the shipment date and price, battery life is yet to be confirmed). </p>
<p><a href="http://news.cnet.com/8301-10805_3-57455682-75/microsoft-breaks-tradition-with-microsoft-surface-tablets/?tag=mncol;topStories">Mary-Jo Foley wrote</a> of the launch of Surface <em>“It&#8217;s the end of an era. Or maybe the start of a new one.” </em>Indeed. Microsoft began by providing OEMs (including Apple) with BASIC, then with Operating Systems. OEMs where very much the Geese that laid golden eggs for Microsoft – during the 10 years I worked at there (not in the OEM part of the business) there were times when I felt that the company forgot the problem with Geese is they produce a lot of … <a href="http://en.wikipedia.org/wiki/Guano">guano</a>. The OEMs have been poor on design and innovation for a long time: <a href="http://www.businessweek.com/articles/2012-06-19/why-microsofts-surface-tablet-should-shame-the-pc-industry">Bloomberg business week no-less</a> talks about how the PC industry should be shamed by Surface, and talks about recent years of PC Development as “the great stagnation”. The Bloomberg piece puts some blame on Microsoft and Intel for taking too much from the OEMs, I doubt that if the chips and OS had cost less the difference would have gone into innovations that add value. That lack of added value means margins on PCs aren’t great and that’s led manufacturers to take money to install all manner of junk on the machines they ship. The whole DoJ saga – which grew out of Microsoft trying to prevent OEMs installing software it didn’t like – left a situation where the company was required to sell Windows to anyone who wanted it and could not do anything to prevent an explosion of crapware. Lots of people are asking <strong>WHY has Microsoft chosen to get into making computers? </strong>There answer is either (a) It can make much more profit by selling computers and operating systems together. or (b) it has an idea of what a PC should be in the second decade of the 21st century and it <strong>doesn’t trust PC makers to deliver</strong> that by themselves. </p>
<p>If I were fielding calls from angry OEMs upset Microsoft arriving in their market I’d make the case that no OEM would have made a product like this: their lack of a similar product both lost them right to complain and forced Microsoft to do something: if they do have something , Microsoft is saying they won’t undercut on price, something we won’t know for sure until the units go on sale. Some people wonder if Microsoft will aim to make the same from selling the hardware as anyone else and make the price of Windows on top of that; or if they will think $x of margin per unit sold is the same whether they sell a computer/OS combination or they sell a licence to an OEM. The latter would make it very hard for OEMs to compete; but Microsoft trying to make <em>desirable</em> hardware profitably ? That’s a lot less of a threat to OEMs. Apple doesn’t sell as many units as Samsung, but the profit to Apple <u>per unit</u> is more than the retail price of the Samsung. When the iPhone was launched I questioned whether there was sufficient market for a phone at that price point: it has actually sold more units than Apple envisaged at the start: which proves one thing –<strong> people will pay handsomely for something they love.</strong>&#160; </p>
<p>&#160;</p>
<p><a href="http://jamesone111.files.wordpress.com/2012/06/image.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;float:left;border-top:0;border-right:0;padding-top:0;" title="image" border="0" alt="image" align="left" src="http://jamesone111.files.wordpress.com/2012/06/image_thumb.png?w=124&#038;h=93" width="124" height="93" /></a>* There are two keyboards. The thin pressure sensitive&#160; “Touch cover” and a thicker moving key “Type cover” You can see the difference in this picture&#160; (click for a bigger version)</p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/general-musings/'>General musings</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1807&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/06/19/microsofts-new-surface-all-it-needs-is-love/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="https://mountainss.files.wordpress.com/2012/06/surface_01.jpg?w=450&#38;h=253" medium="image">
			<media:title type="html">surface_01</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/06/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Lies, damn lies and public sector pensions</title>
		<link>http://jamesone111.wordpress.com/2012/05/11/lies-damn-lies-and-public-sector-pensions/</link>
		<comments>http://jamesone111.wordpress.com/2012/05/11/lies-damn-lies-and-public-sector-pensions/#comments</comments>
		<pubDate>Fri, 11 May 2012 09:08:22 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1804</guid>
		<description><![CDATA[Every time I hear that “public sector workers”&#160; are protesting about government plans for their pensions –as happened yesterday &#8211; I think of two points I made to fellow school governors when the teachers took a day off (with the knock on cost to parents and the economy) these were Does&#160; classroom teachers understand their [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1804&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Every time I hear that “public sector workers”&#160; are protesting about government plans for their pensions –as happened yesterday &#8211; I think of two points I made to fellow school governors when the teachers took a day off (with the knock on cost to parents and the economy) these were </p>
<ul>
<li>Does&#160; classroom teachers understand their union wants them to subsidize the head’s pension from theirs ?&#160; (Have the Unions explained to the classroom teachers that <strong>is</strong> what they are doing</li>
<li>Any teacher who is part of this protest has demonstrated they have insufficient grasp of maths to teach the subject (except, perhaps to the reception class.)</li>
</ul>
<p>This second point is the easier of the two to explain: <u>the pension paid for over your working life</u> is a function of: </p>
<ul>
<li>The salary you earned (which in turn depends on the rate at which your salary grew) </li>
<li>What fraction of your salary was paid in to your pension fund. It might be you gave up X% or your employer put in Y% that you never saw or a combination.&#160;&#160;&#160; </li>
<li>How many years you paid in for (when you started paying in, and when you retire) </li>
<li>How well your pension fund grew before it paid out </li>
<li>How long the pension is expected to pay out for (how long you live after retirement) </li>
<li>Annuity rates – the interest earned on the pension fund as it pays out. </li>
</ul>
<p>In addition, some people receive some pension which wasn’t paid for in this way; some employers (public or private sector) make guarantees to top up either the pension fund so it will buy a given level of annuity or to top-up&#160; pension payments bought with the fund. The total you receive is the combination of what you have paid for directly and the top-up.    <br />Change any factor – for example how long you expect to live – and either what you have paid for changes or the other factors have to change to compensate. Since earnings and rates of return aren’t something we control, living longer means either we have pay for a smaller pension, or we must pay in more, or retire later or some combination of all three. Demanding that the same amount will come out of a pension for longer, without paying more in is a demand for a guaranteed top-up in future – in the case of public sector employees that future top-up comes from future taxes, for private sector it comes from future profits. </p>
<p>It’s easy for those in Government to make pension promises because those promises don’t need to be met for 30 years or more. Teachers whose retirement is imminent would have come into the profession when Wilson, Heath or Callaghan was in Downing Street, and all 3 are dead and buried; so with the exception of Denis Healey are all the chancellors who set budgets while they were in office. It’s temping for governments to save on spending by under-contributing to pensions today, and leave future governments with the shortfall: taken to the extreme governments can turn pensions into a <a href="http://en.wikipedia.org/wiki/Ponzi_scheme">Ponzi scheme</a> – this year’s “Teachers’/Police/whatever pay” bill covers what all current teachers/police officers / whoever are paid for doing the job and all pensions paid to retired ones <u>for doing the job cheaply in the past</u>. Since I am, more-or-less, accusing governments of all colours of committing <strong>fraud</strong>, I might as well add <strong>false accounting</strong> to the charge sheet. Let’s say the Government wants to buy a squadron of new aircraft but doesn’t want to raise taxes to pay for them all this year; it borrows money and the future liability that creates is accounted for. If the deal it makes with public sector workers is for a given amount to spend today, and a promise of a given standard of living in retirement ,does it record that promise – that future liability &#8211; as part of pay today? Take a wild guess.     <br />This wouldn’t matter – outside accounting circles – if everything was constant. But the length of time between retirement and death has increased and keeps on increasing. For the sake of a simple example: lets assume someone born in 1920, joined the Police after world war II , served for 30 years and retired in 1975 at age 55 expecting to die at 70. Their retirement was half their length of service. Now consider someone born in 1955, who also joined the police at age 25, served for 30 years and retired 2010. Is any one making plans for their Pension to stop in 2025 ? We might reasonably expect this person to live well into their eighties – so we’ve moved from 1 retired officer for every 2 serving, to a 1:1 ratio. I’m not saying that in 1975 the ratio <u>was</u> 1:2 and in 2012 it <u>is</u> 1:1 but that’s the direction of travel.&#160; </p>
<p> I’ve yet to hear a single teacher say their protests about pensions amount to a demand that they should under-fund their retirement as a matter of public policy and their pupils – who will then be tax payers – should make up difference. As one of those whose work generates the funds to pay for the public sector <strong>I must choose a combination of lower pension, later retirement, and higher contributions than I was led to expect when I started work 25 years or so ago</strong>. And there are people demanding my taxes insulate them from having to do the same; or (if you prefer) demanding a pay rise to cover the gap between what past governments have promised them and what they are actually paying for, or (and this becomes a bit uncomfortable) that government starts telling <em>us </em>what it <em>really</em> costs to have the teachers, nurses, police officers and so on we want.&#160; </p>
<p>But what of my claim that <strong>Unions get low paid staff to subsidize the pensions of higher paid colleagues</strong>. Lets take two teachers; I’ll call them Alice and Bob, and since this is a simplified example they’ll fit two stereotypes: Alice sticks to working in the class room; and gets a 2% cost of living rise every year. Bob competes for every possible promotion, and gets promoted in alternate years, so he gets a 2% cost of living rise alternating with a 10% rise. Although they both started on the same salary after 9 end-of-year rises, Alice’s pay has gone up by 19.5% and Bob – who must be a head by now – has seen his rise by 74%&#160;&#160; <br />Throughout the 10 years they pay 10% of their salary into their pension fund – to make the sums easy we’ll assume they pay the whole 10% on the last day of the year, and each year their pension fund manager earns them 10% of what was in their pension pot at the end of the previous year. After 10 years Alice has £17,184 in her pension pot, and Bob has £20,390 in his.</p>
<p>Alice (and her fellow class room teachers) are told by the Union Rep that <strong>any attempt to change from final salary as the calculation mechanism is an attack on your pension,</strong> for her,<strong> this is factually wrong.</strong> If you are ever told this you need to ask if you are a high flier like Bob or if your career is more like Alice’s. To see why it is wrong (and lets put it down to the Union rep being innumerate , rather than dishonest), lets pretend the pension scheme only has Alice and Bob in it. So the total pot is £37,574 – Alice put in 46% of that money, but of it is shared in the ratio of the final salaries 11,950 : 17,432 ,Alice gets 41% of the pay out.&#160; <br />You can argue it doesn’t work like that because Alice’s pot (at 1.44 times her final salary) might <em>just</em> cover the percentage of her final salary she has been promised: Bob’s pension pot is only 1.17 times his final salary which will give him a smaller percentage so the government steps in and boosts his pot to be 1.44 time <em>his</em>&#160; final salary just like Alice’s. So Bob gets a golden handshake of nearly £4700 and Alice gets nothing.     <br />Suppose 1.44 years is nowhere near enough and Alice and Bob need 3 years salary to buy a large enough annuity; the government needs to find £18,668 for Alice (108% of her pot), and 31,907 for Bob (156% of his pot). Whichever way you cut and slice if your salary grows quicker than your colleagues you will do better out of final salary than they do. If it grows more slowly you will fare worse.&#160;&#160; </p>
<table style="line-height:normal;border-collapse:collapse;" border="0" cellspacing="0" cellpadding="0" width="1065">
<col style="width:125pt;" width="167" />
<col style="width:61pt;" width="81" />
<col style="width:44pt;" width="59" />
<col style="width:87pt;" width="116" />
<col style="width:60pt;" width="80" />
<col style="width:53pt;" width="71" />
<col style="width:61pt;" width="81" />
<col style="width:44pt;" width="59" />
<col style="width:87pt;" width="116" />
<col style="width:61pt;" width="82" />
<col style="width:27pt;" width="36" />
<tbody>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" height="23" width="187"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" width="378" colspan="4" align="center"><font face="Calibri"><font><strong>Alice</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" width="79"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" width="420" colspan="5" align="center"><font face="Calibri"><font><strong>Bob</strong></font></font></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" height="23"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Salary</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Increase</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Pension Payment</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Pension Pot</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Salary</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Increase</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Pension Payment</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl72" align="center"><font face="Calibri"><font><strong>Pension Pot</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 1</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>1,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>10%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>1,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 2</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,200.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,020.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>2,120.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,000.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,100.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>2,200.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 3</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,404.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,040.40 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>3,372.40 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,220.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>10%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,122.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>3,542.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 4</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,612.08 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,061.21 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>4,770.85 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>12,342.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,234.20 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>5,130.40 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 5</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>10,824.32 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,082.43 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>6,330.36 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>12,588.84 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>10%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,258.88 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>6,902.32 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 6</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,040.81 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,104.08 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>8,067.48 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>13,847.72 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,384.77 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160; </font></span><font>8,977.33 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 7</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,261.62 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,126.16 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>10,000.39 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>14,124.68 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>10%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,412.47 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>11,287.53 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 8</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,486.86 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,148.69 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>12,149.12 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>15,537.15 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,553.71 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>13,970.00 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 9</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>11,716.59 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>2%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,171.66 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>14,535.69 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl66"><font face="Calibri"><span><font>&#160; </font></span><font>15,847.89 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font>10%</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,584.79 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>16,951.79 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Year 10</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl68"><font face="Calibri"><strong><span><font>&#160; </font></span><font>11,950.93 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,195.09 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"><font face="Calibri"><strong><span><font>&#160;</font></span><font>17,184.35 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl68"><font face="Calibri"><strong><span><font>&#160; </font></span><font>17,432.68 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65" align="right"><font face="Calibri"><font></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>1,743.27 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"><font face="Calibri"><strong><span><font>&#160; </font></span><font>20,390.23 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl70"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Average Salary</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl68"><font face="Calibri"><strong><span><font>&#160; </font></span><font>10,949.72 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl68"><font face="Calibri"><strong><span><font>&#160; </font></span><font>13,394.10 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl65"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl70"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Combined Final Salary</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"><font face="Calibri"><strong><span><font>&#160; </font></span><font>29,383.60 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl71" align="right"><font face="Calibri"><font>Total Pot</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl69"><font face="Calibri"><strong><span><font>&#160;</font></span><font>37,574.58 </font></strong></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Alice&#8217;s share</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>15,282.37 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Bob&#8217;s Share</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>22,292.21 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Combined Average Salary</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>24,343.82 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl71" align="right"><font face="Calibri"><font>Total Pot</font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>37,574.58 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Alice&#8217;s share</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>16,900.85 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl73" height="23" align="right"><font face="Calibri"><font><strong>Bob&#8217;s Share</strong></font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" class="xl67"><font face="Calibri"><span><font>&#160; </font></span><font>20,673.73 </font></font></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
<tr style="height:15.35pt;">
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;" height="23"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
<td style="line-height:normal;padding-left:1px;padding-right:1px;vertical-align:bottom;padding-top:1px;"></td>
</tr>
</tbody>
</table>
<p>What if the mechanism for calculating were Average salary , not final salary? It doesn’t quite remove gap but gets very close. Instead of £2,000 of Alice’s money going to Bob it’s less than £300.&#160; <br />A better way to look at this is to say if the amount of money in the combined Pension pot pays £5000 a year in Pensions, do we split it as roughly £2000 to Alice and £3000 to Bob (the rough ratio of their final salaries – each gets about 1/6th of their final salary) or £2250 to Alice and £2750 to Bob (the ratio of their average salaries and each gets about 1/5th of their average).     <br />Whenever average salary is suggested as a basis, union leaders will say that pensions are calculated from a smaller number as if it reduces the amount paid. If the government wanted to take money that way it would be simpler to say “a full pension will in future be a smaller percentage of final salary”. <strong>Changing to average-based implies an increase in the percentage paid.</strong>&#160;</p>
<p>That perhaps is the final irony. Rank and file Police officers – whose career pay is like Alice’s in the example &#8211; marched through London yesterday demanding that their pensions be left alone; you do not need to spend long reading “<a href="http://inspectorgadget.wordpress.com/2012/05/10/a-sea-of-berghaus-and-north-face/">Inspector Gadget</a>” to realise when you remove the problems created for the Police by politicians most of the problems that are left are created by senior officers whose career pay follows the “Bob” path. Yet the “many” marching were demanding that they continue to subsidize these “few”. As Gadget himself likes to say : you couldn’t make it up.    </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1804&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/05/11/lies-damn-lies-and-public-sector-pensions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Don&#8217;t Swallow the cat. Doing the right thing with software development and other engineering projects.</title>
		<link>http://jamesone111.wordpress.com/2012/04/22/dont-swallow-the-cat-doing-the-right-thing-with-software-development-and-other-engineering-projects/</link>
		<comments>http://jamesone111.wordpress.com/2012/04/22/dont-swallow-the-cat-doing-the-right-thing-with-software-development-and-other-engineering-projects/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 20:30:26 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1802</guid>
		<description><![CDATA[In my time at Microsoft I became aware of the saying “communication occurs only between equals.” usually couched in the terms “People would rather lie than deliver bad news to Steve Ballmer”. Replacing unwelcome truths with agreeable dishonesty wasn’t confined to the CEOs direct reports, and certainly isn’t a disease confined to Microsoft. I came [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1802&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In my time at Microsoft I became aware of the saying “<strong>communication occurs only between equals</strong>.” usually couched in the terms “People would rather lie than deliver bad news to Steve Ballmer”. Replacing unwelcome truths with agreeable dishonesty wasn’t confined to the CEOs direct reports, and certainly isn’t a disease confined to Microsoft. I came across ‘The Hierarchy of Power Semantics’ more than 30 years ago when I didn’t understand what was meant by the title; it was written in the 1960s and if you don’t recognise “In the &quot;beginning was the plan and the specification, and the plan was without form and the specification was void and there was darkness on the face of the implementation team”&#160; see <a href="http://www.wussu.com/humour/semantic.htm">here</a> – language warning for the easily offended.     <br /><a href="http://en.wikipedia.org/wiki/Celine%27s_laws">Wikipedia</a> says the original form of “communication occurs only between equals”&#160; is <b>Accurate communication is possible only in a non-punishing situation. </b>There are those who (consciously or not) use the impossibly of saying “No” to extract more from staff and suppliers; it can produce extraordinary results, but sooner or later it goes horribly wrong. For example the Challenger disaster was caused by the failure of an ‘O’ ring in solid rocket booster made by Morton Thiokol. <a href="http://en.wikipedia.org/wiki/Roger_Boisjoly#Challenger_disaster">The engineers responsible for the booster were quite clear</a> that in cold weather the ‘O’ rings were likely to fail with catastrophic results.&#160; NASA asked if a launch was OK after a freezing night and fearing the consequences of saying “No” managers at Morton Thiokol over-ruled the engineers and allowed the disastrous launch to go ahead.&#160; Most people can think of some case where someone made an impossible promise to a customer, because they were afraid to say no. </p>
<p>Several times recently I have heard people say something to the effect that ‘We’re so committed to doing this the wrong way that we can’t change to the right way.”&#160; Once the person saying it was me, which was the genesis of this post. Sometimes, in a software project because saying to someone – even to ourselves – “We’re doing this wrong” is difficult, so we create work rounds. The the odd title of this post comes from a song which was played on the radio a lot when I was a kid. </p>
<blockquote><p><em>There was an old lady, who swallowed a fly, I don’t know why she swallowed a fly. I guess she’ll die.        <br />There was an old lady, who swallowed a spider that wriggled and jiggled and ticked inside her. She Swallowed the spider to catch the fly&#160; … I guess she’ll die         <br />There was an old lady, who swallowed a bird. How absurd to swallow a bird. She swallowed the bird to catch the spider … I guess she’ll die         <br />There was an old lady, who swallowed a cat. Fancy that to swallow a cat. She swallowed the cat to catch the bird …&#160; I guess she’ll die         <br />There was an old lady, who swallowed a dog. What a hog to swallow a dog. She swallowed the dog to catch the cat … I guess she’ll die         <br />There was an old lady, who swallowed a horse. She’s dead, of course</em></p>
</blockquote>
<p>In other words each cure needs a further, more extreme cure.&#160; In my case the “fly” was a simple problem I’d inherited. It would take a couple of pages to explain the context, so for simplicity it concerns database tables and the “spider” was to store data de-normalized. If you don’t spend your days working with databases, imagine you have a list of suppliers, and a list of invoices from those suppliers. Normally you would store an ID for the supplier in the invoice table, and look up the name from the supplier table using the ID. For what I was doing it was better to put the supplier name in the invoices table, and ignore the ID. All the invoices for the supplier can be looked up by querying for the name. The same technique applied to products supplied by that supplier: store the supplier name in the product table, look up products by supplier name. This is <strong><u>not</u> because I didn’t know any better</strong>, I had <a href="http://en.wikipedia.org/wiki/Database_normalization#Normal_forms">database normal forms</a> drummed into me two decades ago. To stick with the metaphor: I know that, under normal circumstances, swallowing spiders is bad, but faced with <em>this specific fly</em> it was demonstrably the best course of action.     <br />At this point someone who could have saved me from my folly pointed out that supplier names had to be editable. I protested that the names don’t change, but <em>Amalgamated Widgets</em> <u>did</u>, in fact, become <em>Universal Widgets</em>. This is an issue because <em>Amalgamated</em> not <em>Universal </em>raised the invoices in the filing cabinet so matching them to invoices in the system requires preserving the name <u>as it was when the invoice was generated.</u> “See, I was right name should be stored” – actually this exception doesn’t show I was right at all, but on I went. On the other hand all of&#160; <em>Amalgamated’s</em> products belong to <em>Universal</em> now. Changing names means doing a cascaded update (update any product with the old company name to the new name when a name changes) the real case has more than just products. If you’re filling in the metaphor you’ve guessed I’d reached the point of figuring out how to swallow a bird. Worse, I could see another problem looming (anyone for Cat ?): changes to products had to be exported to another system, and the list of changes had their own table requiring cascaded updates from the cascaded updates. </p>
<p>One of the great quotes in <em>Macbeth </em>says “I am in blood stepped in so far that should I wade no more, Returning were as tedious as go o’er.” he knows what he’s doing is wrong, but it is as hard to go back (and do right) as it is to go on.&#160; Except it isn’t: the solution is <u>not</u> to swallow another couple more spiders and a fly, the solution is to swallow a bird, then a cat and so on.&#160; The dilemma is that the effort for an additional work-round is smaller than the effort to go back fix the initial problem and unpick all the work-rounds to date – either needs to be done now, and the easy solution is to choose the one which needs the least effort now. The sum of effort required for future work-rounds is greater but we can discount that effort because it isn’t needed <em>now</em>. Only in a non-punishing situation can we tell people that progress must be halted for a time to fix a problem which has been mitigated up to now. Persuading people that such a problem needs to fixed <u>at all</u> isn’t trivial, I heard this quote in a <a href="http://www.bbc.co.uk/programmes/b00v1qrz">Radio programme a while back</a><em></em></p>
<blockquote><p><em>“Each uneventful day that passes reinforces a steadily growing false sense of confidence that everything is alright:        <br />that I, we, my group must be OK because the way we did things today resulted in no adverse consequences.” </em></p>
</blockquote>
<p>In my case the problem is being fixed at the moment, but in how many organisations is it what career limiting move to tell people that something which has had now adverse consequences to date must be fixed?&#160; </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1802&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/04/22/dont-swallow-the-cat-doing-the-right-thing-with-software-development-and-other-engineering-projects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Customizing PowerShell, Proxy functions and a better Select-String</title>
		<link>http://jamesone111.wordpress.com/2012/02/04/customizing-powershell-proxy-functions-and-a-better-select-string/</link>
		<comments>http://jamesone111.wordpress.com/2012/02/04/customizing-powershell-proxy-functions-and-a-better-select-string/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 21:24:11 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1799</guid>
		<description><![CDATA[I suspect that even regular PowerShell users don&#8217;t customize their environment much. By co-incidence, in the last few weeks I&#8217;ve made multiple customizations to my systems (my scripts are sync&#8217;d over 3 machines, customize one, customize all). Which has given me multiple things to talk about. My last post was about adding persistent history this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1799&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I suspect that even regular PowerShell users don&#8217;t customize their environment much. By co-incidence, in the last few weeks I&#8217;ve made multiple customizations to my systems (my scripts are sync&#8217;d over 3 machines, customize one, customize all). Which has given me multiple things to talk about. My last post was about adding persistent history this time I want to look at Proxy Functions …
<p><code><strong>Select-String</strong></code> is, for my money, one of the best things in PowerShell. It looks through piped text or through files for anything which matches a regular expression (or simple text) and reports back what matched and where with all the detail you could ever need.<strong> BUT</strong> It has a couple of things wrong with it: it won&#8217;t do a recursive search for files, and sometimes the information which comes back is <strong>too </strong>detailed. I solved both problems with a function I named &#8220;WhatHas&#8221; which has been part of my profile for ages. I have been using this to search scripts, XML files and saved SQL whenever I need a snippet of code that I can&#8217;t remember or because something needs to be changed and I can&#8217;t be sure I&#8217;ve remembered which files contain it. I use WhatHas dozens (possibly hundreds) of times a week. Because it was a quick hack I didn&#8217;t support every option that <code>Select-string</code> has, so if a code snippet spans lines I have go back to the proper <code><strong>Select-String</strong></code> cmdlet and use its <code>-context</code> option to get the lines either side of the match: more often than not I find myself typing <code>dir -recurse {something} | select-String {options}</code>
<p>A while back I saw a couple of presentations on Proxy functions (there’s <a href="http://blogs.msdn.com/b/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx">a good post about them here by Jeffrey Snover</a>): I thought when I saw them that I would need to implement one for real before I could claim to understand them, and after growing tired of jumping back and forth between <code>select-string</code> and WhatHas, I decided it was time to do the job properly creating a proxy function for <code>Select-String</code> and keep whathas as an alias.&nbsp;
<p>There are 3 bits of background knowledge you need for proxy functions.
<ol>
<li><strong>Precedence</strong>. Aliases beat Functions, Functions beat Cmdlets. Cmdlets beat external scripts and programs. A function named Select-String will be called instead of a cmdlet named Select-String &#8211; meaning a function can replace a cmdlet simply by giving it the same name. That is the starting point for a Proxy function.
<li>A command can be invoked as <code>moduleName\CommandName</code>. If I load a function named “Get-Stuff” from my profile.ps1 file for example, it won&#8217;t have an associated module name but if I load it as part of a module, or if “Get-Stuff” is a cmdlet it will have a module name.<br /><code>Get-Command get-stuff | format-list name, modulename</code><br />will show this information You can try <br /><code>&gt; Microsoft.PowerShell.Management\get-childitem</code><br />For yourself. It looks like an invalid file-system path, but remember PowerShell looks for a matching Alias, then a matching Function and a then a matching cmdlet before looking for a file.
<li>Functions can have a <code>process</code> block (which runs for each item passed via the pipeline) a <code>begin</code> block (which runs before the first pass through process, and an <code>end</code> block (which runs after the last item has passed through process.) Cmdlets follow the same structure, although it&#8217;s harder to see.</li>
</ol>
<p>Putting these together<strong> A function named Select-String can call the Select-String cmdlet, but it must call it as Microsoft.PowerShell.Utility\Select-String</strong> or it will just go round in a loop. In some cases, calling it isn’t quite enough and PowerShell V2 delivered the <strong>steppable pipeline</strong> which can take a PowerShell command (or set of commands piped together) and allow us to run its begin block , process block , and end block, under the control of an function. So a Proxy function looks like this :<code><br />Function Select-String { <br />&nbsp; [CmdletBinding()] <br />&nbsp; Param&nbsp; ( <em>Same Parameters as the real Select-String<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Less any I want to prevent people using<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Plus any I want to add <br /></em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ) <br />&nbsp;&nbsp; Begin { <em>My preliminaries<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Get</em> $steppablePipeline<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $steppablePipeline.begin()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp; <br />Process { <em>My Per-item code against current item ($_ )<br /></em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $steppablePipeline.Process($_)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
<p>&nbsp;&nbsp;&nbsp;&nbsp; end { $steppablePipeline.End <br /><em>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; My Clean up code </em><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />} </code></p>
<p>What would <strong>really help</strong> would be something produce a function like this template, and fortunately it is built into PowerShell: it does the whole thing in 3 steps: Get the command to be proxied, get the detailed metadata for command and build a Proxy function with the meta data, like this:<br /><code>&nbsp; $cmd=Get-command select-string -CommandType cmdlet <br />&nbsp; $MetaData = New-Object System.Management.Automation.CommandMetaData ($cmd) <br />&nbsp; [System.Management.Automation.ProxyCommand]::create($MetaData) </code>
<p>The last command will output the Proxy function body to the console, I piped the result into Clip.exe and pasted the result into a new definition<br /><code>Function Select-String { }</code> <br />And I had a proxy function. </p>
<p>At this point it didn’t do anything that the original cmdlet doesn’t do but that was a starting point for customizing. <br />The auto-generated parameters are be formatted like this <br /><code>&nbsp; [Parameter(ParameterSetName='Object', Mandatory=$true, ValueFromPipeline=$true)] <br />&nbsp; [AllowNull()] <br />&nbsp; [AllowEmptyString()] <br />&nbsp; [System.Management.Automation.PSObject] <br />&nbsp; ${InputObject},</code> </p>
<p>And I removed some of the line breaks to reduce the screen space they use from 53 lines to about half that. <br />The ProxyCommand creator wraps parameter names in braces just in case something has a space or other breaking character in the name, and I took those out. <br />Then I added two new switch parameters <code>-Recurse</code> and <code>-BareMatches</code>.
<p>Each of the Begin, Process and End blocks in the function contains a <code>try...catch</code> statement, and in the <code>try</code> part of the begin block the creator puts code to check if the <code>-OutBuffer</code> common parameter is set and if it is, over-rides it (why I&#8217;m not sure) &#8211; followed by code to create a steppable pipeline, like this:<br /><code>&nbsp; $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Select-String', <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.Management.Automation.CommandTypes]::Cmdlet) <br />&nbsp; $scriptCmd = {&amp; $wrappedCmd @PSBoundParameters } <br />&nbsp; $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) </code>
<p>I decided it would be easiest to build up a string and make that into the steppable pipeline . In simplified form <br />&nbsp;&nbsp; <code>$wrappedCmd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = "Microsoft.PowerShell.Utility\Select-String "&nbsp; <br />&nbsp; $scriptText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = "$wrappedCmd @PSBoundParameters" <br />&nbsp; if ($Recurse)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { $scriptText = "Get-ChildItem @GCI_Params | " + $scriptText } <br />&nbsp; if ($BareMatches)&nbsp; { $scriptText += " | Select-Object –ExpandProperty 'matches' " + <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " | Select-Object -ExpandProperty 'value'&nbsp;&nbsp; " }&nbsp;&nbsp; <br />&nbsp; $scriptCmd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = [scriptblock]::Create($scriptText)&nbsp;&nbsp; <br />&nbsp; $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)</code>
<p><code>&amp; commandObject</code> works in a scriptblock: the “&amp;” sign says&nbsp; “run <em>this</em>” and if <em>this</em> is a command object that’s just fine: so the generated code has <code>scriptCmd = {&amp; $wrappedCmd @PSBoundParameters }</code> where <code><code>$wrappedCmd</code></code>&nbsp; is a command object. <br />but when I first changed the code from using a script block to using a string I put the original object <code>$wrappedCmd </code>inside a string. When the object is inserted into a string, the conversion renders it as the <em>unqualified</em> name of the command – the information about the module is lost, so I produced a script block which would call the function, which would create a script block which would call the function which&#8230; is an effective way to cause a crash.
<p>The script above won&#8217;t quite work on its own because <br />(a) I haven&#8217;t built up the parameters for <code>Get-Childitem. </code>So if <code>-recurse</code> or –<code>barematches </code>are specified I build up a hash table to hold them, using taking the necessary parameters from what ever was passed, and making sure they aren’t passed on to the Select-String Cmdlet when it is called. I also make sure that a file specification is passed for a recursive search it is moved from the path parameter to the include parameter. <br />(b) If <code>-recurse</code> or <code>-barematches</code> get passed to the” real” Select-String cmdlet it will throw a &#8220;<code>parameter cannot be found</code>&#8221; error, so they need to be removed from <code>$psboundParameters. </code></p>
<p><code></code> This means the first part of the block above turns into <br />&nbsp; <code>if ($recurse -or $include -or $exclude) { <br />&nbsp;&nbsp;&nbsp;&nbsp; $GCI_Params = @{} <br />&nbsp;&nbsp;&nbsp;&nbsp; foreach ($key in @("Include","Exclude","Recurse","Path","LiteralPath")) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($psboundparameters[$key]) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </code><code>$GCI_Params[$key] = $psboundparameters[$key] <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [void]$psboundparameters.Remove($key) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp;&nbsp; # if Path doesn't seem to be a folder it is probably a file spec <br />&nbsp;&nbsp;&nbsp;&nbsp; # So if recurse is set, set Include to hold file spec and path to hold current directory <br />&nbsp;&nbsp;&nbsp;&nbsp; if ($Recurse -and -not $include -and ((Test-Path -PathType Container $path) -notcontains $true) ) { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $GCI_Params["Include"] = $path <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $GCI_Params["Path"] = $pwd <br />&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp; $scriptText = "Get-ChildItem @GCI_Params | " <br />} <br />else { $scriptText = ""}</p>
<p></code>
<p>And the last part is <br /><code>if ($BareMatches) { <br />&nbsp; $psboundparameters.Remove("BareMatches") <br />&nbsp; $scriptText += " | Select-object -expandproperty 'matches' | Select-Object -ExpandProperty 'value' " <br />} <br /></code><code>$scriptCmd = [scriptblock]::Create($scriptText) <br />$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)</code>
<p>There’s no need for me to add anything to the process or end blocks, so that&#8217;s it &#8211; everything Select-String originally did, plus recursion and returning bare matches.
<p>I’ve put the whole file on skydrive <a href="http://brzuqg.blu.livefilestore.com/y1pyIzjqKNMe91ycmgC7LZoIoLSqPTQR_44n03TVMlCZ1P4ASTF9WwaevJOihgT-n_yS7kJwJUxPOhlTCtNzFJXJQ/Proxy-Select-String.ps1">here</a></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1799&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/02/04/customizing-powershell-proxy-functions-and-a-better-select-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding Persistent history to PowerShell</title>
		<link>http://jamesone111.wordpress.com/2012/01/28/adding-persistent-history-to-powershell/</link>
		<comments>http://jamesone111.wordpress.com/2012/01/28/adding-persistent-history-to-powershell/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 19:19:37 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1795</guid>
		<description><![CDATA[The Doctor: “You’re not are you? Tell me you’re not Archaeologists” River Song: “Got a problem with Archaeologists ?”The Doctor: “I’m a time traveller. I point and laugh at Archaeologists” I’ve said to several people that my opinion of Linux has changed since I left Microsoft: after all I took a job with Microsoft because [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1795&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><em>The Doctor: “You’re not are you? Tell me you’re not Archaeologists” <br />River Song: “Got a problem with Archaeologists ?”<br />The Doctor: “I’m a time traveller. I point and laugh at Archaeologists”</em></p>
<p>I’ve said to several people that my opinion of Linux has changed since I left Microsoft: after all I took a job with Microsoft because I rated their products and stayed there 10 years, I didn’t have much knowledge of Linux at the start of that stay and had no call to develop any during it, so my views I had was based on ignorance and supposition. After months of dealing with it on a daily basis I know how wrong I was. In reality Linux is uglier, more dis-functional and, frankly, retarded than I ever imagined it could be. Neither of our Linux advocates suggest it should be used anywhere but servers (just as well they both have iPhones and Xboxes which are about as far from the Open source ideal as it’s possible to go). But compare piping or tab expansion in PowerShell and Bash and you’re left in no doubt which one was designed 20 years ago. “You can only pipe text ? Really ? How … <em>quaint”. </em></p>
<p>One of guys was trying to do something with Exchange from the command-line and threw down a gauntlet. <br />“<strong>If PowerShell’s so bloody good why hasn’t it got persistent history”</strong> <br />OK. This is something which Bash has got going for it. How much work would it take to fix this ? Being PowerShell the answer is “a few minutes”. Actually the answer is “a lot less time than it takes to write a blog post about it”</p>
<p>First a little side track various people I know have a PowerShell prompt which looks like <br /><code>[123]&nbsp; PS C:\users\Fred&gt;</code><br />Where 123 was the<strong> history ID.</strong> Type <code>H</code> (or <code>history</code>, or <code>Get-History</code>) and PowerShell shows you the previous commands, with their history ID, the command <code>Invoke-History &lt;id&gt;</code> (or <code>ihy</code> for short) runs the command. <br />I’d used PowerShell for ages before I discovered typing #&lt;id&gt;[Tab] inserts the history item into the command line. I kept saying “I’ll do that one day”, and like so many things I didn’t get round to it. <br />I already use the history ID I have this function in my profile<br /><code><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">HowLong</span> <span style="color:#000000;">{</span><br /><span style="color:#006400;">&nbsp;&nbsp; &lt;# .Synopsis Returns the time taken to run a command <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Description By default returns the time taken to run the last command <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#006400;">.Parameter ID The history ID of an earlier item. <br />&nbsp;&nbsp; #&gt;</span><br /><span style="color:#00008b;">&nbsp;&nbsp; param</span>&nbsp; <span style="color:#000000;">(</span> <span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">ValueFromPipeLine</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff4500;">$id</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$MyInvocation</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">HistoryId</span> <span style="color:#a9a9a9;">-</span><span style="color:#800080;">1</span><span style="color:#000000;">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&nbsp;&nbsp;&nbsp; <span style="color:#000000;">)</span><br /><span style="color:#00008b;">&nbsp; process</span> <span style="color:#000000;">{</span>&nbsp; <span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$i</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$id</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <br /><span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (</span><span style="color:#0000ff;">get-history</span> <span style="color:#ff4500;">$i</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">endexecutiontime</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">subtract</span><span style="color:#000000;">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#000000;">(</span><span style="color:#0000ff;">get-history</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$i</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">startexecutiontime</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">totalseconds</span><br /><span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span><br /><span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span> <br /><span style="color:#000000;">}</span><br /></code>Once you know <strong>$MyInvocation.HistoryID gives the ID of the current item,</strong> it is easy to change the Prompt function to return something which contains it. </p>
<p>At the moment I find I’m jumping back and forth between PowerShell V2, and the CTP of V3 on my laptop <br />(and I can run PowerShell –Version 2 to launch a V2 version if I see something which I want to check between versions). <br />So<strong> I finally decided I would change the prompt function. </strong>This happened about the time I got the <strong>“why doesn’t the history get saved” </strong>question. Hmmm. Working with history in the Prompt function. Tick, tick, tick.&nbsp; [Side track 2 In PowerShell the prompt isn’t a constant, it is the result of a function.&nbsp; To see the function use the command type function:prompt] <br />So here is the prompt function I now have in my profile. <br /><code><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">prompt</span> <span style="color:#000000;">{</span><br /><span style="color:#ff4500;">&nbsp; $hid</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$myinvocation</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">historyID</span><br /><span style="color:#00008b;">&nbsp; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$hid</span> <span style="color:#a9a9a9;">-gt</span> <span style="color:#800080;">1</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#0000ff;">get-history</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$myinvocation</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">historyID</span> <span style="color:#a9a9a9;">-</span><span style="color:#800080;">1</span> <span style="color:#000000;">)</span> <span style="color:#a9a9a9;">|</span> <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#0000ff;">convertto-csv</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">Select</span> <span style="color:#000080;">-last</span> <span style="color:#800080;">1</span> <span style="color:#a9a9a9;">&gt;&gt;</span> <span style="color:#ff4500;">$logfile<br />&nbsp; </span><span style="color:#000000;">}</span><br /><span style="color:#000000;">&nbsp; $(</span><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">test-path</span> <span style="color:#8a2be2;">variable:/PSDebugContext</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#8b0000;">'[DBG]: '</span> <span style="color:#000000;">}</span> <span style="color:#00008b;">else</span> <span style="color:#000000;">{</span> <span style="color:#8b0000;">''</span> <span style="color:#000000;">}</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span>&nbsp;<br />&nbsp;&nbsp;&nbsp; <span style="color:#8b0000;">"#$([math]::abs($hid)) PS$($PSVersionTable.psversion.major) "</span> <span style="color:#a9a9a9;">+ </span><span style="color:#000000;">$(</span><span style="color:#0000ff;">Get-Location</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span>&nbsp;<br />&nbsp;&nbsp;&nbsp; <span style="color:#000000;">$(</span><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$nestedpromptlevel</span> <span style="color:#a9a9a9;">-ge</span> <span style="color:#800080;">1</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#8b0000;">'&gt;&gt;'</span> <span style="color:#000000;">}</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">+</span> <span style="color:#8b0000;">'&gt; '</span><br /><span style="color:#000000;">}</span></code></p>
<p><code><span style="color:#000000;"></span></code>The first part is new lines are new: get the history ID and if is greater than 1, get the previous history item, convert from an object to CSV format, discard the CSV header and append it to the file named in <code>$logFile</code> (I know I haven’t set it yet)</p>
<p>The second part is lifted from the prompt function found in the default profile, that reads <br /><code>"PS $($executionContext.SessionState.Path.CurrentLocation)$('&gt;' * ($nestedPromptLevel + 1)) "</code><br />It’s actually one line but I’ve split it at the + signs for ease of reading. <br />I put the a # sign and the history ID before “PS” – when PowerShell starts the ID is –1 so I make sure it is the absolute value.<br />After “PS” I put the major version of PowerShell. <br />I’m particularly pleased with the #ID part in the non-ISE version of PowerShell double clicking on #ID selects it. My mouse is usually close enough to my keyboard that the keypad [Enter] key is within reach of my thumb so if I scroll up to look at something I did earlier, one flickity gesture (double-click, thumb enter, right click [tab]) has the command in the current command line. </p>
<p>So now I’m keeping a log, and all I need to do is to load that log my from Profile. PowerShell has an Add-History command and the on-line help talks about reading in the history from a CSV file so that was easy – I decided I would truncate the log when PowerShell started and also ensure that the file had the CSV header so here’s the reading friendly version of what’s in my profile.</p>
<p><code><span style="color:#ff4500;">$MaximumHistoryCount</span> <span style="color:#a9a9a9;">=</span> <span style="color:#800080;">2048</span><br /><span style="color:#ff4500;">$Global:logfile</span> <span style="color:#a9a9a9;">=</span> <span style="color:#8b0000;">"$env:USERPROFILE\Documents\windowsPowershell\log.csv"</span> <br /><span style="color:#ff4500;">$truncateLogLines</span> <span style="color:#a9a9a9;">=</span> <span style="color:#800080;">100</span><br /><span style="color:#ff4500;">$History</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">@(</span><span style="color:#000000;">)</span><br /><span style="color:#ff4500;">$History</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">'#TYPE Microsoft.PowerShell.Commands.HistoryInfo'</span><br /><span style="color:#ff4500;">$History</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#8b0000;">'"Id","CommandLine","ExecutionStatus","StartExecutionTime","EndExecutionTime"'</span><br /><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">Test-Path</span> <span style="color:#ff4500;">$logfile</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$history</span> <span style="color:#a9a9a9;">+=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">get-content</span> <span style="color:#ff4500;">$LogFile</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">[</span><span style="color:#a9a9a9;">-</span><span style="color:#ff4500;">$truncateLogLines</span><span style="color:#a9a9a9;">..</span><span style="color:#800080;">-1</span><span style="color:#a9a9a9;">]</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">where</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$_</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#8b0000;">'^"\d+"'</span><span style="color:#000000;">}</span> <span style="color:#000000;">}</span><br /><span style="color:#ff4500;">$history</span> <span style="color:#a9a9a9;">&gt;</span> <span style="color:#ff4500;">$logfile</span><br /><span style="color:#ff4500;">$History</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">select</span> <span style="color:#000080;">-Unique</span>&nbsp; <span style="color:#a9a9a9;">|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&nbsp;<span style="color:#0000ff;">Convertfrom-csv</span> <span style="color:#000080;">-errorAction</span> <span style="color:#8a2be2;">SilentlyContinue</span> <span style="color:#a9a9a9;">|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>&nbsp;<span style="color:#0000ff;">Add-History</span> <span style="color:#000080;">-errorAction</span> <span style="color:#8a2be2;">SilentlyContinue</span> </code></p>
<p><strong>UPDATE Copying this code into the blog page and splitting the last $history line to fit, something went wrong and the <br />select -unique went astray. Oops.</strong> It’s there because hitting enter doesn’t advance the History count, or run anything but does cause the prompt function to re-run. Now I’ve had to look it again it occurs to me it would be better to have select –unique in the (get-content $logfile) rather in the Add-history section as this would remove duplicates before truncating. </p>
<p>So … increase the history count, from the default of 64 (writing this I found that in V3 ctp 2 the default is 4096). Set a Global variable to be the path to the log file, and make it obvious what the length is I will truncate the log to.<br />Then build an array of strings named history. Put the CSV header information into $history, and if the log file exists put up to the truncate limit of lines into $history as well. Write $history back to the log file and pipe it into add history, hide any lines which won’t parse correctly. Incidentally those who like really long lines of PowerShell could recode all lines with $history in them into one line. So a couple of lines in the prompt function and between 3 and 9 lines in the profile depending on how you write them all in it’s less than a dozen lines. This blog post has taken a good couple of hours, and I don’t the code in 10 to 15 minutes. </p>
<p><a href="http://jamesone111.files.wordpress.com/2012/01/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2012/01/image_thumb.png?w=600&#038;h=254" width="600" height="254"></a></p>
<p>Oh , and one thing I really like – when I launch PowerShell –Version 2 inside Version 3, it imports the history giving easy access to the commands I just used without needing to cut and paste. </p>
<p>If you’re a Bash user and didn’t storm off in a huff after my initial rudeness I’d like to set a good natured challenge. A non-compiled enhancement to bash I can load automatically which gives it tab expansion on par with PowerShell’s (OK, PowerShell has an unfair advantage completing parameters, so just command names and file names). And in case you wondered about the quote at the top of the post from one of Stephen Moffat’s Dr Who episodes. You see, <strong>“I Know PowerShell. I point and laugh at Bash users.” </strong></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1795&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2012/01/28/adding-persistent-history-to-powershell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2012/01/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>Free NetCmdlets</title>
		<link>http://jamesone111.wordpress.com/2011/12/20/free-netcmdlets/</link>
		<comments>http://jamesone111.wordpress.com/2011/12/20/free-netcmdlets/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 21:48:52 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1791</guid>
		<description><![CDATA[I’ve mentioned the NetCmdlets before. Although not perfect if you spend a lot of your life using PowerShell and various network tools they are a big help. They’ve made a bunch of things which would have been longwinded and painful relatively easy. So here is a mail I have just had from PowerShell inside (aka [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1791&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h5>I’ve <a href="https://jamesone111.wordpress.com/2011/08/23/enhancing-the-netcmdlets/">mentioned the NetCmdlets before.</a> Although not perfect if you spend a lot of your life using PowerShell and various network tools they are a big help. They’ve made a bunch of things which would have been longwinded and painful relatively easy. So here is a mail I have just had from PowerShell inside (aka /n software) . I don’t normally hold with pasting mails straight into a blog post, but you’ll see why if you read on: if you click the link it asks you to fill in some details and “<em>A member of our sales team will contact you with your FREE </em><a href="http://www.powershellinside.com/powershell/netcmdlets"><em>NetCmdlets Workstation License</em></a><em>. (Limit one per customer.)”</em></h5>
<blockquote><p><a href="http://nsoftware.us1.list-manage.com/track/click?u=62a6ad331ef56f40b64703ca2&amp;id=efe5def7ae&amp;e=4056f37911"><em>A Gift For The Holidays: FREE NetCmdlets Workstation License &#8211; This Week Only, Tell a Friend!</em></a></p>
<p><em>Help us spread some PowerShell cheer! Tweet, blog, post, email, or just tell a friend and you can both <b>receive a completely free workstation license of NetCmdlets!</b></em>
<p><em>NetCmdlets includes powerful cmdlets offering easy access to every major Internet technology, including: SNMP, LDAP, DNS, Syslog, HTTP, WebDav, FTP, SMTP, POP, IMAP, Rexec/RShell, Telnet, SSH, Remoting, and more. Hurry, this offer ends on Christmas day &#8211; Happy Holidays! </em><br />
<table border="0" cellpadding="0">
<tbody>
<tr>
<td>
<p><b><em>NetCmdlets Workstation License:</em></b></p>
</td>
<td>
<p><em><s>$99.00</s> <b>FREE</b></em></p>
</td>
</tr>
<tr>
<td valign="top">
<p><em><b>NetCmdlets Server License:</b><br />* Special Limited Time Offer *</em></p>
</td>
<td valign="top">
<p><em><s>$349.00</s> <b>$199.00</b> </em><a href="http://nsoftware.us1.list-manage1.com/track/click?u=62a6ad331ef56f40b64703ca2&amp;id=c656ba1478&amp;e=4056f37911"><em>[+] Order Now</em></a></p>
</td>
</tr>
</tbody>
</table>
<p><b><em>Hurry, this offer ends on Christmas day &#8211; </em><a href="http://nsoftware.us1.list-manage1.com/track/click?u=62a6ad331ef56f40b64703ca2&amp;id=3354724bf7&amp;e=4056f37911"><em>Get your free license now</em></a><em>!</em></b>
<p>Happy Holidays!</p>
</blockquote>
<p>Or as we say this side of the Pond <strong>Merry Christmas !</strong></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1791&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/12/20/free-netcmdlets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>PowerShell HashTables: Splatting, nesting, driving selections and generally simplifying life</title>
		<link>http://jamesone111.wordpress.com/2011/12/10/powershell-hashtables-splatting-nesting-driving-selections-and-generally-simplifying-life/</link>
		<comments>http://jamesone111.wordpress.com/2011/12/10/powershell-hashtables-splatting-nesting-driving-selections-and-generally-simplifying-life/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 23:43:12 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[splatting]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1784</guid>
		<description><![CDATA[Having spent so much of my life working in Microsoft environments where you typed a password to get into your computer and never had to type it again until the next time you had to login / unlock it (however many servers you worked with in between)&#160; I find working in an office with a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1784&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Having spent so much of my life working in Microsoft environments where you typed a password to get into your computer and never had to type it again until the next time you had to login / unlock it (however many servers you worked with in between)&nbsp; I find working in an office with a bunch of Linux servers but no shared set of accounts to be alternately comical and stupid.&nbsp; <br />Some of our services use Active Directory to provide LDAP authentication so at least I retype <u>the same</u> user name and password over and over: but back as far as the LAN Manager client for DOS, Microsoft network clients tried to transparently negotiate connections using credentials they already had. Internet explorer does the same when connecting to intranet servers. Even in a workgroup if you manually duplicate accounts between machines you’ll get connected automatically. I stopped retyping passwords about the same time as I stopped saving work to floppy disk; I’m baffled that people in the Unix/Linux world tolerate it.<br />Some of our services use a separate LDAP service instead of AD (just to keep me on my toes I’m JamesOne on one, and Joneill on the other) and others again use their own internal accounts database. I might log on to a given box as <em>root</em>, and sign into MySQL as <em>root</em> but the passwords are different. If I go to another box the root password is different again. And so on. <br />Recently we hit a roadblock because one of the team had set up a server with a root password he couldn’t share (because of the other places he’d used it) and he gave me a key file as a work round. <a href="http://jamesone111.wordpress.com/2011/08/23/enhancing-the-netcmdlets/">I’ve talked before</a> about using the <a href="http://www.powershellinside.com/powershell/netcmdlets/"><strong>NetCmdlets</strong></a> to connect run commands on the Linux boxes. And this sent me back to look at how I was using them. I ended up <strong>driving them from Hash Table of Hash Tables</strong>&nbsp; &#8211; something <a href="http://blogs.technet.com/b/heyscriptingguy/archive/2011/12/10/create-a-hash-table-in-powershell-that-contains-hash-tables.aspx">Ed Wilson has recently written about on the “Hey Scripting Guy” blog</a>. </p>
<p>I use the Hash tables for <strong>splatting&nbsp; -</strong> the name, if not the concept, is peculiar to PowerShell. If it’s not familiar, lets say I want to invoke the command</p>
<p><code>connect-ssh –Server "192.168.42.42" -Force –user "root" –AuthMode "publickey" –CertStoreType "pemkeyfile" `<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -CertSubject "*" -certStore="$env:USERPROFILE\documents\secret.priv"</code> </p>
<p>I can create a hash table ” with members Server, Force, Authmode and so on.&nbsp; Normally when we refer to PowerShell variables we want to work with their <em>Values</em> so we prefix the name with the $ sign. Prefixing the name with the @ sign instead turns the members of an object into parameters for a command : like this </p>
<p><code>
<p>$master =@{Server="192.168.42.42"; Force=$True; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; user="root"; AuthMode="publickey"; CertStoreType="pemkeyfile";&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CertSubject="*"; certStore="$env:USERPROFILE\documents\secret.priv"}<br />connect-ssh @master</p>
<p></code>
<p>For another server I might logon with conventional user name and password so I might have: </p>
<p><code>
<p>$Spartacus =@{Server="192.168.109.71"; Force=$True; user="root";}</code></p>
<p>and use that hash table as the basis of logging on.&nbsp; If I want to use one of the copy commands I might add two hash tables together – one containing logon information and the other containing the parameters relating to what should be copied – or I might specify these in the normal way in addition to my splatted variable. <br />In <a href="http://jamesone111.wordpress.com/2011/08/23/enhancing-the-netcmdlets/">my original piece</a> about using the <a href="http://www.powershellinside.com/powershell/netcmdlets/">NetCmdlets</a> I showed <code>Invoke-RemoteCommand, Get-RemoteItem, Copy-Remote-Item, Copy-LocalItem</code> and so on: I have modified all of these to<strong> take a hash table as a parameter and use splatting.</strong> I also showed a set-up function named <code>connect-remote</code>: the hash tables are set up when I load the module, but they don’t contain credentials: <code>connect-remote</code> now looks at the hash-table for the connection I want to make and says “Is this fully specified specified to use a certificate, if not does it have credentials ?” If the answer is no to both parts it <strong>prompts for credentials and adds them to the hash table</strong> – in the snippet below <code>$Server</code> contains the hash table, and <code>$user</code> can be a parameter passed to <code>connect-remote</code> or in the Hash table, and if it can&#8217;t be found in either place it is set to the current user name</p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">-not</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$server</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">AuthMode</span> <span style="color:#a9a9a9;">-or</span> <span style="color:#ff4500;">$server</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">credential</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><br /><span style="color:#ff4500;">&nbsp;&nbsp;&nbsp; $server</span><span style="color:#a9a9a9;">[</span><span style="color:#8b0000;">"credential"</span><span style="color:#a9a9a9;">]</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Host</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ui</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">PromptForCredential</span><span style="color:#000000;">(</span><span style="color:#8b0000;">"Login"</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">"Enter Your Details for $ServerName"</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">"$user"</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">""</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">}</span><br /></code>
<p>A global variable set in <code>connect-remote</code> keeps track of which of the hash tables with SSH settings in should be used as the default for <code>Invoke-RemoteCommand, Get-RemoteItem, Copy-Remote-Item, Copy-LocalItem</code> and so on. But it makes sense to have all the hash tables listed somewhere where they can be accessed by name so I have </p>
<p><code>$hosts = @{ master =@{Server="192.168.42.42"; Force=$True; user="root"; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AuthMode="publickey"; CertStoreType="pemkeyfile"; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CertSubject="*"; certStore="$env:USERPROFILE\documents\secret.priv"}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Spartacus =@{Server="192.168.109.71"; Force=$True; user="root";}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Maximus =@{Server="192.168.10.10"; Force=$True; user="joneill";}<br />}</code><code></code><br /></code></p>
<p>In <code>connect-remote,</code> the -<code>server</code> parameter is declared like this&nbsp; </p>
<p><code><span style="color:#ff4500;">&nbsp; $server</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$hosts</span><span style="color:#a9a9a9;">[</span><span style="color:#000000;">(</span><span style="color:#0000ff;">select-item</span> <span style="color:#ff4500;">$hosts</span> <span style="color:#000080;">-message</span> <span style="color:#8b0000;">"Which server do you want to connect to ? "</span> <span style="color:#000080;">-returnkey</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span></code> </p>
<p> <code><span style="color:#0000ff;"><font color="#000000">Select-item </font></span></code>is a function I wrote ages ago which takes a hash table and offers the user a choice based of the keys in the hash table and returns either the number of the choice (not very helpful for hash tables) or its name. <code><br /><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">Select-Item</span><br /><span style="color:#000000;">{</span><span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">CmdletBinding</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><br /><span style="color:#00008b;">param</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">ParameterSetName</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">"p1"</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;">Position</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[String[]]</span><span style="color:#ff4500;">$TextChoices</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#a9a9a9;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [</span><span style="color:#00bfff;">Parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">ParameterSetName</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">"p2"</span><span style="color:#a9a9a9;">,</span><span style="color:#000000;">Position</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#008080;">[hashTable]</span><span style="color:#ff4500;">$HashChoices</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#008080;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [String]</span><span style="color:#ff4500;">$Caption</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">"Please make a selection"</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#008080;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [String]</span><span style="color:#ff4500;">$Message</span><span style="color:#a9a9a9;">=</span><span style="color:#8b0000;">"Choices are presented below"</span><span style="color:#a9a9a9;">,</span> <br /><span style="color:#008080;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [int]</span><span style="color:#ff4500;">$default</span><span style="color:#a9a9a9;">=</span><span style="color:#800080;">0</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#008080;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Switch]</span><span style="color:#ff4500;">$returnKey</span><br /><span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )</span> <br /><span style="color:#ff4500;">$choicedesc</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">New-Object</span> <span style="color:#8a2be2;">System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]</span> <br /><span style="color:#00008b;">switch</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$PsCmdlet</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ParameterSetName</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span>&nbsp;<br /><span style="color:#8b0000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "p1"</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$TextChoices</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">ForEach-Object</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$choicedesc</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Add</span><span style="color:#000000;">(</span><span style="color:#000000;">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#0000ff;">New-Object</span> <span style="color:#8b0000;">"System.Management.Automation.Host.ChoiceDescription"</span> <span style="color:#000080;">-ArgumentList</span> <span style="color:#ff4500;">$_</span> <span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">}</span> <span style="color:#000000;">}</span> <br /><span style="color:#8b0000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "p2"</span> <span style="color:#000000;">{</span><span style="color:#00008b;">foreach</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$key</span> <span style="color:#00008b;">in</span> <span style="color:#ff4500;">$HashChoices</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Keys</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$choicedesc</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">Add</span><span style="color:#000000;">(</span><span style="color:#000000;">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#0000ff;">New-Object</span> <span style="color:#8b0000;">"System.Management.Automation.Host.ChoiceDescription"</span> <span style="color:#000080;">-ArgumentList</span> <span style="color:#ff4500;">$key</span><span style="color:#a9a9a9;">,</span><span style="color:#ff4500;">$HashChoices</span><span style="color:#a9a9a9;">[</span><span style="color:#ff4500;">$key</span><span style="color:#a9a9a9;">]</span> <span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000000;">}</span> <span style="color:#000000;">}</span> <br /><span style="color:#000000;">}</span><br /><span style="color:#00008b;">If</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$returnkey</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span> <span style="color:#ff4500;">$choicedesc</span><span style="color:#a9a9a9;">[</span><span style="color:#ff4500;">$Host</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ui</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">PromptForChoice</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$caption</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$message</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$choicedesc</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$default</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">label</span> <span style="color:#000000;">}</span><br /><span style="color:#00008b;">else</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#000000;">{</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#ff4500;">$Host</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">ui</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">PromptForChoice</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$caption</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$message</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$choicedesc</span><span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$default</span><span style="color:#000000;">)</span> <span style="color:#000000;">}</span><br /><span style="color:#000000;">}</span></code></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a> Tagged: <a href='http://jamesone111.wordpress.com/tag/splatting/'>splatting</a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1784&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/12/10/powershell-hashtables-splatting-nesting-driving-selections-and-generally-simplifying-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>PowerShell&#8211;full of stringy goodness.</title>
		<link>http://jamesone111.wordpress.com/2011/12/03/powershellfull-of-stringy-goodness/</link>
		<comments>http://jamesone111.wordpress.com/2011/12/03/powershellfull-of-stringy-goodness/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 14:28:02 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/?p=1780</guid>
		<description><![CDATA[I think almost everyone who works with PowerShell learns two things in their first few minutes. (a) Assigning some text to a Variable looks like this $Name =&#160; "James" (b) When you wrap a string in double quotes it expands variables inside it, for example "Hello $Name" will evaluate to Hello James. A little later [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1780&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I think almost everyone who works with PowerShell learns two things in their first few minutes. <br />(a) Assigning some text to a Variable looks like this <code>$Name =&nbsp; "James" </code><br />(b) When you wrap a string in double quotes it expands variables inside it, for example <code>"Hello $Name"</code> will evaluate to <code>Hello James.</code> </p>
<p>A little later we tend to learn that if we want to put a property of a variable in string things get marginally more complex.<br /><code>"The name $Name is $name.length characters long"</code> gives the text <code>the name James is James.length characters long.</code> </p>
<p>To get the length of what is stored in the variable named “name” we need to use <code>"The name $Name is $($name.length) characters long"</code>. This gives <code>the name James is 5 characters long.</code></p>
<p>And most of us also learn that PowerShell can have Multi-line strings, sometimes called Here-Strings. which look like this</p>
<p><code>$HereString=@"<br />Here is a string<br />The string $name made<br />It hasn’t finished yet<br />There’s more<br />OK we’re done<br />"@</code></p>
<p>Here-strings are really useful because they don’t close until they reach “@ at the start of a line, they can contain quotes, newlines and if they use double quotes they too will expand variables. </p>
<p>I should say here that I don’t have any detailed knowledge of how PowerShell’s parser <em>actually</em> deals with strings, but it <em>seems</em> pretty clear that when it hits a $ sign it says “OK I need to work out a value here”. I think using $ to mean “value of” was something PowerShell picked up from another language but I can’t be sure which one it was. My inner Pedant wants to correct people when they say “PowerShell variable names begin with $” <code>$foo</code> means the value that is in <code>foo</code>: the <em>name</em> is foo. The way I understand it, when the parser sees $ inside a string it looks to see what comes next: if it is a sequence of letters it assumes they are the name of variable, which it should insert into the string. If is a “(“ character then it looks for the matching “)” and evaluates the expression inside, like <code>$name.length</code>. Easy. Only this week I found myself saying … <em>these are nothing special … well, we can get much better things than that. </em></p>
<p>I had to create over a dozen XML files: all identical except each one had a different database table name and a subset of the fields in the table – some had just 2 fields others had more than a dozen, the XML went like this. <br /><code>&lt;stuff&gt;<br />&nbsp;&nbsp; &lt;kind1 name=<strong>TableName</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field1</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field2</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field3</strong>&gt;<br />&nbsp;&nbsp; &lt;/kind1&gt;<br />&nbsp;&nbsp; &lt;xmlgumf /&gt;<br />&nbsp;&nbsp; &lt;kind2 name=<strong>TableName</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field1</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field2</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field3</strong>&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;field name=<strong>field3</strong>&gt;<br />&nbsp;&nbsp; &lt;/kind2&gt;<br />&nbsp;&nbsp; &lt;xmlgumf /&gt;<br />&lt;/stuff&gt;</code></p>
<p>Except the real XML was <em>much</em> more complicated. Using the tool which builds the XML file from scratch takes about an hour per file. The XML files then go into another tool which is where the real magic happens. Building a dozen files would be couple of days work, or with the project I’m working on , a very late night. <br />I had a document with the table names and the field lists in them, so <strong>could I automate the creation of these XML files ?<br /></strong>Outside of a string I’d write something like this<br /><code>$fields=”Field1”,”Field2”,”Field3”<br />$fields | foreach -begin {$a=""} -process {$a += [environment]::newline + "&lt;Field name=$_ &gt;"} -end {$a}</p>
<p></code>
<p>Would that second line work in a here-string based on an existing XML file?<strong> How well would this work ?</strong></p>
<p></code>$Table = "TableName"<br />$fields=”Field1”,”Field2”,”Field3”<br />@"<br />&lt;stuff&gt;<br />&nbsp;&nbsp; &lt;kind1 name=$Table&gt;$($fields | foreach -begin {$a=""} -process {$a += [environment]::newline + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Field name=$_ &gt;"} -end {$a})<br />&nbsp;&nbsp; &lt;/kind1&gt;<br />&nbsp;&nbsp; &lt;xmlgumf /&gt;<br />&nbsp;&nbsp; &lt;kind2 name=$Table&gt;$($fields | foreach -begin {$a=""} -process {$a += [environment]::newline + " &lt;Field name=$_ &gt;"} -end {$a})<br />&nbsp;&nbsp; &lt;/kind2&gt;<br />&nbsp;&nbsp; &lt;xmlgumf /&gt;<br />&lt;/stuff&gt;<br />"@ | out-file "$table.xml” –encoding ascii</code></p>
<p>The simple answer is <strong>it works like a charm. </strong><br />Building the first XML file took an hour using the normal tool to do it from scratch, it then took 5 minutes to convert that file into a template - which to my immense delight worked first time. Producing each additional XML file took 2 minutes. Half an hour in all. That’s 10.5 hours I’ve got to do something else with. And showing it to colleagues their reaction was I had performed serious magic. The kind of magic which lets me disappear early. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1780&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/12/03/powershellfull-of-stringy-goodness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>
	</item>
		<item>
		<title>Maximize the reuse of your PowerShell</title>
		<link>http://jamesone111.wordpress.com/2011/10/24/maximize-the-reuse-of-your-powershell/</link>
		<comments>http://jamesone111.wordpress.com/2011/10/24/maximize-the-reuse-of-your-powershell/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 15:18:18 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/2011/10/24/maximize-the-reuse-of-your-powershell/</guid>
		<description><![CDATA[Last week I was at The Experts Conference in Frankfurt presenting at the PowerShell Deep Dive. My presentation was entitled “Maximize the reuse of your PowerShell”. My PowerShell library for managing Hyper-V has now gone through a total of 100,000 downloads over all its different versions but whether it’s got wide use because of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1776&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tbody>
<tr>
<td valign="top">Last week I was at The Experts Conference in Frankfurt presenting at the PowerShell Deep Dive. <a href="https://skydrive.live.com/view.aspx?cid=1EFE2682BFBBD817&amp;resid=1EFE2682BFBBD817%212431">My presentation</a> was entitled “Maximize the reuse of your PowerShell”. <br />My PowerShell library for managing Hyper-V has now gone through a total of 100,000 downloads over all its different versions but whether it’s got wide use because of the way I wrote it or in spite of it, I can’t say.&nbsp; Some people whose views I value seem to agree with the ideas I put forward in the talk, so I’m setting them out in this (rather long) post. </td>
<td valign="top"><a href="http://jamesone111.files.wordpress.com/2011/10/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="image" border="0" alt="image" src="http://jamesone111.files.wordpress.com/2011/10/image_thumb.png?w=308&#038;h=142" width="308" height="142"></a></td>
</tr>
</tbody>
</table>
<p>I have never lost my love of Lego. Like Lego,<em>&nbsp;</em><strong>PowerShell succeeds by being a kit of small, general purpose blocks to link together.&nbsp; Not everything we do can extend that kit, but we should aim to do that where possible. </strong>I rediscovered the <a href="http://blogs.msdn.com/b/powershell/archive/2007/03/19/monad-manifesto-the-origin-of-windows-powershell.aspx">Monad Manifesto</a> recently via <a href="http://www.jsnover.com/blog/2011/10/01/monad-manifesto/">a piece Jeffrey Snover about </a>how PowerShell has remained true to the original vision, named Monad. It talks of a model where <em>“Every executable should do a narrow set of functions and complex functions should be composed by pipelining or sequencing executables together”.&nbsp; </em><u>Your work is easier to reuse if it becomes a building block that can be incorporated into more complex solutions;</u> and this certainly isn’t unique to PowerShell.&nbsp;&nbsp;&nbsp;
<p><strong>Functions for re-use, scripts for a single task.</strong> If you a use .PS1 script to carry out a task it is effectively a batch file:&nbsp; it <u>is</u> automating stuff so that’s good, but it isn’t a building block for other work. Once loaded, functions behave like like compiled cmdlets. If you separate parts of a script into functions it should be done with the aim of making those parts easy to reuse and recycle, <u>not</u> simply be to reformat a script into subroutines. <br />If we’re going to write functions how should they look?
<p><strong>Choose names wisely, One task : One Function, there is no &#8220;DoStuffWith&#8221; verb for a reason.<br /></strong>PowerShell cmdlets use names in the form <em>Verb-Noun </em>and it is best to stick to this model; the <code>Get-Verb</code> command will give you a list of the <em>Approved </em>verbs. PowerShell’s enforcement of these standards is limited to raising a warning if you use non-standard names in a module. If your action <u>really</u> isn’t covered by a standard verb, then <a href="http://connect.microsoft.com/PowerShell">log the need for another one on connect</a>; but if you use “Duplicate” when the standard says “Copy”, or “Delete” when the standard says “Remove” it simply makes things more difficult for people who know PowerShell but don’t know your stuff. The same applies to parameter names: try to use the ones people already know. Getting the function name right sets the scope for your work. For my talk I used an example of generating MD5 hashes for files. My assertion was that the MD5 hash <em>belonged to </em>the file, and so command should return a file with an added MD5 hash &#8211; meaning my command should be <code>ADD-MD5</code>.</p>
<p><strong>Output: Use the right Out- and Write- cmdlets</strong> Other speakers at the Deep Dive made the point that output created with <code>Write-Host</code> isn’t truly <em>returned </em>(in the sense that it can be piped or redirected) it is <em>writing on the console screen; </em>so only use <code>Write-Host</code> if you want to <u>prevent output going anywhere else</u>. There are other “write on the screen without returning” cmdlets which might be better: for example when debugging we often want messages that say “starting stage 1” , “starting stage 2” and so on. One way to do this would be to add <code>Write-Host</code> statements and after the code is debugged remove them. A better way is to use <code>Write-Verbose</code> which outputs if you specify a <code>–verbose</code> switch and doesn’t if you don’t. As a side effect you have a quasi-comment in your code which tells you what is happening. The same is true when you use <code>Write-Progress</code> to indicate something is happening in a long running function, (remember to run it with the <code>-completed</code> switch when you have finished otherwise your progress box can remain on screen while something else runs). <code>Write-Debug, Write-Error</code> and <code>Write-Warning</code> are valuable, I try to prevent errors appearing where the code can recover and write a warning when something didn’t go to plan, in a non-fatal way.</p>
<p><strong>Formatting</strong> results can be a thorny issue: you <em>can</em> redirect the results of <code>Format-Table</code> or <code>Format-List</code> to a file, but the output is useless if you want to pipe results into another command – which needs the original object. <br />Some objects can have ugly output: it’s not a crime to have an <code>–asTable</code> switch so output goes through <code>Format-Table</code> at the end of the function or even to produce pretty output by default provided you <strong>ensure that it is possible to get the raw object into the pipe</strong> with a <code>–noFormat</code> or <code>–Raw</code> switch. But it’s <strong>best to create formatting XML</strong> and a lot easier with tools like <a href="http://ezout.start-automating.com/">James Brundage’s E-Z-out.</a></p>
<p><strong>Output with the pipeline in mind.</strong>&nbsp;<u>It’s not enough to just avoid write-host and return some sort of object</u>. I argued that a function should <em><strong>return the richest object that is practical &#8211; </strong></em>my example used <code>Add-Member</code> to take a file object and give it an additional MD5Hash property. I can do anything with the result that I can do with a file and default formatting rules for a file are used (which is usually good)&nbsp; but being the right <u>type</u> need not matter if the object has the right property/properties. For example, this line of PowerShell :<br /><code>&nbsp; Select-String -Path *.ps1 -Pattern "Select-String"</code><br />looks at all the .PS1 files in the current folder and where it finds the text&nbsp; “<code>Select-String</code>”&nbsp; it outputs a <code>MatchInfo</code> object with the properties: <code>.Context, .Filename, .IgnoreCase, .Line, .LineNumber, .Matches, .Path </code>and<code> .Pattern.</code> A<code> </code>Cmdlet like <code>Copy-Item</code> has no idea about <code>MatchInfo</code> objects, but if an object piped in has a <code>.path</code> property, it will knows what it is being asked to work on. If <code>Matchinfo</code> objects named this property “NameOfMatchingFile” it just would not work.&nbsp; </p>
<p><strong>Think about pipelined input.</strong> All it takes to tell PowerShell that a parameter should come from the pipeline is prefixing its declaration with<br /><code>&nbsp; [parameter(ValueFromPipeLine= $true)]<font face="Verdana"> </font></code><br />If you find that you are using your new function like this<br /><code>&nbsp; Get-thing | ForEach {Verb-Thing –thing $_}</code> <br />It’s telling you that <code>-thing</code> should take pipeline values. </p>
<p>The <u>pipeline can supply multiple items</u> , so a function may need to be split into <code>Begin{}</code> ,&nbsp; <code>process{}</code> and <code>End {}</code> blocks. (If you don’t specify these the whole function body is treated as an end block and only the last item passed is processed). Eventually the realization dawns that if the example above works, it should be possible have two lines:<br />&nbsp; <code>$t = Get-thing <br />Verb-Thing –thing $t</code>&nbsp;<br />So parameters need to be able to handle arrays – something I’ll come back to further down. </p>
<p>You can do the same thing that <code>Copy-Item</code> was shown doing above: I have another function which is a wrapper for <code>Select-String,</code> its parameters include:<br /><code>&nbsp; [Parameter(ValueFromPipeLine=$true,Mandatory=$true)] <br />&nbsp; $Pattern, <br />&nbsp; [parameter(ValueFromPipelineByPropertyName = $true)][Alias('Fullname','Path')]<br />&nbsp; $Include=@("*.ps1","*.js","*.sql")</code><br />If the function gets passed a string via the pipeline it is the value for the <code>-pattern</code> parameter. If it gets an object containing a property named “Include”, “Fullname” or “path” that <em>property</em> becomes the value for the <code>–include</code> parameter. </p>
<p>Sometimes a function needs to output to a destination based on input: so you can check to see if a parameter is a script block and evaluate it if it is.&nbsp; </p>
<p><strong>Don’t require users to know syntax for things inside your function.</strong> If you are going to write code to do one job and never reuse it then you don’t need to be flexible.<strong> If the code is to be reused, <strong>you need to do a little extra work so users don’t have to. </strong></strong>For example: if something you use needs a fully qualified path to a file, then the function should use <code>Resolve-Path</code> to avoid an error when the user supplies the name of a file in the current directory. <code>Resolve-Path</code> is quite content to resolve multiple items so replacing&nbsp;&nbsp;&nbsp; <br /><code>&nbsp; Do_stuff_with $path <br /></code>with<br /><code>&nbsp; Resolve-Path $path | for-each {Do stuff with $_ }<br /></code>Delivers, at a stroke, support for Wildcards, multiple items passed in $path and relative names . <br />Another example is with WMI, where syntax is based on SQL so the wildcard is “%”, not “*”. Users will assume the wildcard is *. In this case do you say:<br />(a) Users should learn to use “%”&nbsp;&nbsp;&nbsp; <br />(b) My&nbsp; function should include&nbsp;&nbsp; <code>$parameter = $parameter -Replace "*","%"</code>&nbsp; <br />For my demo I showed a function I have named “<code>Get-IndexedItem</code>” which finds things using the Windows index. I wanted to find my best underwater photos- they are tagged “portfolio” and are the only photos I shoot with a Canon camera. My function lets me type<br /><code>&nbsp; Get-IndexedItem cameramaker=can*,tag=portfolio </code><br />Inside the function the search system needs a where condition of <br />&nbsp; “<code>System.Photo.Cameramanufacturer LIKE 'can%'&nbsp; AND System.Keywords = 'portfolio'</code>” <br />Some tools would require me to type the filtering condition in this form, but I don’t want to remember what prefixes are needed and whether the names are “<code>camera Maker</code>” or “<code>camera Manufacturer</code>” and “<code>keyword</code>”, “<code>keywords</code>” or “<code>tag</code>”. Half the time I’ll forget to wrap things in single quotes, or use “*” as I wild card because I forgot this was SQL. And if I have multiple search terms why shouldn’t they be a list not “A and b and C” (there is a write-up coming for how I did this processing. ).</p>
<p><strong>Set sensible defaults.</strong>&nbsp; The talk before mine highlighted some examples of “bad” coding, and showed a function which accepted a computer name. Every time the user runs the command they must specify the computer. Is it valid to assume that most of the time the command will run against the current computer? If so the parameter should default to that. If the tool deals with files is it valid to assume “all files in the current directory” – if the command is delete, probably not, if it displays some aspect of the files, it probably can. <br /><strong>Constants could be Parameter defaults </strong>With computer name example you might write a function which <u>only</u> ran against the current computer. Obviously it is more useful if the computer name is a parameter (with a default) not a constant. In a surprising number of a cases, something you create to do a specific task can carry out a generic task if you change a fixed value in your code into a parameter, which defaults to the former fixed value. </p>
<p><strong>Be flexible about parameter types and other validation </strong>This is a variation on not making the user understand the internals of your function and I have talked about it before, in particular the dangers of people who are trained systems programmers applying the techniques in PowerShell they would use in something like C#: in those languages a function declaration might look like:<br /><code>&nbsp; single Circumference(single radius) {} </code><br />which says <em>Circumference takes a parameter which must have been declared to be a single precision real number and returns a single precision real number.</em> Writing<br /><code>&nbsp;&nbsp; c = Circumference("Hello"); <br /></code>or<code>&nbsp; c = Circumference("42"); </code><br />will cause the compiler to give a “type mismatch” error – &#8220;Hello&#8221; and &#8220;42&#8243; are strings, not single precision real numbers . Similarly <br /><code>System.io.fileinfo f = Circumference(42); </code><br />Is a type mismatch: <code>f</code> is a <code>fileInfo</code> object and we can’t assign a number to it. The compiler picks these things up before the program is ever run, so&nbsp; users don’t see run-time errors. <br />PowerShell isn’t compiled and its <u>Function declarations don’t include a return a type</u>: <code>Get-Item</code>, for example, deals with the file system, certificate stores, the registry etc. so it can return more than a dozen different types: there is no way to know in advance what type of item <code>Get-Item $y</code> will return. If the result is stored in a variable (with&nbsp; <code>$x = Get-item $y )</code>&nbsp; the type of the variable isn’t specified, but defined at runtime. <br />Trying to translate that declaration into PowerShell gives something like this.<br /><code>&nbsp; Function Get-Circumference{ <br />&nbsp; Param([single]$Radius) <br />&nbsp; $radius * 2 * [system.math]::PI<br />&nbsp; }</code> <br />Calling <br /><code>&nbsp; Get-Circumference "Hello"<br /></code>Produces an error but a closer inspection show it is not a type mismatch: it says<code> <br />Cannot process argument transformation on parameter 'radius'. Cannot convert value "hello" to type "System.Single".<br /></code>the <code>[Single] </code>says “always try to cast $f as a single”.&nbsp; Which means <br /><code>&nbsp; Get-Circumference "42"</code> <br />Will be cast from a string to a single and the function returns 263.89</p>
<p>So you might expect&nbsp; <br /><code>&nbsp; [System.IO.FileInfo]$f = Get-Circumference 42</code><br />To throw an error, but it doesn’t, PowerShell casts 263.893782901543 to an object representing a file with that name in \windows\system32. The file doesn’t exist and is read-only!&nbsp; So <strong>it can be better to resolve types in code. </strong></p>
<p>I’d go further than that. Some validation is redundant because the parameter will be passed to a cmdlet which is more flexible than the function writer thought, in other cases <strong>parameter validation is there to cover up <strong>a programmer’s laziness </strong>. </strong>When I see a web site which demands that I don’t enter spaces in my credit card number I think “Lazy. It’s easy to strip spaces and dashes”. Having “O’Neill” for a surname means that the work of slovenly developers who don’t check their SQL inputs or demand only letters gets drawn to my attention too. If the user is forced to use the equivalent of<br /><code>&nbsp; Stop-Process (get-process Calc)</code><br />they will think “Lazy. you&nbsp; couldn’t be bothered even to provide<strong> </strong>a <code>–name</code> parameter”.&nbsp; <code>Stop-process</code> does just that to cope with process objects, names and IDs (and notice it allows you to pass more than one ID)&nbsp; for example:<br /><code>&nbsp; stop-process [-id ] 4472,5200,5224<br />&nbsp; $p = get-process calc ; Stop-Process -InputObject $p&nbsp; <br />&nbsp; stop-process -name calc</code> </p>
<p>Other cmdlets are able to resolve types without using different parameters for example <br /><code>&nbsp; ren '.\100 Meter Event.txt' "100 Metre Event.txt"<br />&nbsp; $f = get-item '.\100 Metre Event.txt' ; ren $f '100 Meter Event.txt' <br /></code>In one case the first item is a string and the other is a file object: from a user’s point of view this is better than <code>stop-process</code> which in turn is much better than having to get an object representing the process you want to stop. In my talk I used <code>Set-VMMemory </code>from my <a href="http://pshyperv.codeplex.com">Hyper-V module on Codeplex</a>, which has:<br />&nbsp; <code>param(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [parameter(ValueFromPipeLine = $true)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $VM,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Alias("MemoryInBytes")]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [long]$Memory = 0 ,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Server = "."<br />&nbsp; )</code><br />There isn’t a way for me to work out what a user means if they specify a memory size which isn’t a number (and can’t be cast to one). If the user specifies many Virtual Machines, catching something during parameter validation will produce one error instead of one per VM (so this would be the place to trap negative numbers too).<br />I have a -<em>server</em> parameter and it <strong>makes sense to assume</strong> the local machine – but I <strong>can’t make an assumption</strong> about which VM(s) the user might want to modify. The VM can come from the pipeline, and it might be an object which represents a VM, or a string with a VM name (possibly a wildcard) or an array of VM objects and/or Names. If the user says <br /><code>&nbsp; Set-VMMemory –memory 1GB –vm london* –server h1,h2</code><br />the function, not the user, translates that into the necessary objects. If this doesn’t match any VMs I don’t want things to break. (Though I might produce a warning). It takes 4 lines to support all of this <br />&nbsp;&nbsp; <code>if ($VM -is [String]) { $VM = Get-VM -Name $VM -Server $Server}<br />&nbsp; if ($VM.count -gt 1 )&nbsp; {[Void]$PSBoundParameters.Remove("VM") <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $VM | ForEach-object {Set-VMMemory -VM $_ @PSBoundParameters}}<br />&nbsp; if ($vm.__CLASS -eq 'Msvm_ComputerSystem') {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <em>do the main part of the code&nbsp; <br /></em>&nbsp; }</code>&nbsp; <br />Incidentally I use the __CLASS property because it works with remoting when other methods for checking a WMI type failed. </p>
<p><strong>Allow parts to be switched off </strong>In the talk I explained I have a script which applies new builds of software to a remote host. It does a backup and creates a roll back script. Sometimes I have to roll back, produce another build and then apply that. Since I have an up to date backup I don’t need to run the backup a second time so that part of the code runs unless I specify a –nobackup switch. </p>
<p><strong>Support –whatif&nbsp; or restore data. You choose. </strong>A function can use <code>$pscmdlet.shouldProcess </code>– which returns true if the function should proceed into a danger zone and false if it shouldn’t. It only takes one line&nbsp; line before declaring the parameters at the start of a function to enable this<br /><code>&nbsp; [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='High' )] <br /></code>There are 4 confirmation levels “None”, “Low”, “Medium,”, “High”, and for the Confirm impact value and the $Confirmation preference variable. If either is set to “None” no confirmation appears. If the impact level is higher or equal to the preference setting this line <br /><code>&nbsp; if ($pscmdlet.shouldProcess($file.name,”Delete file”) {Remove-item $file} <br /></code>Will delete the file or not depending on the users response to a prompt – in this case “Performing operation &#8220;Delete File&#8221; on Target &#8220;filename&#8221;. Confirmation can be forced on by specifying –<code>confirm. </code>Specifying <code>–whatif </code>will echo the message but return <em>false</em>. If no confirmation is needed, <code>&nbsp; <br />–verbose</code> will echo the message but return true. </p>
<p><strong>Prepare your code for other people, including you.&nbsp; </strong>I’m not the person I was a year ago. You’re not either, and we’ll be different people in 3 or 6 months. So even if you are not publishing your work are writing for someone else. You, in 3 months time. You under-pressure at 3 in the morning. And that you will curse the you of today if leave your code in a mess. </p>
<p>There are many ways to format your code: find a style of indenting that works for you, if it does, then the chances that anyone else will like it are about the same as the chance they will hate it.&nbsp; Some people like to play “PowerShell Golf” where fewest [key]strokes wins – this is fine for the command prompt. Expanding things to make them easier to read is generally good. That’s not an absolute ban on aliases – using Sort and Where instead of Sort-Object and Where-Object may help readability – the key is to break up very long lines, but without creating so many short lines that you are constantly scrolling up and down. </p>
<p>Everyone says “Put in brief comments”, but I’d refine that: explain WHY not WHAT, for example&nbsp; I only decided to use <code>Set-VMMemory </code>as an example in my talk at the last moment.&nbsp; It has a line<br /><code>&nbsp; if (-not ($memory % 2mb))&nbsp; {$memory /= 1mb}</p>
<p></code>
<p>I can’t have looked code in 8 months. Why would I do that ? Fortunately I’ve put in WHY comment – the WHAT is self explanatory<br /><code># The API takes the amount of memory in MB, in multiples of 2MB. <br /># Assume that anything less than 2097152 is in already MB (we aren't assigning 2TB to the VM). If user enters 1024MB or 0.5GB divide by 1MB&nbsp; </code>&gt;</p>
<p>Instead of putting comments next to each parameter saying what it does, move the comments into comment based help, take a couple of places where you have used / tested the command and put them into the comment based help as examples. Your future self will thank you for it. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1776&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/10/24/maximize-the-reuse-of-your-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2011/10/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>How to be Creative with QR Codes</title>
		<link>http://jamesone111.wordpress.com/2011/10/17/how-to-be-creative-with-qr-codes/</link>
		<comments>http://jamesone111.wordpress.com/2011/10/17/how-to-be-creative-with-qr-codes/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 13:15:46 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/2011/10/17/how-to-be-creative-with-qr-codes/</guid>
		<description><![CDATA[I’ve been playing with QR codes recently, and have started to use them . HELLO if you’ve been at The Experts Conference in Frankfurt and scanned one of from one of my sessions the files form these are on My Skydrive&#160; I looked at a number of different code libraries which would build a QR [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1773&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I’ve been playing with QR codes recently, and have started to use them . HELLO if you’ve been at The Experts Conference in Frankfurt and scanned one of from one of my sessions the files form these are on <a title="https://skydrive.live.com/?cid=1efe2682bfbbd817&amp;sc=documents#cid=1EFE2682BFBBD817&amp;id=1EFE2682BFBBD817%21133&amp;sc=documents" href="https://skydrive.live.com/?cid=1efe2682bfbbd817&amp;sc=documents#cid=1EFE2682BFBBD817&amp;id=1EFE2682BFBBD817%21133&amp;sc=documents">My Skydrive</a>&nbsp;</p>
<p>I looked at a number of different code libraries which would build a QR code for me but the easiest way turned out to be to use a web service, provided by Google.&nbsp; And I wrapped this up in a little PowerShell </p>
<p><code><span style="color:#008080;">[System.Reflection.Assembly]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">LoadWithPartialName</span><span style="color:#000000;">(</span><span style="color:#8b0000;">”System.Web"</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">|</span> <span style="color:#0000ff;">Out-Null</span><br /><span style="color:#00008b;">Function</span> <span style="color:#8a2be2;">get-qrcode</span> <span style="color:#000000;">{</span><br /><span style="color:#00008b;">param</span> <span style="color:#000000;">(</span><span style="color:#a9a9a9;">[</span><span style="color:#00bfff;">parameter</span><span style="color:#000000;">(</span><span style="color:#000000;">ValueFromPipeLine</span><span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$true</span><span style="color:#a9a9a9;">,</span> <span style="color:#000000;">mandatory</span><span style="color:#a9a9a9;">=</span><span style="color:#ff4500;">$true</span><span style="color:#000000;">)</span><span style="color:#a9a9a9;">]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:#ff4500;">$Text</span><span style="color:#a9a9a9;">,</span><br /><span style="color:#ff4500;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $path</span> <span style="color:#a9a9a9;">=</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">join-path</span> <span style="color:#ff4500;">$pwd</span> <span style="color:#8b0000;">"QRCode.PNG"</span><span style="color:#000000;">)</span><br /><span style="color:#000000;">)</span><br /><span style="color:#00008b;">&nbsp; if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$WebClient</span> <span style="color:#a9a9a9;">-eq</span> <span style="color:#ff4500;">$null</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#ff4500;">$Global:WebClient</span><span style="color:#a9a9a9;">=</span><span style="color:#0000ff;">new-object</span> <span style="color:#8a2be2;">System.Net.WebClient</span> <span style="color:#000000;">}</span><br /><span style="color:#ff4500;">&nbsp; $WebClient</span><span style="color:#a9a9a9;">.</span><span style="color:#000000;">DownloadFile</span><span style="color:#000000;">(</span><span style="color:#000000;">(</span><span style="color:#8b0000;">"http://chart.apis.google.com/chart?cht=qr&amp;chs=547x547&amp;chld=H|0&amp;chl="</span> <span style="color:#a9a9a9;">+</span><span style="color:#008080;">&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [System.Web.HttpUtility]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">UrlEncode</span><span style="color:#000000;">(</span><span style="color:#ff4500;">$Text</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#a9a9a9;">,</span> <span style="color:#ff4500;">$path</span><span style="color:#000000;">)</span> <br /><span style="color:#0000ff;">&nbsp;&nbsp; Start-Process</span> <span style="color:#ff4500;">$path</span><br /><span style="color:#000000;">}</span></code>
<p>So it takes two parameters, a block of text and a path where the file should be saved. The text must be specified, but there is a default for </p>
<p>To save having to the create lots of web client objects, I keep a web client object , then one line of powershell gets a PNG file from the Google Service and saves it to the path.&nbsp; Finally I launch the file in the default viewer.</p>
<p>The next step is to use some image tools to pretty up the QR code. I still use an ancient version of PaintShop pro and that does the job here nicely. </p>
<p><a href="http://jamesone111.files.wordpress.com/2011/10/qr-1.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;padding-top:0;" title="qr-1" border="0" alt="qr-1" src="http://jamesone111.files.wordpress.com/2011/10/qr-1_thumb.jpg?w=838&#038;h=278" width="838" height="278"></a></p>
<p>On the left is the original QR code, in the middle I have applied some gaussian blur to the image and on the right I have reduced this to two colours, 100% black and 100% white. This smoothes off the edges. Then I take the image back to 16 million colours and add some colour. </p>
<p>&nbsp;</p>
<p><a href="http://jamesone111.files.wordpress.com/2011/10/qr-2.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:block;float:none;margin-left:auto;border-top:0;margin-right:auto;border-right:0;padding-top:0;" title="qr-2" border="0" alt="qr-2" src="http://jamesone111.files.wordpress.com/2011/10/qr-2_thumb.jpg?w=558&#038;h=278" width="558" height="278"></a></p>
<p>One of the really nice things about QR codes is that they have error correction built in (and in my call to the web service I specify the maximum amount) this means we put something which isn’t part of the code into the picture. I’ve used the Frankfurt skyline from the slide template here, but this is scope for creativity. </p>
<p>Of course there is nothing which says the data in the code must be a URL. Here’s a message for anyone who has got a reader. The file name and the Ferrari might be a clue to what it says. </p>
<p align="center"><a href="http://jamesone111.files.wordpress.com/2011/10/qr-ferris.jpg"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="QR-Ferris" border="0" alt="QR-Ferris" src="http://jamesone111.files.wordpress.com/2011/10/qr-ferris_thumb.jpg?w=500&#038;h=500" width="500" height="500"></a></p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/uncategorized/'>Uncategorized</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1773&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/10/17/how-to-be-creative-with-qr-codes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2011/10/qr-1_thumb.jpg" medium="image">
			<media:title type="html">qr-1</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2011/10/qr-2_thumb.jpg" medium="image">
			<media:title type="html">qr-2</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2011/10/qr-ferris_thumb.jpg" medium="image">
			<media:title type="html">QR-Ferris</media:title>
		</media:content>
	</item>
		<item>
		<title>Adding $Clipboard automatic variable support to PowerShell</title>
		<link>http://jamesone111.wordpress.com/2011/09/11/adding-clipboard-automatic-variable-support-to-powershell/</link>
		<comments>http://jamesone111.wordpress.com/2011/09/11/adding-clipboard-automatic-variable-support-to-powershell/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 17:08:42 +0000</pubDate>
		<dc:creator>jamesone111</dc:creator>
				<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">https://jamesone111.wordpress.com/2011/09/11/adding-clipboard-automatic-variable-support-to-powershell/</guid>
		<description><![CDATA[Some of the bits and pieces I have been working on recently have needed PowerShell to take input from the windows clipboard, and as with so many things in PowerShell there is more than one way this can be done. If it is something short paste into a command line –wrapping in quotes or @” [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1765&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Some of the bits and pieces I have been working on recently have needed PowerShell to take input from the windows clipboard, and as with so many things in PowerShell there is more than one way this can be done. </p>
<ul>
<li>If it is something short paste into a command line –wrapping in quotes or @” “@ as required
<li>Paste into a new edit window in the PowerShell ISE and wrap it with $variableName = “” (or @” “@ as required) , then run the contents of the Window
<li>Use&nbsp; [windows.clipboard]::GetText() in a command line
<li>Create a Get-Clipboard Function which wraps&nbsp; [windows.clipboard]::GetText()
<li>Create a $Clipboard “automatic variable”
<li>Use the <a href="http://huddledmasses.org/clipexe-and-the-missing-pasteexe/">Paste.Exe which Joel has on his site.</a></li>
</ul>
<p>Note (a) that if you want to use&nbsp; [windows.clipboard]::GetText() in the “Shell” version of PowerShell you need to start PowerShell.exe with the –STA switch and run the command&nbsp;&nbsp; Add-Type -AssemblyName (&#8220;Presentationcore&#8221;) – I’m actually only interested in doing this in the ISE. <br />and (b) If you want to send PowerShell’s output to the clipboard the command line Clip.exe has been Windows for several versions. It’s getting text from the clipboard which needs more work. </p>
<p>There’s very little to choose between having Get-Clipboard and $clipboard but having just read <a href="http://robertrobelo.wordpress.com/2011/06/24/tied-variables-a-simpler-way/">Robert’s piece on <strong>Tied Variables</strong></a> I thought I would go down that path. </p>
<p>Robert explained that you can set a <strong>breakpoint </strong>on a PowerShell variable to run a <strong>scriptblock</strong> when the<strong> variable is read, </strong>so he got to thinking “I can run something which <strong>changes the value of variable, </strong>when it is read. </p>
<p>In my profile I have </p>
<p><code><span style="color:#ff4500;"><font color="#00008b"></font>$Global:MyBreakPoint</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Global:Clipboard</span> <span style="color:#a9a9a9;">=</span> <span style="color:#0000ff;">Set-PSBreakpoint</span> <span style="color:#000080;">-Variable</span> <span style="color:#8a2be2;">Clipboard</span> <span style="color:#000080;">-Mode</span> <span style="color:#8a2be2;">Read</span> <span style="color:#000080;">-Action</span> <span style="color:#000000;">{</span><br /><span style="color:#0000ff;">Set-Variable</span> <span style="color:#8a2be2;">clipboard</span> <span style="color:#000000;">(</span><span style="color:#008080;">[Windows.clipboard]</span><span style="color:#a9a9a9;">::</span><span style="color:#000000;">GetText</span><span style="color:#000000;">(</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <span style="color:#000080;">-Option</span> <span style="color:#8a2be2;">ReadOnly</span><span style="color:#a9a9a9;">,</span> <span style="color:#8a2be2;">AllScope</span> <span style="color:#000080;">-Scope</span> <span style="color:#8a2be2;">Global</span> <span style="color:#000080;">-Force</span><span style="color:#000000;">}</span></code>
<p>So <code>$clipboard</code> is set to the breakpoint which is created by monitoring read action on itself. At each subsequent read the script-block is called and uses <code>set-variable</code> to make <code>$clipboard</code> a ReadOnly variable holding the value of the clipboard at that moment.</p>
<p>In the I modified my <code>Get-SQL</code> command to take advantage of this. I added a <code>–Paste</code> switch (I chose –paste over –clipboard simply because I already had –connection so –c would be ambiguous –paste allows me to run <code>sql –p</code> , taking advantage of SQL being an automatic alias for Get-SQL) then </p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$Paste</span> <span style="color:#a9a9a9;">-and</span> <span style="color:#000000;">(</span><span style="color:#0000ff;">test-path</span> <span style="color:#8b0000;">'Variable:\Clipboard'</span><span style="color:#000000;">)</span><span style="color:#000000;">)</span> <br /><span style="color:#000000;">&nbsp;&nbsp;&nbsp; {</span><span style="color:#ff4500;">$sql</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ff4500;">$Clipboard</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">".*(?=select|update|delete)"</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">""</span> <span style="color:#a9a9a9;">-replace</span> <span style="color:#8b0000;">"[\n|\r]+"</span><span style="color:#a9a9a9;">,</span><span style="color:#8b0000;">""</span> <span style="color:#000000;">}</span><br /></code>
<p>This trims out anything before “Select”, “update” or “Delete” –as I am usually copying a statement from a log with a time stamp at the start, and it also replaces any carriage return/line feed characters. </p>
<h2>WHAT ? A bug in the PowerShell ISE</h2>
<p><a href="http://jamesone111.files.wordpress.com/2011/09/image.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:left;padding-top:0;border-width:0;" title="image" border="0" alt="image" align="left" src="http://jamesone111.files.wordpress.com/2011/09/image_thumb.png?w=244&#038;h=90" width="244" height="90"></a>This was all working brilliantly until I went to use a function which uses the PowerShellISE object model to manipulate text in an edit window it fails – the error implies that PowerShell’s object model thinks a debugger is running if a break point exists, even though trying to edit the script files interactively is not a problem . </p>
<p>To get around this I set TWO variables to point to the break point, one was $clipboard, which will be overwritten as soon as it is used, and the other is $myBreakPoint. In my edit function I now start with </p>
<p><code><span style="color:#00008b;">if</span> <span style="color:#000000;">(</span><span style="color:#ff4500;">$MyBreakPoint</span><span style="color:#000000;">)</span> <span style="color:#000000;">{</span><span style="color:#0000ff;">Remove-PSBreakpoint</span> <span style="color:#ff4500;">$MyBreakPoint</span> <span style="color:#000000;">}</span> </code></p>
<p>Removing the breakpoint doesn’t remove the variable $myBreakpoint so on the way out of the function I use it to decide if I need to reinstate it. </p>
<br />Filed under: <a href='http://jamesone111.wordpress.com/category/powershell/'>Powershell</a>  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jamesone111.wordpress.com&#038;blog=15763423&#038;post=1765&#038;subd=jamesone111&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jamesone111.wordpress.com/2011/09/11/adding-clipboard-automatic-variable-support-to-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/256a9fa9cbef2fc1e3230fb3797360b9?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">jamesone111</media:title>
		</media:content>

		<media:content url="http://jamesone111.files.wordpress.com/2011/09/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
	</channel>
</rss>
