<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>richmac</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/richmac/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/richmac/feed.xml</link>
    <description>richmac&amp;#039;s random musings</description>
    <lastBuildDate>Wed, 17 Apr 2019 04:03:27 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Creating a QR Code Reader App for Windows Phone 7</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/creating-a-qr-code-reader-app-for-windows-phone-7</link>
      <pubDate>Mon, 07 Nov 2011 07:44:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/11/07/creating-a-qr-code-reader-app-for-windows-phone-7/</guid>
      <description><![CDATA[If you’ve updated Windows Phone 7.5, you might have noticed that it has some nice built-in...]]></description>
      <content:encoded><![CDATA[<p>If you’ve updated Windows Phone 7.5, you might have noticed that it has some nice built-in functionality for reading QR Codes using Bing Vision search.&nbsp; After trying it out for a while, I pretty quickly started wondering how I could use this in an app, and equally quickly I was disappointed to find that there are no APIs available for 3rd party developers to access this feature.</p>
<p>After a short hunt online, I found an excellent library that can be used as an alternative, and I built a simple reader with it.&nbsp; Here are the basic steps to create one yourself:</p>
<p>&nbsp;</p>
<blockquote>
<p><em><strong>NOTE</strong>: To build and test this example you must use a physical phone rather than the emulator, as you will need access to the camera feed.</em>&nbsp;</p>
<p>&nbsp;</p>
</blockquote>
<p><strong>Create a new Windows Phone Project in Visual Studio</strong></p>
<blockquote>
<p>I used <em>Visual Studio Ultimate</em>, but the <em>Express</em> version that ships with the Windows Phone SDK will do just fine.&nbsp; Use the basic phone template for this example:</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/2671.image_74C55E60.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/2671.image_5F00_74C55E60.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/5415.image_thumb_2C2B6F8F.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/5415.image_5F00_thumb_5F00_2C2B6F8F.png" width="457" height="51"></a></p>
</blockquote>
<blockquote>
<p>Be sure to select ‘Windows Phone 7.1’ as the target OS:</p>
</blockquote>
<blockquote>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/5808.image4_26DC88DE.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/5808.image4_5F00_26DC88DE.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/1106.image4_thumb_05E91637.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/1106.image4_5F00_thumb_5F00_05E91637.png" width="359" height="78"></a></p>
</blockquote>
<p>&nbsp;</p>
<p><strong>Update the App Manifest</strong></p>
<blockquote>
<p>If you plan to submit a camera-based app to the marketplace, then you must ensure that the WMAppManifest.xaml contains this capability element:</p>
<div class="csharpcode">
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Capability</span> <span style="color: rgb(255, 0, 0);">Name</span><span style="color: rgb(0, 0, 255);">="ID_CAP_ISV_CAMERA"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--></div></div>
</div>
</blockquote>
<blockquote>
<p>A different element is required for the front-facing camera: ID_HW_FRONTCAMERA</p>
</blockquote>
<p><strong>Create the QR Reader Window and Output Area</strong></p>
<blockquote>
<p>Inside the content Grid in <em>MainPage.xaml</em>, add a <em>Rectangle</em> and a <em>TextBlock</em>:</p>
<p>&nbsp;</p>
<div class="csharpcode">
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 128, 0);">&lt;!--TitlePanel contains the name of the application and page title--&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">StackPanel</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="TitlePanel"</span> <span style="color: rgb(255, 0, 0);">Grid</span>.<span style="color: rgb(255, 0, 0);">Row</span><span style="color: rgb(0, 0, 255);">="0"</span> <span style="color: rgb(255, 0, 0);">Margin</span><span style="color: rgb(0, 0, 255);">="12,17,0,28"</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">TextBlock</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="ApplicationTitle"</span> <span style="color: rgb(255, 0, 0);">Text</span><span style="color: rgb(0, 0, 255);">="QR SCANNER"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                <span style="color: rgb(255, 0, 0);">Style</span><span style="color: rgb(0, 0, 255);">="{StaticResource PhoneTextNormalStyle}"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">TextBlock</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="PageTitle"</span> <span style="color: rgb(255, 0, 0);">Text</span><span style="color: rgb(0, 0, 255);">="scanner"</span> <span style="color: rgb(255, 0, 0);">Margin</span><span style="color: rgb(0, 0, 255);">="9,-7,0,0"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                <span style="color: rgb(255, 0, 0);">Style</span><span style="color: rgb(0, 0, 255);">="{StaticResource PhoneTextTitle1Style}"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">StackPanel</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 128, 0);">&lt;!--ContentPanel - place additional content here--&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Grid</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="ContentPanel"</span> <span style="color: rgb(255, 0, 0);">Grid</span>.<span style="color: rgb(255, 0, 0);">Row</span><span style="color: rgb(0, 0, 255);">="1"</span> <span style="color: rgb(255, 0, 0);">Margin</span><span style="color: rgb(0, 0, 255);">="12,0,12,0"</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 128, 0);">&lt;!-- rectangle which acts as the 'camera window' --&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Rectangle</span> <span style="color: rgb(255, 0, 0);">Height</span><span style="color: rgb(0, 0, 255);">="300"</span> <span style="color: rgb(255, 0, 0);">HorizontalAlignment</span><span style="color: rgb(0, 0, 255);">="Left"</span> <span style="color: rgb(255, 0, 0);">Margin</span><span style="color: rgb(0, 0, 255);">="82,61,0,0"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                <span style="color: rgb(255, 0, 0);">Name</span><span style="color: rgb(0, 0, 255);">="videoRectangle"</span> <span style="color: rgb(255, 0, 0);">Stroke</span><span style="color: rgb(0, 0, 255);">="White"</span> <span style="color: rgb(255, 0, 0);">StrokeThickness</span><span style="color: rgb(0, 0, 255);">="5"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                <span style="color: rgb(255, 0, 0);">VerticalAlignment</span><span style="color: rgb(0, 0, 255);">="Top"</span> <span style="color: rgb(255, 0, 0);">Width</span><span style="color: rgb(0, 0, 255);">="300"</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Rectangle.RenderTransform</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">RotateTransform</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="videoRotateTransform"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">Rectangle.RenderTransform</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">Rectangle.Fill</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">VideoBrush</span> <span style="color: rgb(255, 0, 0);">x:Name</span><span style="color: rgb(0, 0, 255);">="viewfinderBrush"</span><span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">Rectangle.Fill</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">Rectangle</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 128, 0);">&lt;!-- Textblock which will hold the output of the QR Code --&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">&lt;</span><span style="color: rgb(128, 0, 0);">TextBlock</span> <span style="color: rgb(255, 0, 0);">Height</span><span style="color: rgb(0, 0, 255);">="171"</span> <span style="color: rgb(255, 0, 0);">HorizontalAlignment</span><span style="color: rgb(0, 0, 255);">="Left"</span> <span style="color: rgb(255, 0, 0);">Margin</span><span style="color: rgb(0, 0, 255);">="31,406,0,0"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                <span style="color: rgb(255, 0, 0);">Name</span><span style="color: rgb(0, 0, 255);">="resultText"</span> <span style="color: rgb(255, 0, 0);">Text</span><span style="color: rgb(0, 0, 255);">="&amp;lt;nothing to display&amp;gt;"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                <span style="color: rgb(255, 0, 0);">VerticalAlignment</span><span style="color: rgb(0, 0, 255);">="Top"</span> <span style="color: rgb(255, 0, 0);">Width</span><span style="color: rgb(0, 0, 255);">="399"</span> <span style="color: rgb(255, 0, 0);">TextWrapping</span><span style="color: rgb(0, 0, 255);">="Wrap"</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                <span style="color: rgb(255, 0, 0);">TextAlignment</span><span style="color: rgb(0, 0, 255);">="Center"</span> <span style="color: rgb(0, 0, 255);">/&gt;</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">&lt;/</span><span style="color: rgb(128, 0, 0);">Grid</span><span style="color: rgb(0, 0, 255);">&gt;</span></pre><!--CRLF--></div></div>
</div>
</blockquote>
<blockquote>
<p>&nbsp;</p>
<p>This should result in a main page like this:</p>
</blockquote>
<blockquote>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/2262.scanner_76D2375C.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/2262.scanner_5F00_76D2375C.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="scanner" border="0" alt="scanner" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/4745.scanner_thumb_27853F08.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/4745.scanner_5F00_thumb_5F00_27853F08.png" width="261" height="435"></a></p>
</blockquote>
<p>&nbsp;</p>
<blockquote>
<p>The <em>Rectangle</em> will act as the camera viewer – notice that it has a <em>VideoBrush</em> as its fill;&nbsp; this will eventually contain the live feed from the camera.&nbsp; The <em>TextBlock</em> will display the content read from the QR Code.</p>
</blockquote>

