
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lead, Follow, or Move &#187; XML</title>
	<atom:link href="http://www.leadfollowmove.com/archives/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://www.leadfollowmove.com</link>
	<description>Adam Bell on Deployment, Automation, PowerShell et al</description>
	<lastBuildDate>Thu, 22 Apr 2010 14:34:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Listing your XML product keys from MSDN &#8211; Part 2</title>
		<link>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn-part-2</link>
		<comments>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn-part-2#comments</comments>
		<pubDate>Thu, 11 Oct 2007 11:14:41 +0000</pubDate>
		<dc:creator>Adam Bell</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[MSDN]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn-part-2</guid>
		<description><![CDATA[In the last post we we&#8217;re playing around with how to extract a Product Key from an MSDN exported XML document. We&#8217;ll take this a step further today and see if we turn it into something a little more useful. Building on the previous script, we want to list the products that match our search [...]]]></description>
			<content:encoded><![CDATA[<p>In the last post we we&#8217;re playing around with how to extract a Product Key from an MSDN exported XML document. We&#8217;ll take this a step further today and see if we turn it into something a little more useful.</p>
<p>Building on the previous script, we want to list the products that match our search criteria and then select the one we want the Product Key for. As well as displaying this information on screen, it would be useful if the key could be put into the clipboard, ready for use.</p>
<div class="quickcodenoclick"><code><br />
$msdn = Get-Content MSDN-Sample-ProdKeys.xml<br />
$msdn.Your_Product_Keys.Product_Key.Count<br />
# Initialise the Hash Table<br />
$lookup = @{}<br />
&nbsp;<br />
$product = read-host &quot;what product are you using?&quot;<br />
$i = 0<br />
foreach ($item in $msdn.Your_Product_Keys.Product_Key)<br />
{<br />
&nbsp;&nbsp;if ($item.name -like &quot;*$product*&quot;)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$lookup = $lookup + @{$i=$item.name}<br />
&nbsp;&nbsp;&nbsp;&nbsp;write-host $i &quot;:&quot; $item.name<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;$i ++<br />
}<br />
$ID = read-host &quot;Please enter the number of the product you want&quot;<br />
$msdn.Your_Product_Keys.Product_Key[$ID].Key | clip<br />
write-host ($msdn.Your_Product_Keys.Product_Key[$ID].Name) &quot;(&quot;($msdn.Your_Product_Keys.Product_Key[$ID].Key)&quot;)&quot; -foregroundcolor &quot;cyan&quot;<br />
</code></div>
<p>The first thing, was to change where we increment the <em>$i</em> variable. I found during testing that we were returning the incorrect number to retrieve the details from the XML document. Incrementing at the end of the <em>foreach</em> corrected this oversight. We&#8217;ve also removed displaying the Product Key until the end of the script, to make things neater.</p>
<p>The main new feature has been to use an <a title="Wikipedia: PowerShell Associative Array" href="http://http://en.wikipedia.org/wiki/Associative_array#Windows_PowerShell">associative array</a>, or hash table,  to store our ID number and <em>Product Name</em>. The ID number identifies which node in the XML document our product is stored in.</p>
<p>Once, the hash table has been built with a list of potential products, the user is prompted to enter the ID number of the specific product.  The ID is then used to lookup the Product&#8217;s Name and Key form the XML document, which is then displayed on screen and put in the clipboard buffer by using the clip.exe utility available on Windows 2003 Server and Vista.</p>
<p><a href='http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn-part-2/attachment/show-msdnkeyps1-part-2-5/' rel='attachment wp-att-190' title='show-msdnkey.ps1 Part 2'><img src='http://www.leadfollowmove.com/wp-content/uploads/show-msdnkey21.thumbnail.png' alt='show-msdnkey.ps1 Part 2' /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn-part-2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Listing your XML product keys from MSDN</title>
		<link>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn</link>
		<comments>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn#comments</comments>
		<pubDate>Tue, 09 Oct 2007 19:27:26 +0000</pubDate>
		<dc:creator>Adam Bell</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[MSDN]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn</guid>
		<description><![CDATA[I&#8217;m lucky enough to have an MSDN subscription at work. If you&#8217;re familiar with this service, you will know that when you request a Product Key, there is an option to download it in XML format. One of my favourite things with PowerShell, is how it handles XML. $msdn = Get-Content MSDN-Sample-ProdKeys.xml $msdn.Your_Product_Keys.Product_Key.Count &#160; $product [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m lucky enough to have an MSDN subscription at work. If you&#8217;re familiar with this service, you will know that when you request a Product Key, there is an option to download it in XML format.<br />
<a href='http://www.leadfollowmove.com/wp-content/uploads/sample-msdn-xml.PNG' title='Sample MSDN Product Key Export to XML'><img src='http://www.leadfollowmove.com/wp-content/uploads/sample-msdn-xml.thumbnail.PNG' alt='Sample MSDN Product Key Export to XML' /></a></p>
<p>One of my favourite things with PowerShell, is how it handles XML.</p>
<div class="quickcodenoclick"><code><br />
$msdn = Get-Content MSDN-Sample-ProdKeys.xml<br />
$msdn.Your_Product_Keys.Product_Key.Count<br />
&nbsp;<br />
$product = read-host &quot;what product are you using?&quot;<br />
$i = 0<br />
foreach ($item in $msdn.Your_Product_Keys.Product_Key)<br />
{<br />
&nbsp;&nbsp;$i ++<br />
&nbsp;&nbsp;if ($item.name -like &quot;*$product*&quot;)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;write-host $i &quot;:&quot; $item.name &quot;(&quot; $item.key &quot;)&quot;<br />
&nbsp;&nbsp;}<br />
}<br />
</code></div>
<p>A quick look through the code shows how simple it really is. we define <em>$msdn</em> as an XML type and get the contents of our MSDN exported keys. As a quick sanity check we output how many nodes are under <em>Product_Key</em>.</p>
<p>We then prompt the user for some guidance on what product is of interest. Looping through the nodes any item that is <em>like</em> our supplied product is returned, along with the node number, and product key.<br />
<a href='http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn/attachment/show-msdnkeyps1/' rel='attachment wp-att-184' title='show-msdnkey.ps1'><img src='http://www.leadfollowmove.com/wp-content/uploads/show-msdnkey.thumbnail.PNG' alt='show-msdnkey.ps1' /></a></p>
<p>As you can see in the example above, the first time the script is run we provided no input, which returned us all the contents of the file. The second time we searched on the term <em>xp</em>.</p>
<p>If you&#8217;re wondering why we kept track of the node number, we&#8217;ll expand on this example next time to see if we can improve on the script to make it a bit more useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leadfollowmove.com/archives/powershell/listing-your-xml-product-keys-from-msdn/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PowerShell and XML</title>
		<link>http://www.leadfollowmove.com/archives/powershell/powershell-and-xml</link>
		<comments>http://www.leadfollowmove.com/archives/powershell/powershell-and-xml#comments</comments>
		<pubDate>Thu, 01 Mar 2007 16:56:38 +0000</pubDate>
		<dc:creator>Adam Bell</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.leadfollowmove.com/?p=53</guid>
		<description><![CDATA[By far, for me, the most useful feature I&#8217;ve found in PowerShell is how easy it is to manipulate XML documents. I&#8217;ve written VBScripts in the past that extracted data from XML, performed a task, and then updated the XML document when it was done. Now, personally I found this quite a tricky task. Let [...]]]></description>
			<content:encoded><![CDATA[<p>By far, for me, the most useful feature I&#8217;ve found in PowerShell is how easy it is to manipulate XML documents. I&#8217;ve written VBScripts in the past that extracted data from XML, performed a task, and then updated the XML document when it was done. </p>
<p>Now, personally I found this quite a tricky task. Let me give you an example, we have the following XML document:</p>
<div class="quickcodenoclick"><code><br />
&lt;section name=&quot;build&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;Package ID=&quot;W2K3&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PartitionType&gt;A&lt;/PartitionType&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/package&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;package ID=&quot;XPSP2&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PartitionType&gt;B&lt;/PartitionType&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/package&gt;<br />
&lt;/section&gt;<br />
</code></div>
<p>The aim is to extract the PartitionType for the W2K3 package.</p>
<p>In PowerShell, this is really easy:</p>
<div class="quickcodenoclick"><code><br />
$payload = get-content -path d:\coding\ps\payload.xml<br />
foreach ($package in $payload.section.package)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;if ($package.id -eq &quot;w2k3&quot;)<br />
&nbsp;&nbsp; {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;write-host $package.partitiontype<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}<br />
</code></div>
<p>As you can see it&#8217;s easy to navigate to attributes or nodes within the XML document. So what happens if you want to add another package?</p>
<div class="quickcodenoclick"><code><br />
$newPkg = $payload.CreateElement(&quot;Package&quot;)<br />
$newPkg.SetAttribute(&quot;ID&quot;, &quot;Vista&quot;)<br />
</code></div>
<p>Now the only really hassle I&#8217;ve experienced when trying to add new nodes, is when it&#8217;s the first node at that level. Adrian Milliner over at <a title="doing inappropriate things with powershell" href="http://ps1.soapyfrog.com/">Soapy Frog</a> provided a work around for this (bug?) in the comments of the <a title="Adding elements to XML objects with Windows PowerShell" href="http://blogs.msdn.com/powershell/search.aspx?q=xml&#038;p=1">Microsoft PowerShell Team blog</a>.</p>
<p>With this in mind I use the following the syntax to add the node:</p>
<div class="quickcodenoclick"><code><br />
$payload[&quot;section&quot;].AppendChild($newPkg)<br />
</code></div>
<p>Now we have the new <em>package</em> node, we still need to add the child node under it for the <em>partitiontype</em>.</p>
<div class="quickcodenoclick"><code><br />
$partition = $payload.CreateElement(&quot;partitiontype&quot;)<br />
$partition.set_InnerText(&quot;C&quot;)<br />
&nbsp;<br />
$nodecount = $payload.section.package.count<br />
if ($nodecount -ne $null)<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;$I = ($nodecount -1)<br />
&nbsp;&nbsp;&nbsp;&nbsp;$payload.section.package[$I].AppendChild($partition)<br />
}<br />
$payload.save(&quot;D:\coding\ps\payload.xml&quot;)<br />
</code></div>
<p>So now we&#8217;ve covered some basic XML manipulations. Our end result in this example woud be:</p>
<div class="quickcodenoclick"><code><br />
&lt;section name=&quot;build&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;Package ID=&quot;W2K3&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PartitionType&gt;A&lt;/PartitionType&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/package&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;package ID=&quot;XPSP2&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PartitionType&gt;B&lt;/PartitionType&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/package&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;package ID=&quot;Vista&quot;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;PartitionType&gt;C&lt;/PartitionType&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/package&gt;<br />
&lt;/section&gt;<br />
</code></div>
<p>If we take some of our earlier AD examples you can see how easy it would be to add in nodes for ou&#8217;s, groups, and accounts and it suddenly looks a whole lot easier to build an Active Directory ;)</p>
<p>Of course one application could be to write some script to read your live AD structure, save it into an XML document, and then use it to build Active Directory for a test environment. Though this would take some effort it has some excellent portability advantages. If you&#8217;re careful on your coding you can write your scripts so that the domain name isn&#8217;t hard-coded.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.leadfollowmove.com/archives/powershell/powershell-and-xml/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