<p><strong>Directives and Variables</strong></p>
<blockquote>
<p>In <em>MainPage.xaml.cs</em>, add a few relevant using statements, such as for the <em>Devices</em> namespace so we can use the <em>PhotoCamera</em> type:</p>
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">using</span> Microsoft.Devices;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">using</span> System.IO;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">using</span> System.Windows.Media.Imaging;</pre><!--CRLF--></div></div><div class="csharpcode">
<pre class="alt">&nbsp;</pre>
</div>
<p>Declare some variables inside the main app class:</p>
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">private</span> PhotoCamera camera;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">bool</span> capturing = <span style="color: rgb(0, 0, 255);">false</span>;</pre><!--CRLF--></div></div>

<div class="csharpcode">&nbsp;&nbsp;&nbsp; </div>
</blockquote>
<p><strong>Handle Navigation</strong></p>
<blockquote>
<p>It is recommended practice to initialise and dispose of the camera object when we navigate to and from our page, respectively, so we need to override these methods:</p>
<p>&nbsp;</p>
</blockquote>
<blockquote>
<div class="csharpcode">
<pre class="alt">&nbsp;</pre><div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> OnNavigatedTo(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">           System.Windows.Navigation.NavigationEventArgs e)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">{</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (<span style="color: rgb(0, 0, 255);">null</span> == camera)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        camera = <span style="color: rgb(0, 0, 255);">new</span> PhotoCamera();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 128, 0);">// filred when the camera is initialised</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        camera.Initialized += camera_Initialised;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 128, 0);">// fired when the button is fully pressed</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        CameraButtons.ShutterKeyPressed += camera_ButtonFullPress;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 128, 0);">// fired when an image is available.</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        camera.CaptureImageAvailable += camera_CaptureImageAvailable;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 128, 0);">// set the VideoBrush source to the camera output</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        videoRotateTransform.CenterX = videoRectangle.Width / 2;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        videoRotateTransform.CenterY = videoRectangle.Height / 2;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        videoRotateTransform.Angle = 90;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        viewfinderBrush.SetSource(camera);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">base</span>.OnNavigatedTo(e);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 128, 0);">// user navigated away from page</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">protected</span> <span style="color: rgb(0, 0, 255);">override</span> <span style="color: rgb(0, 0, 255);">void</span> OnNavigatedFrom(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">           System.Windows.Navigation.NavigationEventArgs e)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">{</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (camera != <span style="color: rgb(0, 0, 255);">null</span>)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 128, 0);">// unhook the event handlers</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        CameraButtons.ShutterKeyPressed -= camera_ButtonFullPress;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        camera.CaptureImageAvailable -= camera_CaptureImageAvailable;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        camera.Initialized -= camera_Initialised;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        <span style="color: rgb(0, 128, 0);">// dispose of the camera object</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        camera.Dispose();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">base</span>.OnNavigatedFrom(e);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre><!--CRLF--></div></div></div></blockquote>


<blockquote>
<p>&nbsp;</p>
<p>Here the <em>PhotoCamera</em> object is initialised to to use ‘primary’ camera, which is the rear-facing one. If you want to use the front-facing camera in an application, then you can call the alternate constructor like this:</p>
<div class="csharpcode">
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">camera = <span style="color: rgb(0, 0, 255);">new</span> PhotoCamera(CameraType.FrontFacing);</pre><!--CRLF--></div></div>
</div>
</blockquote>
<p>&nbsp;</p>
<p><strong>Add Camera Event Handlers</strong></p>
<blockquote>
<p>The event handlers are quite simple and simply initialise some settings and respond to the user taking a picture:</p>
<div class="csharpcode">
<div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> camera_Initialised(<span style="color: rgb(0, 0, 255);">object</span> sender, CameraOperationCompletedEventArgs e)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 128, 0);">// set the camera resolution</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">if</span> (e.Succeeded)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        var res = from resolution <span style="color: rgb(0, 0, 255);">in</span> camera.AvailableResolutions</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                    <span style="color: rgb(0, 0, 255);">where</span> resolution.Width == 640</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                    select resolution;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        camera.Resolution = res.First();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(0, 128, 0);">// user has pressed the camera button</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> camera_ButtonFullPress(<span style="color: rgb(0, 0, 255);">object</span> sender, EventArgs e)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    <span style="color: rgb(0, 0, 255);">if</span> (capturing) <span style="color: rgb(0, 0, 255);">return</span>;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    capturing = <span style="color: rgb(0, 0, 255);">true</span>;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    camera.CaptureImage();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">}</pre><!--CRLF--></div></div></div></blockquote>


<blockquote>
<p>We will add the ‘camera_CaptureImageAvailable’ handler later.</p>
<p>&nbsp;</p>
</blockquote>
<p><strong>Download the Barcode Library</strong></p>
<blockquote>
<p>The library can be downloaded here:</p>
</blockquote>
<blockquote>
<p><a title="https://twit88.com/platform/projects/show/mt-barcode" href="https://twit88.com/platform/projects/show/mt-barcode">https://twit88.com/platform/projects/show/mt-barcode</a></p>
</blockquote>
<blockquote>
<p>It ships with a sample Windows Phone 7 app, but this doesn’t read the data directly from the camera; instead it uses some hard-coded test images.&nbsp; It also ships with an example desktop app for generating your own codes.</p>
<p>&nbsp;</p>
</blockquote>
<p><strong>Add the Barcode Library to the Project</strong></p>
<blockquote>
<p>In <em>MainPage.xaml.cs</em>, add the following <em>using</em> statement:</p>
<p>&nbsp;</p><div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">using</span> MessagingToolkit.Barcode;</pre><!--CRLF--></div></div><p>&nbsp;</p></blockquote>
<blockquote>
<p>Add a reference to the <em>Windows Phone 7</em> version of the library:</p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/7065.image_5B4D3559.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/7065.image_5F00_5B4D3559.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/5811.image_thumb_5AE10264.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/5811.image_5F00_thumb_5F00_5AE10264.png" width="240" height="37"></a></p>
</blockquote>
<p><strong>Decode the QR Code Image</strong></p>
<blockquote>
<p>The <em>camera_CaptureImageAvailable</em> event handler is called once we have an image to process. This is where we try to decode any possible QR Code it contains:</p>
</blockquote><p>&nbsp;</p><blockquote><div id="codeSnippetWrapper"><div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(0, 0, 255);">private</span> <span style="color: rgb(0, 0, 255);">void</span> camera_CaptureImageAvailable(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    <span style="color: rgb(0, 0, 255);">object</span> sender, </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    ContentReadyEventArgs e)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">{</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    capturing = <span style="color: rgb(0, 0, 255);">false</span>;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    Stream imageStream = (Stream)e.ImageStream;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    BarcodeDecoder barcodeDecoder = <span style="color: rgb(0, 0, 255);">new</span> BarcodeDecoder();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    Dictionary&lt;DecodeOptions, <span style="color: rgb(0, 0, 255);">object</span>&gt; decodingOptions = </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                        <span style="color: rgb(0, 0, 255);">new</span> Dictionary&lt;DecodeOptions, <span style="color: rgb(0, 0, 255);">object</span>&gt;();</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    List&lt;BarcodeFormat&gt; possibleFormats = <span style="color: rgb(0, 0, 255);">new</span> List&lt;BarcodeFormat&gt;(1);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    Result result;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">    Dispatcher.BeginInvoke(() =&gt;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        WriteableBitmap qrImage = <span style="color: rgb(0, 0, 255);">new</span> WriteableBitmap(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                                        (<span style="color: rgb(0, 0, 255);">int</span>)camera.Resolution.Width, </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                                        (<span style="color: rgb(0, 0, 255);">int</span>)camera.Resolution.Height);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        imageStream.Position = 0;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        qrImage.LoadJpeg(imageStream);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        possibleFormats.Add(BarcodeFormat.QRCode);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        decodingOptions.Add(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            DecodeOptions.PossibleFormats, </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            possibleFormats);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">try</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            result = barcodeDecoder.Decode(qrImage, decodingOptions);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">&nbsp;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            resultText.Text = result.Text;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">catch</span> (NotFoundException)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            <span style="color: rgb(0, 128, 0);">// this is expected if the image does not contain a valid</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            <span style="color: rgb(0, 128, 0);">// code, Or is too distorted to read</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            resultText.Text = <span style="color: rgb(0, 96, 128);">"&lt;nothing to display&gt;"</span>;</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">        <span style="color: rgb(0, 0, 255);">catch</span> (Exception ex)</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        {</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">            <span style="color: rgb(0, 128, 0);">// something else went wrong, so alert the user</span></pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">            MessageBox.Show(</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                ex.Message, </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">                <span style="color: rgb(0, 96, 128);">"Error Decoding Image"</span>, </pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">                MessageBoxButton.OK);</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">        }</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);">    });</pre><!--CRLF--><pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;">}</pre><!--CRLF--></div></div></blockquote>
<p><strong>Compile and Deploy</strong></p>
<blockquote>
<p>As mentioned at the top of this post, you will need to deploy the app to a real phone, since the Emulator generates its own test output which is useless in this case.</p>
</blockquote>
<blockquote></blockquote>
<p>That’s it.&nbsp; You should now have a basic QR Reader.&nbsp; Test it by reading the code below:</p>
<blockquote><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/1185.qrsample_39ED8FBD.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/1185.qrsample_5F00_39ED8FBD.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="qrsample" border="0" alt="qrsample" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/42/05/metablogapi/1777.qrsample_thumb_31F5ED5B.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-42-05-metablogapi/1777.qrsample_5F00_thumb_5F00_31F5ED5B.png" width="143" height="143">&nbsp;</a></blockquote>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #5: Discussion</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-5-discussion</link>
      <pubDate>Mon, 15 Aug 2011 03:55:48 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/08/15/powershell-diversion-5-discussion/</guid>
      <description><![CDATA[[Follow-up discussion for PowerShell Diversion #5 ]  Here is an implementation of FizzBuzz that...]]></description>
      <content:encoded><![CDATA[<p><em>[Follow-up discussion for </em><a href="https://blogs.msdn.com/b/richmac/archive/2011/08/01/powershell-diversion-5-let-s-play-fizzbuzz.aspx"><em>PowerShell Diversion #5</em></a><em> ]</em></p>  <p>Here is an implementation of FizzBuzz that solves the original request to display the ‘numbers’ between 1 and 100:</p>  <blockquote>   <p><code>       <br />foreach($i in (1..100))         <br />{         <br />&#160;&#160;&#160; $out = &quot;&quot;         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($i % 3 -eq 0)         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $out += &quot;Fizz&quot;         <br />&#160;&#160;&#160; }         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($i % 5 -eq 0)         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $out += &quot;Buzz&quot;         <br />&#160;&#160;&#160; }         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($out -eq &quot;&quot;) {$out = $i}         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; $out         <br />}</code></p> </blockquote>  <p>Note the use of the modulus operator (‘%’), which returns the remainder of a division.&#160; If the remainder is zero, then the number being divided (the ‘dividend’) must be an exact multiple of the number doing the dividing (the ‘divisor’).&#160; So, this is an excellent way to check things like ‘is a number odd or even’ (calculate ‘X % 2’ – no remainder it’s even). </p>  <p>Here we use the modulus operator to check the FizzBuzz rules regarding divisibility by 3 or 5.&#160; Although we also have a rule for divisible by 3 <em>and</em> 5, we don’t need to check that condition explicitly as it will show up naturally in the results from the two calculations we already make.</p>  <p>A slightly more compact version, using a <em>switch</em> statement:</p>  <blockquote>   <p><code>&#160;&#160;&#160; switch ((1..100))        <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {$_%15 -eq 0} {&quot;FizzBuzz&quot;;continue}         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {$_%3 -eq 0} {&quot;Fizz&quot;;continue}         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {$_%5 -eq 0} {&quot;Buzz&quot;;continue}         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Default {$_}         <br />&#160;&#160;&#160; }</code></p> </blockquote>  <p>This makes use of a lesser-known feature of switch – the ability to supply a scriptblock as the ‘condition’ to match against.&#160; Typically, switch is used to match numbers and simple strings, but it is much more powerful than that; the above is a fairly simple example, but the scriptblock could be much more complex, which the restriction that it return $true or $false, so switch can tell if the match was successful.&#160; In his excellent book, <a href="https://www.manning.com/payette2/" target="_blank">PowerShell in Action</a>, Bruce Payette (co-designer of PowerShell) rates switch as the most powerful feature in PowerShell because of this incredible flexibility. </p>  <p>To count the number of ‘Fizz’, etc between 1 and 1000, a simple modification of the original attempt is required:<code></code> </p>  <blockquote>   <p><code>1 .. 1000 | foreach{        <br />&#160;&#160;&#160; $out = &quot;&quot;         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($_ % 3 -eq 0)         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $out += &quot;Fizz&quot;         <br />&#160;&#160;&#160; }         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($_ % 5 -eq 0)         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; $out += &quot;Buzz&quot;         <br />&#160;&#160;&#160; }         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; if($out -eq &quot;&quot;) {$out = $i}         <br />&#160;&#160;&#160; <br />&#160;&#160;&#160; $out         <br />} | Where-Object {$_ -like &quot;*zz*&quot;} | Group-Object</code></p> </blockquote> Incidentally, I mentioned in the original post that some people use this as a developer recruiting tool.&#160; Check out some commentary on that <a href="https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/" target="_blank">here</a> and <a href="https://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html" target="_blank">here</a>.    ]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #5: Let’s Play FizzBuzz</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-5-lets-play-fizzbuzz</link>
      <pubDate>Mon, 01 Aug 2011 02:52:08 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/08/01/powershell-diversion-5-lets-play-fizzbuzz/</guid>
      <description><![CDATA[Here’s a quick diversion to exercise your basic scripting skills:  FizzBuzz is a game often played...]]></description>
      <content:encoded><![CDATA[<p>Here’s a quick diversion to exercise your basic scripting skills:</p>  <p>FizzBuzz is a game often played by schoolchildren as a way to practice their metal arithmetic abilities (or as a game for drunken adults, if you listen to <a href="https://en.wikipedia.org/wiki/FizzBuzz" target="_blank">Wikipedia</a>).&#160; The rules we will use are:</p>  <ul>   <li>Count from 1 to 100, displaying the numbers as you go, however ... </li>    <li>Any number divisible by 3 should be replaced by the word “Fizz” </li>    <li>Any number divisible by 5 should be replaced by the word “Buzz” </li>    <li>Any number divisible by both 3 <i>and</i> 5 should be replaced by the word “FizzBuzz” </li> </ul>  <p>The first 20 “numbers” would then be:</p>  <blockquote>   <p>1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz</p> </blockquote>  <p>This is a deceptively simple task that I sometimes include as a challenge in my PowerShell workshops.&#160; While I just use it as an unusual and fun way to learn about scripting (specifically looping and decision making), some recruiters actually use it as a screening tool when hiring developers!</p>  <p>Once you have your solution, modify it to count the number of “Fizz”, “Buzz” and “FizzBuzz” entries between 1 and 1000.</p>  <p>Have fun and get some hints <a href="https://bit.ly/n0IUAH" target="_blank">here</a> if you need them.</p>]]></content:encoded>
    </item>
    <item>
      <title>WMI Date Formats … PowerShell to the Rescue</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/wmi-date-formats-powershell-to-the-rescue</link>
      <pubDate>Thu, 21 Jul 2011 12:05:11 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/07/21/wmi-date-formats-powershell-to-the-rescue/</guid>
      <description><![CDATA[If you use WMI in your PowerShell scripts you’ll probably have noticed that dates are shown in a...]]></description>
      <content:encoded><![CDATA[<p>If you use WMI in your PowerShell scripts you’ll probably have noticed that dates are shown in a rather odd format.&#160; For example:</p>  <blockquote><code>     <p>Get-WmiObject –Class Win32_BIOS |        <br />&#160;&#160;&#160;&#160; Format-Table Description, ReleaseDate -AutoSize</p>      <br />      <p>Description&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ReleaseDate&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />-----------&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -----------&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />Phoenix ROM BIOS A14 20090511000000.000000+000</p>   </code></blockquote>  <p>While you <em>can</em> read the dates (they are in “<em>year-month-day-hour-minute-second”</em> format), they are difficult to parse quickly, especially if you have a long table filled with them.</p>  <p>Fortunately, PowerShell helps you out by adding the <em>ConvertToDateTime()</em> method to each WMI object.&#160; This allows you to do things like this:</p>  <blockquote><code>     <p>$os = Get-WmiObject –Class Win32_OperatingSystem        <br />$os.ConvertToDateTime($os.InstallDate)</p>      <p>10 May 2011 14:54:26</p>   </code></blockquote>  <p>It is easy to include this in a table using a calculated property:</p>  <blockquote><code>     <p>$Lease = @{Label=“Lease Expires”;&#160;&#160; <br />Expression={$_.ConvertToDateTime($_.DHCPLeaseExpires)}}</p>      <p>Get-WmiObject -Class Win32_NetworkAdapterConfiguration `        <br />&#160;&#160;&#160; -Filter &quot;NOT DHCPLeaseExpires = NULL&quot; |         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Format-Table Description, $Lease –Autosize</p>      <blockquote>       <p>Description&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Lease Expires&#160;&#160;&#160;&#160;&#160;&#160; <br />-----------&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ------------&#160;&#160;&#160;&#160;&#160;&#160; <br />Intel(R) WiFi Link 5300 AGN 04/08/2011 18:49:09</p>     </blockquote>   </code></blockquote>  <p>Strictly speaking the hashtable could be defined in-line with the <em>Format-Table</em> cmdlet, but I’ve put it on its own line for readability.</p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #4: Discussion</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-4-discussion</link>
      <pubDate>Thu, 23 Jun 2011 14:42:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/06/23/powershell-diversion-4-discussion/</guid>
      <description><![CDATA[[Follow-up discussion for PowerShell Diversion #4 ]
OK, first the stats:

There are 86 solutions...]]></description>
      <content:encoded><![CDATA[<p><em>[Follow-up discussion for </em><a href="https://blogs.msdn.com/b/richmac/archive/2011/06/13/powershell-diversion-4-you-can-count-on-powershell.aspx" target="_blank"><em>PowerShell Diversion #4</em></a><em> ]</em></p>
<p>OK, first the stats:</p>
<ol>
<li>There are 86 solutions if you allow for leading zeros in EFGH and consider &lsquo;AB x CD&rsquo; to be distinct from &lsquo;CD x AB&rsquo;</li>
<li>There are 52 solutions if you rule out any with leading zeros in EFGH.&nbsp; Leading zeros in AB or CD are not a consideration since there are no solutions where that is the case.</li>
<li>There are 26 solutions when leading zeros are disallowed and &lsquo;AB x CD&rsquo; is considered to be the same as &lsquo;CD x AB&rsquo;.</li>
<li>The largest and smallest values of EFGH are, respectively:&nbsp; 6840 and 0456 (allowing leading zeros), or 6840 and 1058 (disallowing leading zeros).</li>
</ol>
<p>So, how do you calculate all this?&nbsp; There are two key things we need to know.&nbsp; The first is how to build an executable expression one piece at a time. For example, if we set some variables like this:</p>
<blockquote><code>$a = 5 <br />$b = 7 <br />$c = 2 <br />$d = 4</code></blockquote>
<p>We can create a string like this: &ldquo;$a$b * $c$d&rdquo;, which will generate this output if &lsquo;run&rsquo; by itself: 57 * 24.&nbsp; We get the standard variable replacement, but it didn&rsquo;t execute the calculation that the string represents.&nbsp; We can force it to do the calculation using <em>Invoke-Expression</em>:</p>
<blockquote>
<p><code>Invoke-Expression &ldquo;$a$b * $c$d&rdquo;</code></p>
</blockquote>
<p>Which gives the expected output: 1568.&nbsp; We can go a step further:</p>
<blockquote>
<p><code>$e = 1; $f =3; $g = 6; $h = 8</code> <br /><code>Invoke-Expression&nbsp; &ldquo;$a$b * $c$d -eq $e$f$g$h&rdquo;</code></p>
</blockquote>
<p>Which returns &lsquo;true&rsquo;.&nbsp; Notice that the full expression, &ldquo;57 x 24 = 1368&rdquo;, is actually one of the solutions to the problem, since all of the digits are unique and the equation is true.&nbsp; If you set <em>$h = 9</em>, then the digits are all still unique, but <em>Invoke-Expression</em> returns &lsquo;false&rsquo;, since the equation is no longer valid.&nbsp; Using this technique we can easily check any possible solution to see if it represents a valid equation.</p>
<p>Now we know how to validate an expression that we build at runtime, we need to find a way to generate candidate expressions to test.&nbsp; As this is a combinatorial problem, I am guessing there is no shortcut to finding the answers, though if you know different,&nbsp;I&rsquo;m happy to be proved wrong!&nbsp; The solution below uses simple brute-force to generate all possible combinations of digits, discarding any that fail the test.</p>
<p>As the final script has formatting issues due to the blog theme, I&rsquo;ll describe the general approach and you can download the full solution from the link at the end.&nbsp;</p>
<p>Firstly, we need an array of the digits:</p>
<blockquote>
<p><code>$digits = 0 .. 9</code></p>
</blockquote>
<p>Then we need to loop through these setting $a to each digit, then $b to each remaining digit, and so on:</p>
<blockquote>
<p><code> <br />foreach ($a in $digits) <br />{ <br />&nbsp;&nbsp;&nbsp; foreach ($b in ($digits -ne $a)) <br />&nbsp;&nbsp;&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($c in ($digits -ne $a -ne $b)) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # rest of processing omitted <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br />&nbsp;&nbsp;&nbsp; } <br />}</code></p>
</blockquote>
<p>This nesting carries on, with each level looping through the unassigned digits until we reach $h, at which point we apply the <em>Invoke-Expression</em> test to see if the expression is valid &ndash; since we only use the unused digits from the previous step at each stage, we will not generate any duplicates, so anything that passes the test is not only a valid equation, but also guaranteed to have all digits unique &ndash; it is a solution to the original problem.</p>
<p>At this point, we could simply print out the correct solutions as we find them.&nbsp; However, as the original question asked for some statistics, we need a way of counting the solutions.&nbsp; Rather than run the script multiple times, a more efficient method is to capture each solution into a collection, that way we need only run the script once to get the solutions and we can sort, filter and count the them later.&nbsp; The final step is to create a <em>PsObject</em> for each solution and add it to an array:</p>
<blockquote>
<p>if (Invoke-Expression "$a$b * $c$d -eq $e$f$g$h") <br />{ <br />&nbsp;&nbsp;&nbsp;&nbsp; $solutions += New-Object PsObject -Property `&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{AB="$a$b"; CD="$c$d";EFGH="$e$f$g$h"}&nbsp; <br />}</p>
</blockquote>
<p>Once we have the array, we can count the solutions (and find the maximum an minimum values for EFGH) like this:</p>
<blockquote>
<p>$solutions | Measure-Object EFGH -Maximum -Minimum</p>
</blockquote>
<p>Get the full script <a href="https://bit.ly/lYScfz" target="_blank">here</a>.</p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #4: Can You Count on PowerShell?</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-4-can-you-count-on-powershell</link>
      <pubDate>Mon, 13 Jun 2011 07:30:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/06/13/powershell-diversion-4-can-you-count-on-powershell/</guid>
      <description><![CDATA[Here&rsquo;s a problem that is more abstract and a bit trickier than the previous...]]></description>
      <content:encoded><![CDATA[<p>Here&rsquo;s a problem that is more abstract and a bit trickier than the previous &lsquo;diversions&rsquo;.&nbsp; It doesn&rsquo;t require anything very sophisticated or obscure in terms of PowerShell, but might take a bit of extra thought on how to construct your solution.</p>
<p>Recently I say the following tweet from <a target="_blank" href="https://twitter.com/#!/standupmaths">@standupmaths</a>:</p>
<blockquote>
<p><em>Multiple two two-digit numbers to get a four-digit answer. ab &times; cd = efgh Can you find one with all eight digits different?</em></p>
</blockquote>
<p>As it happens there are quite a few solutions to this problem.&nbsp; Can you use PowerShell to find them?&nbsp; </p>
<p>Some things to work out:</p>
<ol>
<li>How many solutions are there if you allow EFGH to have leading zeros (e.g. &lsquo;12 x 12 = 0144&rsquo;) and consider &lsquo;AB x CD&rsquo; to be distinct from &lsquo;CD x AB&rsquo;? </li>
<li>How many solutions are there if you disallow leading zeros in EFGH?&nbsp; Why are leading zeros for &lsquo;AB&rsquo; and &lsquo;CD&rsquo; not a consideration? </li>
<li>How many solutions are there is you disallow leading zeros for EFGH and consider &lsquo;AB x CD&rsquo; to be the same as &lsquo;CD x AB&rsquo;? </li>
<li>What are the largest and smallest values of EFGH (with and without leading zeros)? </li>
</ol>
<p><a target="_blank" href="https://bit.ly/mSXt7U">Hints</a>, if you need them.</p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #3: Discussion</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-3-discussion</link>
      <pubDate>Mon, 06 Jun 2011 16:22:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/06/06/powershell-diversion-3-discussion/</guid>
      <description><![CDATA[[Follow-up discussion for PowerShell Diversion #3]
So, it looks like 35 people attended the...]]></description>
      <content:encoded><![CDATA[<p>[<em>Follow-up discussion for </em><a target="_blank" href="https://blogs.msdn.com/b/richmac/archive/2011/05/30/powershell-diversion-3-powershell-week.aspx"><em>PowerShell Diversion #3</em></a>]</p>
<p>So, it looks like 35 people attended the different sessions for PowerShell Week.&nbsp; The following one-liner can generate a list of their names and email addresses from the individual attendee lists (linked in the original post):</p>
<p><code>
<p>1..5 | <br />&nbsp;&nbsp;&nbsp; ForEach-Object { Import-Csv ("Session$_.csv") } | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select-Object Name, Email -Unique | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sort-Object @{Expression={$_.Name.Split(" ")[-1]}} | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Export-Csv CombinedList.csv &ndash;NoTypeInformation</p>
</code></p>
<p>How does this work?&nbsp; Well, the first two lines are reading the entries from all of the individual CSV files:</p>
<blockquote>
<p><code>1..5 | <br />ForEach-Object { Import-Csv ("Session$_.csv") } |</code> </p>
</blockquote>
<p>The <em>Range Operator</em> (..) is a nice way to quickly loop through a set of consecutive numbers.&nbsp; We then take advantage of PowerShell&rsquo;s excellent variable substitution to generate the name of the current file.&nbsp; That is, &ldquo;Session$_.csv&rdquo; becomes &ldquo;Session1.csv&rdquo;, &ldquo;Session2.csv&rdquo;, etc.&nbsp; This result sin every entry from each of the CSV files being sent along the pipeline for processing.</p>
<p>Next, the <em>Select-Object</em> cmdlet grabs the Name and Email fields for each entry and the &ldquo;-Unique&rdquo; switch ensures that any duplicates (and there are quite a few across the files) are discarded:</p>
<p><code>
<blockquote><code>
<p>Select-Object Name, Email -Unique</p>
</code></blockquote>
</code></p>
<p>At this point we have a collection of objects, one for each unique attendee.&nbsp;&nbsp; Technically, we&rsquo;ve done enough to now send the email to each attendee as mentioned in the original post.&nbsp; However, for ease of use (and because the original problem asked for it), we should sort the names alphabetically.</p>
<p>Since the names are given as a single string containing all parts of the name, we need a way to break this into the individual words, so we can sort on the last name.&nbsp;&nbsp; The good news is that <em>Sort-Object</em> allows us to do all of this in a single step:</p>
<blockquote><code>
<p>Sort-Object @{Expression={$_.Name.Split(" ")[-1]}}</p>
</code></blockquote>
<p>We simply supply the cmdlet with a hash table that defines an expression who&rsquo;s result is used as the item to be sorted &ndash; in this case it will be the last name of the attendee.&nbsp; </p>
<p>The expression uses the <em>Split()</em> method of string objects to generate an array where the first name is in position 0, the second name in position 1, etc.&nbsp; In order to make sure we use the last name in all cases, we use the index &ldquo;-1&rdquo;, which translates as &ldquo;the last item in the array&rdquo; (&ldquo;-2&rdquo;, would be the &ldquo;second to last item&rdquo;, and so on).&nbsp; If we didn&rsquo;t use this and had assumed all attendees had only a first and last name, then &ldquo;James Clerk Maxwell&rdquo; would be sorted incorrectly, and really that&rsquo;s no way to treat one of the greatest physicists of all time.</p>
<p>Finally, we use Export-Csv to generate a new file with the <a target="_blank" href="https://bit.ly/mvsjmf">combined list</a> of attendees.&nbsp; The &ldquo;-NoTypeInformation&rdquo; switch prevents the cmdlet adding the object type name to the head of the file.</p>
<p>To answer the additional question about who attended the most/fewest sessions, we need to modify the code like this:</p>
<p><code>
<p>1..5 | <br />&nbsp;&nbsp;&nbsp; ForEach-Object { Import-Csv ("Session$_.csv") } | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Group-Object Name | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sort-Object Count &ndash;Descending</p>
</code></p>
<p>From the results, it is clear that &ldquo;Marie Curie&rdquo; attended the most sessions (all five &ndash; she can&rsquo;t get enough scripting), and several people (including &ldquo;Grant Imahara&rdquo;,&nbsp; &ldquo;Leonhard Euler&rdquo; and&nbsp; &ldquo;Douglas Adams&rdquo;) need a bit more convincing about the wonders of Powershell as they attended only a single sessions each.</p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #3: PowerShell Week</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-3-powershell-week</link>
      <pubDate>Mon, 30 May 2011 07:00:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/05/30/powershell-diversion-3-powershell-week/</guid>
      <description><![CDATA[Last week was &ldquo;PowerShell Week&rdquo; in our office*.&nbsp; In an effort to introduce my...]]></description>
      <content:encoded><![CDATA[<p>Last week was &ldquo;PowerShell Week&rdquo; in our office*.&nbsp; In an effort to introduce my colleagues to the wonders of PowerShell, I arranged a series of &ldquo;PowerLunches&rdquo;, each one lasting an hour and covering a different aspect of PowerShell scripting.&nbsp;&nbsp; Enrolment was open to anyone in the office, and people were free to attend as many of the sessions as they wanted.</p>
<p>The week was a great success, but now I need to send a follow-up email.&nbsp;&nbsp; I have to target it to just the attendees, so can&rsquo;t just send an office-wide mail. The problem is that all I have are the separate attendee lists for each session (as CSV files), and, since attendance was more-or-less random, there is a lot of duplication between lists:</p>
<blockquote>
<p><a target="_blank" href="https://bit.ly/jNIuMK">Attendee Lists</a></p>
</blockquote>
<p>Can you use PowerShell to generate a single list of attendees and email addresses?&nbsp; While making your list, consider these points:</p>
<ul>
<li>The list must be in alphabetical order by surname</li>
<li>The list must account for everyone who attended at least one session, but should have no duplicate entries</li>
<li>Although there are no two people in my office with same name, make sure your code could handle a situation where two attendees did have the same name (but, of course, different email addresses)?</li>
</ul>
<p>While you&rsquo;re at it, can you work out who attended the most sessions, and who attended the fewest?</p>
<p>Some <a target="_blank" href="https://bit.ly/miRVgv">hints</a>, if you need them.</p>
<p><em>* &ndash; If any of my colleagues are reading this, no, you didn&rsquo;t miss out - last week </em>wasn&rsquo;t<em> &ldquo;PowerShell Week&rdquo;.&nbsp; I made that up.&nbsp; Wouldn&rsquo;t it be great, though?</em></p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #2: Discussion</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-2-discussion</link>
      <pubDate>Sat, 21 May 2011 04:00:00 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/05/21/powershell-diversion-2-discussion/</guid>
      <description><![CDATA[[Follow-up discussion for PowerShell Diversion #2]
There are a number of things we need to look at...]]></description>
      <content:encoded><![CDATA[<p><em>[Follow-up discussion for </em><em><a target="_blank" href="https://blogs.msdn.com/b/richmac/archive/2011/04/28/powershell-diversion-2-charts-tables-and-events.aspx">PowerShell Diversion #2</a></em><em>]</em></p>
<p>There are a number of things we need to look at to make a table with a&nbsp; built-in chart (as mentioned in the <a target="_blank" href="https://rmacdonald.members.winisp.net/Blog_Docs/diversion2_hints.txt">hint</a> file from the original post).&nbsp; The first is working out how to read events from the event log:</p>
<p>This code will do that for us:</p>
<blockquote><code>Get-EventLog -LogName System -EntryType Error,Warning |&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp; Group-Object Source |&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sort-Object Count &ndash;Descending |&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Format-Table Count, Name -Autosize <br /></code></blockquote>
<p>This is a single line of PowerShell.&nbsp; I have broken the line at each pipe and indented the next row &ndash; I find this makes for neater and easier to read code when you have a long or complex 1-liner.</p>
<p>What is it doing? Well, firstly the <em>Get-EventLog</em> cmdlet gathers the specific event types (&ldquo;error&rdquo; and &ldquo;warning&rdquo;) from the target log (&ldquo;system&rdquo;), which it then sends to be grouped by <em>Group-Object</em>.&nbsp; <em>Group-Object</em> produces a group of event entries for each unique source in the&nbsp; stream sent by <em>Get-EventLog</em>.&nbsp; It also kindly counts the number of items in each group, which <em>Sort-Object</em> makes use of to sort the groups in order starting with the most populous to the least.&nbsp; Finally, <em>Format-Table</em> shows the information in a neat table like this:</p>
<blockquote><code>Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />----- ----&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp; 129 Kerberos&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 57 NETLOGON&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 36 cdrom&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 29 Schannel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 26 Microsoft Antimalware&nbsp; <br />&nbsp;&nbsp; 25 DCOM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 17 Service Control Manager <br />&nbsp;&nbsp;&nbsp; 7 Disk&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 2 Virtual Disk Service&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 2 WudfUsbccidDriver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 2 volsnap&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 2 SCardSvr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 1 Ntfs&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 1 EventLog </code></blockquote>
<p>We&rsquo;re half-way to our end goal.&nbsp; This information is useful as is (I think I might have an issue with Kerberos), but just to add that finishing touch of the &lsquo;chart&rsquo;, we need to tweak the code:</p>
<blockquote><code><br />
<p>$Evt = Get-EventLog -LogName System ` <br />&nbsp;&nbsp;&nbsp; -EntryType Error,Warning | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Group-Object Source | <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sort-Object Count -Descending </p>
<p>$Scale = 20 / $Evt[0].Count</p>
<p>$Chart = @{Label="Chart";` <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Expression={"X"*(($_.Count) * $Scale)}}</p>
<p>$Evt | Format-Table Count, Name, $Chart -Autosize</p>
</code></blockquote>
<p>Now, what is <em>this</em> doing?&nbsp; The first line is just the same as before, except we capture the events into a variable instead of displaying them.&nbsp; Next we calculate a scaling factor using the size of the largest group &ndash; here we divide 20 by the group size, so that in the final chart this group will be represented by 20 Xs.&nbsp; </p>
<p>The interesting stuff takes place on the next line, where we use a hashtable (signified by &lsquo;@{ &hellip; }&rsquo;) to produce the &lsquo;calculated property&rsquo; that will be our chart values.&nbsp; The hashtable comprises of two parts, the &lsquo;label&rsquo;, which will become the title of the chart column in our final table, and the &lsquo;expression&rsquo;, which executes some PowerShell code to calculate the values in the rows of that column.&nbsp; In this instance, we make use of the fact that the multiplication operator has an overload that effectively concatenates an input string with itself the given number of times.&nbsp; For example, &ldquo;X&rdquo; * 5, produces: &ldquo;XXXXX&rdquo;.</p>
<p>Finally, we display the events in a table, which gives a result like this:</p>
<blockquote><code>Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Chart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp; 129 Kerberos&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XXXXXXXXXXXXXXXXXXXX <br />&nbsp;&nbsp; 57 NETLOGON&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XXXXXXXXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 36 cdrom&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XXXXXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 29 Schannel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XXXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 26 Microsoft Antimalware&nbsp;&nbsp; XXXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 25 DCOM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XXXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; 17 Service Control Manager XXX&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; 7 Disk&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; X&nbsp; </code></blockquote>
<p>For the optional enhancements (running against multiple computers and grouping by source and Event ID), we need to perform these two simple changes:</p>
<p>To run the script against multiple machines, simply alter the <em>GetEventLog</em> cmdlet to include the <em>&ndash;ComputerName</em> parameter, like this: <em>&ndash;ComputerName Server1, Server2, Server3, &hellip;</em></p>
<p>To group by source <em>and</em> Event ID, change the <em>Group-Object</em> cmdlet to include both, like this: <em>Group-Object Source, EventID</em></p>]]></content:encoded>
    </item>
    <item>
      <title>PowerShell Diversion #2: Charts, Tables and Events</title>
      <link>https://docs.microsoft.com/archive/blogs/richmac/powershell-diversion-2-charts-tables-and-events</link>
      <pubDate>Thu, 28 Apr 2011 06:53:28 GMT</pubDate>
      <dc:creator><![CDATA[UniqueName1]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/richmac/2011/04/28/powershell-diversion-2-charts-tables-and-events/</guid>
      <description><![CDATA[In an old post I discussed using the Microsoft Chart Controls to generate some professional looking...]]></description>
      <content:encoded><![CDATA[<p>In an <a href="https://blogs.technet.com/b/richard_macdonald/archive/2009/04/28/3231887.aspx" target="_blank">old post</a> I discussed using the Microsoft Chart Controls to generate some professional looking charts from PowerShell.&#160; These are great if you want to use the charts in a formal report or presentation, but what if all you want is a basic bar chart to help you quickly compare some values in a table?</p>  <p>For example, here is a table that compares the working set values for some processes:</p>  <blockquote><code>     <p>Name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Working Set Chart&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />----&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ----------- -----&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />svchost&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 133.19 XXXXXXXXXXXXXXXXXXXX         <br />powershell_ise&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 112.82 XXXXXXXXXXXXXXXXX&#160;&#160; <br />sqlservr&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 78.07 XXXXXXXXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />MsMpEng&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 74.93 XXXXXXXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />svchost&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 68.20 XXXXXXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />explorer&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 57.93 XXXXXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />svchost&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 55.70 XXXXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />CcmExec&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 41.02 XXXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />dwm&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 35.68 XXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />SearchIndexer&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 30.46 XXXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />msitcertsvc&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 25.86 XXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />DcaTray&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 25.77 XXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />svchost&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 25.27 XXXX&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />svchost&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 23.95 XXXX&#160; </p>   </code></blockquote>  <p>The ‘chart’ is constructed in such a way that the number of Xs for each entry is relative to the maximum value for the target property.&#160; So, here I have chosen a value of 20 Xs for the top svchost process which has a WS of 133.&#160; This means, for example, that explorer with a WS of 58 will have 9 Xs:</p>  <blockquote><code>     <p>20/9 ≈ 133/58</p>   </code></blockquote>  <p>Thus, the general formula is:</p>  <blockquote><code>   <p>Number of Xs = [(Maximum Xs) * (Current Property Value)] / (Maximum Property Value)</p> </code></blockquote>  <p>Obviously there is some slight discrepancy since we can’t show fractions of an X.&#160; The choice of 20 as the maximum is arbitrary (though constrained by the display width) - you can try a different number to see what looks good, or, indeed use a character other than an X.</p>  <p>Your task is …</p>  <blockquote>   <p><em>Generate a similar table for event log entries on the local system as follows:</em></p>    <ul>     <li><em>Only consider ‘error’ and ‘warning’ events from the ‘System’ log </em></li>      <li><em>Group events by source </em></li>      <li><em>Create a table with these columns: event source, number of events, chart.&#160; </em></li>      <li><em>Sort the table by ‘number of events’ </em></li>   </ul> </blockquote>  <p>This should provide a useful table showing the event source generating the greatest number of errors, but it could be enhanced to be even more useful, so once you have the technique, extend the table in these two ways:</p>  <ol>   <li><em>Scan multiple servers, aggregating the information in one table </em></li>    <li><em>Add the ‘eventID’ to the list of properties, so that the table is sorted first by source, then event id, so you can be more granular about the ‘worst offender’.</em> </li> </ol>  <p>If you’re in need of a hint, click <a href="https://bit.ly/llC7mR" target="_blank">here</a>.</p>]]></content:encoded>
    </item>
  </channel>
</rss>