<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Use PowerShell &#187; PowerShell Version 2</title>
	<atom:link href="http://blog.usepowershell.com/category/powershell/powershellv2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.usepowershell.com</link>
	<description>Real Admins Script</description>
	<lastBuildDate>Sat, 17 Jul 2010 14:38:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Adding Configuration To Remoting</title>
		<link>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/</link>
		<comments>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/#comments</comments>
		<pubDate>Fri, 16 Jul 2010 14:25:57 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/</guid>
		<description><![CDATA[This is a pretty brief post that will need some further elaboration, but Twitter doesn’t provide the best mechanism for longer examples. There are two places where you can configure your remote sessions.. 1) On the remote server – which is covered in a number of places and probably the most discoverable place to put [...]]]></description>
			<content:encoded><![CDATA[<p>This is a pretty brief post that will need some further elaboration, but Twitter doesn’t provide the best mechanism for longer examples.</p>
<p>There are two places where you can configure your remote sessions.. </p>
<p>1) On the remote server – which is covered in a number of places and probably the most discoverable place to put configuration information..&#160; See the help for <a href="http://technet.microsoft.com/en-us/library/dd819508.aspx" target="_blank">about_Session_Configurations</a>.</p>
<p>2) Locally, when you start the PSSession.&#160; This is where we will dig deeper.</p>
<p>&#160;</p>
<p>One of the cool, lesser covered features in remoting is the ability to persist a remote connection to a module.&#160; After you establish a PSSession, you can use <a href="http://technet.microsoft.com/en-us/library/dd347679.aspx" target="_blank">Export-PSSession</a> to create a module that will hold the commands to create the proxies for the commands in that session, as well as the plumbing to recreate that session.</p>
<p><a href="http://technet.microsoft.com/en-us/library/dd347679.aspx" target="_blank">Export-PSSession</a> will create a script module, which you can modify to work how you would like..&#160; </p>
<p>In the module, one of the functions generated is the Get-PSImplicitRemotingSession function.&#160; When you call one of the commands from the imported session, it will get the saved PSSession.&#160; If the session has not been established yet, the Set-PsImplicitRemotingSession function will be called, creating the session.&#160; You can add your any commands you would like after that.. </p>
<p>For example I’m using Invoke-Command below to import a module once the session is connected.&#160; That script block can be anything you need to do to configure your remote session. (NOTE: The Set-PSImplicitRemotingSession is generated by the command, I’m only adding code after that and using the $script:PSSession object that is created.</p>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">Set-PSImplicitRemotingSession</span> <span style="color: #000000">`
</span>            <span style="color: #000080">-CreatedByModule</span> <span style="color: #ff4500">$true</span> <span style="color: #000000">`
</span>            <span style="color: #000080">-PSSession</span> <span style="color: #000000">(</span>
            <span style="color: #000000">$(</span>
                <span style="color: #a9a9a9">&amp;</span> <span style="color: #ff4500">$script:NewPSSession</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-ComputerName</span> <span style="color: #8b0000">'192.168.16.150'</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-ApplicationName</span> <span style="color: #8b0000">'/wsman'</span>    <span style="color: #000080">-ConfigurationName</span> <span style="color: #8b0000">'Microsoft.PowerShell'</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-SessionOption</span> <span style="color: #000000">(</span><span style="color: #0000ff">Get-PSImplicitRemotingSessionOption</span><span style="color: #000000">)</span> <span style="color: #000000">`
</span>                    <span style="color: #000080">-Credential</span> <span style="color: #000000">(</span> <span style="color: #ff4500">$host</span><span style="color: #a9a9a9">.</span><span style="color: #000000">UI</span><span style="color: #a9a9a9">.</span><span style="color: #000000">PromptForCredential</span><span style="color: #000000">(</span> <span style="color: #8b0000">'Windows PowerShell Credential Request'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'Enter your credentials for http://192.168.16.150/wsman.'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'phoenix\steve.murawski'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'192.168.16.150'</span> <span style="color: #000000">)</span> <span style="color: #000000">)</span> <span style="color: #000000">`
</span>                     <span style="color: #000000">`
</span>                    <span style="color: #000080">-Authentication</span> <span style="color: #8a2be2">Default</span> <span style="color: #000000">`
</span>                   <span style="color: #000000">`
</span>            <span style="color: #000000">)</span>
            <span style="color: #000000">)</span>            

          <span style="color: #0000ff">Invoke-Command</span> <span style="color: #000000">{</span><span style="color: #0000ff">Import-Module</span> <span style="color: #8a2be2">TaskScheduler</span><span style="color: #000000">}</span> <span style="color: #000080">-Session</span> <span style="color: #ff4500">$script:PSSession</span></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2010/07/adding-configuration-to-remoting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Script Injection with Set-PSBreakpoint</title>
		<link>http://blog.usepowershell.com/2010/01/script-injection-with-set-psbreakpoint/</link>
		<comments>http://blog.usepowershell.com/2010/01/script-injection-with-set-psbreakpoint/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 20:15:06 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2010/01/script-injection-with-set-psbreakpoint/</guid>
		<description><![CDATA[I’ve used the integrated debugging features with PowerShell V2 since I’ve had it available, but I never really dug below the surface of setting breakpoints at certain lines. Set-PSBreakpoint offers some additional options of which I was not aware. Setting a breakpoint in relation to a variable (read, assigned, or both) Setting a breakpoint when [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve used the integrated debugging features with <a href="http://support.microsoft.com/kb/968929" target="_blank">PowerShell V2</a> since I’ve had it available, but I never really dug below the surface of setting breakpoints at certain lines.</p>
<p><a href="http://technet.microsoft.com/en-us/library/dd315264.aspx" target="_blank">Set-PSBreakpoint</a> offers some additional options of which I was not aware.</p>
<ol>
<li><strong>Setting a breakpoint in relation to a variable (read, assigned, or both)</strong> </li>
<li><strong>Setting a breakpoint when a function or command is called</strong> </li>
<li><strong>Setting a breakpoint based on the column number for the referenced line</strong> </li>
<li><strong>Run a scriptblock when a breakpoint is hit</strong> </li>
<li><strong>Breakpoints do not need to be set on a script</strong> </li>
</ol>
<p>Let’s dig into these in a bit more detail:</p>
<h3>1.  Setting a breakpoint in relation to a variable (read, assigned, or both)</h3>
<p>Set-PSBreakpoint can be used to find all the occurrences of access to a variable (in the current scope).  This can be very useful when attempting to find out where things might be taking an unexpected turn with your variable’s contents.</p>
<h3>2. Setting a breakpoint when a function or command is called</h3>
<p>This is cool.  You can configure breakpoints based on when cmdlets or functions are called.  Great stopping at the entry point to a particularly troublesome function, so you can drop into the debugger and check the state of parameters about to go in, as well as other state related issues.</p>
<h3>3. Setting a breakpoint based on the column number for the referenced line</h3>
<p>I’m not so stoked about this feature.  This merely allows you to specify which column to stop execution on in a particular line of code…  Moderately useful, but not really exciting.</p>
<h3>4. Run a scriptblock when a breakpoint is hit</h3>
<p>This is where things get interesting.  You can assign an action to occur when a breakpoint is hit.  This action is a scriptblock that is run in the scope where it is set.  Since breakpoints can be variable assignments or calls to commands, this opens up some interesting possibilities.  First off, it allows for conditional debugging.  If you only want to drop into a breakpoint if a particular value is less than zero before going into a function, you could do something like</p>
<pre class="PowerShellColorizedScript"><span style="color: #ff4500">$BreakpointAction</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">{</span>
    <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$MyNumber</span> <span style="color: #a9a9a9">-lt</span> <span style="color: #800080">0</span><span style="color: #000000">)</span>
    <span style="color: #000000">{</span>
        <span style="color: #00008b">break</span>
    <span style="color: #000000">}</span>
    <span style="color: #00008b">else</span>
    <span style="color: #000000">{</span>
        <span style="color: #00008b">continue</span>
    <span style="color: #000000">}</span>
<span style="color: #000000">}</span></pre>
<p>This also has applications outside of debugging.  Using the –Action parameter of Set-PSBreakpoint, you have the ability to run a scripblock of your choosing at any of the condition types described above – when variables are accessed, when commands are called, and at certain specific positions in the script.</p>
<p> </p>
<h3>5. Breakpoints do not need to be set on a script</h3>
<p>Finally, breakpoints do not need to be set in a script, they can just be set to respond to variable access or command use.  This means that you could use Set-PSBreakpoint in a profile script to configure a particular environment to respond in a certain way, perhaps prompting you before changing a critical environmental variable.</p>
<p> </p>
<p>I’m definitely going to be exploring these additional features and applications of Set-PSBreakpoint as I go forward. </p>
<p><a href="http://blogs.msdn.com/powershell/archive/2009/07/13/advanced-debugging-in-powershell.aspx" target="_blank">Additional debugging tips/info from the PowerShell Team Blog.</a></p>
<p>Please leave a comment as to how you think this functionality could be used.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2010/01/script-injection-with-set-psbreakpoint/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Script Club &#8211; Coming to the Greater Milwaukee Area</title>
		<link>http://blog.usepowershell.com/2009/12/script-club-coming-to-the-greater-milwaukee-area/</link>
		<comments>http://blog.usepowershell.com/2009/12/script-club-coming-to-the-greater-milwaukee-area/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:06:39 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Community]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/12/script-club-coming-to-the-greater-milwaukee-area/</guid>
		<description><![CDATA[Register here. About PowerShell Script Club 1. You Always Talk About Script club 2. You Always Talk About Script Club 3. If Someone asks for Help, And You Can Help, You Help 4. Two People Help One Person at One Time 5. One Module Per Person Per Night 6. All Scripts, All PowerShell 7. Scripts [...]]]></description>
			<content:encoded><![CDATA[<h3><strong><a href="http://mkescriptclub.eventbrite.com " target="_blank">Register here.</a></strong></h3>
<h3>About PowerShell Script Club</h3>
<p>1. You Always Talk About Script club    <br />2. You Always Talk About Script Club     <br />3. If Someone asks for Help, And You Can Help, You Help     <br />4. Two People Help One Person at One Time     <br />5. One Module Per Person Per Night     <br />6. All Scripts, All PowerShell     <br />7. Scripts will be as short as they can be     <br />8. If This is your First time at Script Club, You Have to Script</p>
<h4>The first Greater Milwaukee <a href="http://msdn.microsoft.com/en-us/library/ms714418(VS.85).aspx" target="_blank">PowerShell</a> Script Club is being formed.</h4>
<p>The first meeting will be on Tuesday, January 19th at 6:00 PM at the Greenfield Law Enforcement Center (in the Municipal Court Room), 5300 W Layton Ave, Greenfield, WI&#160; 53220.&#160; <a href="http://mkescriptclub.eventbrite.com " target="_blank">Register here.</a></p>
<p><strong><u>All</u></strong> <strong>IT Professionals (sysadmins, network admins, developers, help desk, and all others) with <u>any level of experience</u> are welcome.&#160; If you DO NOT KNOW POWERSHELL, but you WANT TO – This is the place.</strong></p>
<p>Pizza and soda will be provided, but please bring a laptop with PowerShell installed (version 1 or 2 is fine).</p>
<h4>What is a Script Club?</h4>
<p><a href="http://get-powershell.com/2009/04/04/powershell-script-club-in-seattle/" target="_blank">Andy Schneider describes a script club:</a></p>
<blockquote><p>Script Clubs are like a hands on lab with no set topic or teacher. You bring an idea for a script, and ask your fellow PowerShell users for help getting the script written.</p>
</blockquote>
<p>This is not a lecture or presentation based group (though we may have presentations from time to time).&#160; Script Club is focused on creating working scripts that will help you get your work done or just enjoy yourself.</p>
<h3><a href="http://mkescriptclub.eventbrite.com/" target="_blank">Register here.</a></h3>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/12/script-club-coming-to-the-greater-milwaukee-area/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using the Sync Framework from PowerShell</title>
		<link>http://blog.usepowershell.com/2009/11/using-the-sync-framework-from-powershell/</link>
		<comments>http://blog.usepowershell.com/2009/11/using-the-sync-framework-from-powershell/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 14:11:50 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[Sync Framework]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/11/using-the-sync-framework-from-powershell/</guid>
		<description><![CDATA[I’ve been exploring the Sync Framework for use in a couple of projects I have going and PowerShell is my preferred exploratory environment. It was a bit of fun, since I got to work with eventing for the first time in V2. First, I downloaded the Sync Framework Software Development Kit.  That provided me with [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been exploring the <a href="http://msdn.microsoft.com/en-us/sync/default.aspx" target="_blank">Sync Framework</a> for use in a couple of projects I have going and <a href="http://msdn.microsoft.com/en-us/library/ms714418(VS.85).aspx" target="_blank">PowerShell</a> is my preferred exploratory environment.</p>
<p>It was a bit of fun, since I got to work with eventing for the first time in V2.</p>
<p>First, I downloaded the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=89adbb1e-53ff-41b5-ba17-8e43a2e66254&amp;displaylang=en" target="_blank">Sync Framework Software Development Kit</a>.  That provided me with the Sync Framework runtime as well as some documentation.</p>
<p>The easiest way for me to get started was to take one <a href="http://msdn.microsoft.com/en-us/library/ee617386(SQL.105).aspx" target="_blank">of the samples</a> and convert that to PowerShell.</p>
<p>I’m going to walk along the MSDN Sample and provide the equivalent PowerShell, as well as any changes I made to make it feel more PowerShell-y.</p>
<h3>Setting Synchronization Options</h3>
<p>We are working with the File Sync Provider First up is setting the <a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.files.filesyncoptions(SQL.105).aspx" target="_blank">FileSyncOptions</a>.  FileSyncOptions are an enumeration (a limited list defined in code that maps to certain values) whose values are controlled by setting the appropriate bits to indicate the presence or absence of a flag.  <a href="http://twitter.com/meson3902" target="_blank">Mark Schill</a> has <a href="http://www.cmschill.net/stringtheory/2009/05/02/bitwise-operators/" target="_blank">a great post about how to set bitwise operations</a>.</p>
<pre class="PowerShellColorizedScript"><span style="color: #ff4500">$options </span><span style="color: #a9a9a9">= </span><span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">ExplicitDetectChanges</span>
<span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecycleDeletedFiles</span>
<span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecyclePreviousFileOnUpdates</span>
<span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecycleConflictLoserFiles</span></pre>
<h3>Specifying a Static Filter</h3>
<p>With the <a href="http://msdn.microsoft.com/en-us/library/bb902860(SQL.105).aspx" target="_blank">File System provider,</a> we can provide filters to include or exclude files and directories.</p>
<p>$FileNameFilter and $SubdirectoryNameFilter are parameters that take strings or string arrays.</p>
<pre class="PowerShellColorizedScript"><span style="color: #ff4500">$filter</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncScopeFilter</span>
<span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$FileNameFilter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">count</span> <span style="color: #a9a9a9">-gt</span> <span style="color: #800080">0</span><span style="color: #000000">)</span>
<span style="color: #000000">{</span>
   <span style="color: #ff4500">$FileNameFilter</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">ForEach-Object</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">FileNameExcludes</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Add</span><span style="color: #000000">(</span><span style="color: #ff4500">$_</span><span style="color: #000000">)</span> <span style="color: #000000">}</span>
<span style="color: #000000">}</span>
<span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$SubdirectoryNameFilter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">count</span> <span style="color: #a9a9a9">-gt</span> <span style="color: #800080">0</span><span style="color: #000000">)</span>
<span style="color: #000000">{</span>
   <span style="color: #ff4500">$SubdirectoryNameFilter</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">ForEach-Object</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SubdirectoryExcludes</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Add</span><span style="color: #000000">(</span><span style="color: #ff4500">$_</span><span style="color: #000000">)</span> <span style="color: #000000">}</span>
<span style="color: #000000">}</span></pre>
<h3>Performing Change Detection</h3>
<p>After configuring the filter, we examine the folders and files located at the paths specified.  If there has not been any previous synchronization, a metadata file will be created in each location to track any changes, updates, and deletes for later synchronization.</p>
<pre class="PowerShellColorizedScript"><span style="color: #00008b">function</span> <span style="color: #8a2be2">Get-FileSystemChange</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
<span style="color: #000000">{</span>
    <span style="color: #00008b">param</span> <span style="color: #000000">(</span><span style="color: #ff4500">$path</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span><span style="color: #000000">)</span>
    <span style="color: #00008b">try</span>
    <span style="color: #000000">{</span>
        <span style="color: #ff4500">$provider</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">new-object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncProvider</span> <span style="color: #000080">-ArgumentList</span> <span style="color: #ff4500">$path</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span>
        <span style="color: #ff4500">$provider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DetectChanges</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
    <span style="color: #000000">}</span>
    <span style="color: #00008b">finally</span>
    <span style="color: #000000">{</span>
        <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$provider</span> <span style="color: #a9a9a9">-ne</span> <span style="color: #ff4500">$null</span><span style="color: #000000">)</span>
        <span style="color: #000000">{</span>
            <span style="color: #ff4500">$provider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Dispose</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
        <span style="color: #000000">}</span>
    <span style="color: #000000">}</span>
<span style="color: #000000">}</span></pre>
<p> </p>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">Get-FileSystemChange</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #ff4500">$filter</span> <span style="color: #ff4500">$options</span>
<span style="color: #0000ff">Get-FileSystemChange</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #ff4500">$filter</span> <span style="color: #ff4500">$options</span></pre>
<p> </p>
<h3>Handling Conflicts</h3>
<p>Conflict resolution in the Sync Framework happens at the at the event level.  An event is merely something that happens that can trigger other actions.  Using <a href="http://technet.microsoft.com/en-us/library/dd347672.aspx" target="_blank">Register-ObjectEvent</a>, we can associate one or more scriptblocks with an event.</p>
<p>First, I defined scriptblocks to handle the conflicts.  There is an enumeration, the <a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.conflictresolutionaction(SQL.105).aspx" target="_blank">ConflictResolutionAction</a> enumeration, that provides some options for dealing with conflicts.  For this example, we are going to pick the source object as the winner for any conflicts.</p>
<p>You will also notice another type of conflict defined, and that is a Constraint conflict.  That can occur when an object of the same name is added on both sides in between synchronizations.  The resolution options for these conflicts can be found in the <a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.constraintconflictresolutionaction(SQL.105).aspx" target="_blank">ConstraintConflictResolutionAction</a> enumeration.</p>
<pre class="PowerShellColorizedScript"><span style="color: #ff4500">$ItemConflictAction</span> <span style="color: #a9a9a9">=</span>   <span style="color: #000000">{</span>
    <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SetResolutionAction</span><span style="color: #000000">(</span><span style="color: #008080">[Microsoft.Synchronization.ConflictResolutionAction]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">SourceWins</span><span style="color: #000000">)</span>
    <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Conflicted</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationChange</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ItemId</span>
<span style="color: #000000">}</span>
<span style="color: #ff4500">$ItemConstraintAction</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">{</span>
     <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SetResolutionAction</span><span style="color: #000000">(</span><span style="color: #008080">[Microsoft.Synchronization.ConstraintConflictResolutionAction]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">SourceWins</span><span style="color: #000000">)</span>
     <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Constrained</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationChange</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ItemId</span>
<span style="color: #000000">}</span>            

<span style="color: #006400"># Configure the events for conflicts or constraints for the source and destination providers</span>
<span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$destinationProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationCallbacks</span>
<span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConflicting</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConflictAction</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span>
<span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConstraint</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConstraintAction</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span>            

<span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$SourceProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationCallbacks</span>
<span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConflicting</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConflictAction</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span>
<span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConstraint</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConstraintAction</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span></pre>
<p>We also see for the first time in the script blocks a variable called $event.  This is an automatic variable exposed by the event and provides us information that we can use in our action.</p>
<p>Finally, I’m updating a variable in the global scope.  There probably is a better way to handle this, but scriptblocks executed in response to events only have access to the global scope and any of the automatic variable exposed to it.  Therefore, I use a variable in the global scope to gather my reporting information.</p>
<h3>Synchronizing Two Replicas</h3>
<p>To start to synchronize the two sides, first we set up the synchronization via a <a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.syncorchestrator.aspx" target="_blank">SyncOrchestrator</a> and assign it the local and remote providers, as well as defining the direction of the synchronization.  In this example (sticking with the format from MSDN, we will do an Upload, which is in the <a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.syncdirectionorder.aspx" target="_blank">SyncDirectionOrder enumeration</a> (other options are Download, DownloadAndUpload, and UploadAndDownload). </p>
<pre class="PowerShellColorizedScript"><span style="color: #006400"># Create the agent that will perform the file sync</span>
<span style="color: #ff4500">$agent</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span>  <span style="color: #8a2be2">Microsoft.Synchronization.SyncOrchestrator</span>
<span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">LocalProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$sourceProvider</span>
<span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">RemoteProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$destinationProvider</span>            

<span style="color: #006400"># Upload changes from the source to the destination.</span>
<span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Direction</span> <span style="color: #a9a9a9">=</span> <span style="color: #008080">[Microsoft.Synchronization.SyncDirectionOrder]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Upload</span>            

<span style="color: #0000ff">Write-Host</span> <span style="color: #8b0000">"Synchronizing changes from $($sourceProvider.RootDirectoryPath) to replica: $($destinationProvider.RootDirectoryPath)"</span>
<span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Synchronize</span><span style="color: #000000">(</span><span style="color: #000000">)</span><span style="color: #000000">;</span></pre>
<p>To achieve two way synchronization, we will do the upload twice, reversing the order of the providers.</p>
<pre class="PowerShellColorizedScript"><span style="color: #0000ff">Invoke-OneWayFileSync</span> <span style="color: #000080">-SourcePath</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #000080">-DestinationPath</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #000080">-Filter</span> <span style="color: #ff4500">$null</span> <span style="color: #000080">-Options</span> <span style="color: #ff4500">$options</span>
<span style="color: #0000ff">Invoke-OneWayFileSync</span> <span style="color: #000080">-SourcePath</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #000080">-DestinationPath</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #000080">-Filter</span> <span style="color: #ff4500">$null</span> <span style="color: #000080">-Options</span> <span style="color: #ff4500">$options</span></pre>
<h3>Full Example</h3>
<p>I modified the example to write out a custom object (and the logging is in the variable in the global scope as noted in the Handling Conflicts section) with the results of the synchronization (rather than logging it to the console).</p>
<p>In all, my translation is pretty similar to the example code, but there are some differences. </p>
<pre class="PowerShellColorizedScript"><span style="color: #006400"># Requires -Version 2</span>
<span style="color: #006400"># Also depends on having the Microsoft Sync Framework 2.0 SDK or Runtime</span>
<span style="color: #006400"># --SDK--</span>
<span style="color: #006400"># http://www.microsoft.com/downloads/details.aspx?FamilyID=89adbb1e-53ff-41b5-ba17-8e43a2e66254&amp;displaylang=en</span>
<span style="color: #006400"># --Runtime--</span>
<span style="color: #006400"># http://www.microsoft.com/downloads/details.aspx?FamilyId=109DB36E-CDD0-4514-9FB5-B77D9CEA37F6&amp;displaylang=en</span>
<span style="color: #006400">#</span>
<span style="color: #006400">#</span>            

<span style="color: #a9a9a9">[</span><span style="color: #add8e6">CmdletBinding</span><span style="color: #000000">(</span><span style="color: #000000">SupportsShouldProcess</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
<span style="color: #00008b">param</span> <span style="color: #000000">(</span>
    <span style="color: #a9a9a9">[</span><span style="color: #add8e6">Parameter</span><span style="color: #000000">(</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">1</span><span style="color: #a9a9a9">,</span> <span style="color: #000000">Mandatory</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #a9a9a9">,</span> <span style="color: #000000">ValueFromPipelineByPropertyName</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
    <span style="color: #a9a9a9">[</span><span style="color: #add8e6">Alias</span><span style="color: #000000">(</span><span style="color: #8b0000">'FullName'</span><span style="color: #a9a9a9">,</span> <span style="color: #8b0000">'Path'</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
    <span style="color: #008080">[string]</span><span style="color: #ff4500">$SourcePath</span>
    <span style="color: #a9a9a9">,</span> <span style="color: #a9a9a9">[</span><span style="color: #add8e6">Parameter</span><span style="color: #000000">(</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">2</span><span style="color: #a9a9a9">,</span> <span style="color: #000000">Mandatory</span><span style="color: #a9a9a9">=</span><span style="color: #ff4500">$true</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
    <span style="color: #008080">[string]</span><span style="color: #ff4500">$DestinationPath</span>
    <span style="color: #a9a9a9">,</span> <span style="color: #a9a9a9">[</span><span style="color: #add8e6">Parameter</span><span style="color: #000000">(</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">3</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
    <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$FileNameFilter</span>
    <span style="color: #a9a9a9">,</span> <span style="color: #a9a9a9">[</span><span style="color: #add8e6">Parameter</span><span style="color: #000000">(</span><span style="color: #000000">Position</span><span style="color: #a9a9a9">=</span><span style="color: #800080">4</span><span style="color: #000000">)</span><span style="color: #a9a9a9">]</span>
    <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$SubdirectoryNameFilter</span>
<span style="color: #000000">)</span>
<span style="color: #006400">&lt;#
    .Synopsis
        Synchronizes to directory trees
    .Description
        Examines two directory structures (SourcePath and DestinationPath) and uses the Microsoft Sync Framework
        File System Provider to synchronize them.
    .Example
        An example of using the command
#&gt;</span>
<span style="color: #00008b">begin</span>
<span style="color: #000000">{</span>
    <span style="color: #008080">[reflection.assembly]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">LoadWithPartialName</span><span style="color: #000000">(</span><span style="color: #8b0000">'Microsoft.Synchronization'</span><span style="color: #000000">)</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span>
    <span style="color: #008080">[reflection.assembly]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">LoadWithPartialName</span><span style="color: #000000">(</span><span style="color: #8b0000">'Microsoft.Synchronization.Files'</span><span style="color: #000000">)</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Out-Null</span>            

    <span style="color: #00008b">function</span> <span style="color: #8a2be2">Get-FileSystemChange</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
    <span style="color: #000000">{</span>
        <span style="color: #00008b">param</span> <span style="color: #000000">(</span><span style="color: #ff4500">$path</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span><span style="color: #000000">)</span>
        <span style="color: #00008b">try</span>
        <span style="color: #000000">{</span>
            <span style="color: #ff4500">$provider</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">new-object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncProvider</span> <span style="color: #000080">-ArgumentList</span> <span style="color: #ff4500">$path</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span>
            <span style="color: #ff4500">$provider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DetectChanges</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
        <span style="color: #000000">}</span>
        <span style="color: #00008b">finally</span>
        <span style="color: #000000">{</span>
            <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$provider</span> <span style="color: #a9a9a9">-ne</span> <span style="color: #ff4500">$null</span><span style="color: #000000">)</span>
            <span style="color: #000000">{</span>
                <span style="color: #ff4500">$provider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Dispose</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
            <span style="color: #000000">}</span>
        <span style="color: #000000">}</span>
    <span style="color: #000000">}</span>            

    <span style="color: #00008b">function</span> <span style="color: #8a2be2">Invoke-OneWayFileSync</span><span style="color: #000000">(</span><span style="color: #000000">)</span>
    <span style="color: #000000">{</span>
        <span style="color: #00008b">param</span> <span style="color: #000000">(</span><span style="color: #ff4500">$SourcePath</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$DestinationPath</span><span style="color: #a9a9a9">,</span>
                    <span style="color: #ff4500">$Filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$Options</span><span style="color: #000000">)</span>
        <span style="color: #ff4500">$ApplyChangeJobs</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">@(</span><span style="color: #000000">)</span>
        <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">@(</span><span style="color: #000000">)</span>
        <span style="color: #00008b">try</span>
        <span style="color: #000000">{</span>
            <span style="color: #006400"># Scriptblocks to handle the events raised during synchronization</span>
            <span style="color: #ff4500">$AppliedChangeAction</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">{</span>
                <span style="color: #ff4500">$argument</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span>
                <span style="color: #00008b">switch</span> <span style="color: #000000">(</span><span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ChangeType</span><span style="color: #000000">)</span>
                <span style="color: #000000">{</span>
                    <span style="color: #000000">{</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ChangeType</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #008080">[Microsoft.Synchronization.Files.ChangeType]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Create</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Created</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">NewFilePath</span><span style="color: #000000">}</span>
                    <span style="color: #000000">{</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ChangeType</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #008080">[Microsoft.Synchronization.Files.ChangeType]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Delete</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Deleted</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">OldFilePath</span><span style="color: #000000">}</span>
                    <span style="color: #000000">{</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ChangeType</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #008080">[Microsoft.Synchronization.Files.ChangeType]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Update</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Updated</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">OldFilePath</span><span style="color: #000000">}</span>
                    <span style="color: #000000">{</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ChangeType</span> <span style="color: #a9a9a9">-eq</span> <span style="color: #008080">[Microsoft.Synchronization.Files.ChangeType]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Rename</span> <span style="color: #000000">}</span> <span style="color: #000000">{</span><span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Renamed</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$argument</span><span style="color: #a9a9a9">.</span><span style="color: #000000">OldFilePath</span><span style="color: #000000">}</span>
                <span style="color: #000000">}</span>
            <span style="color: #000000">}</span>            

            <span style="color: #ff4500">$SkippedChangeAction</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">{</span>
                <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Skipped</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">CurrentFilePath</span>            

                <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Exception</span> <span style="color: #a9a9a9">-ne</span> <span style="color: #ff4500">$null</span><span style="color: #000000">)</span>
                <span style="color: #000000">{</span>
                    <span style="color: #0000ff">Write-Error</span> <span style="color: #8b0000">'['</span> <span style="color: #8a2be2">+</span> <span style="color: #8b0000">"$($event.SourceEventArgs.Exception.Message)"</span> <span style="color: #8a2be2">+']'</span>
                <span style="color: #000000">}</span>
            <span style="color: #000000">}</span>            

            <span style="color: #006400"># Create source provider and register change events for it</span>            

            <span style="color: #ff4500">$sourceProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncProvider</span> <span style="color: #000080">-ArgumentList</span> <span style="color: #ff4500">$SourcePath</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$SourceProvider</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">AppliedChange</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$AppliedChangeAction</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$SourceProvider</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">SkippedChange</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$SkippedChangeAction</span>             

            <span style="color: #ff4500">$ApplyChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$SourceApplyChangeJob</span>            

            <span style="color: #006400"># Create destination provider and register change events for it</span>
            <span style="color: #ff4500">$destinationProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncProvider</span> <span style="color: #000080">-ArgumentList</span> <span style="color: #ff4500">$DestinationPath</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">,</span> <span style="color: #ff4500">$options</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationProvider</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">AppliedChange</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$AppliedChangeAction</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationProvider</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">SkippedChange</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$SkippedChangeAction</span>            

            <span style="color: #ff4500">$ApplyChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$DestApplyChangeJob</span>            

            <span style="color: #006400"># Use scriptblocks for the SyncCallbacks for conflicting items.</span>
            <span style="color: #ff4500">$ItemConflictAction</span> <span style="color: #a9a9a9">=</span>   <span style="color: #000000">{</span>
                <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SetResolutionAction</span><span style="color: #000000">(</span><span style="color: #008080">[Microsoft.Synchronization.ConflictResolutionAction]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">SourceWins</span><span style="color: #000000">)</span>
                <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Conflicted</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationChange</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ItemId</span>
            <span style="color: #000000">}</span>
            <span style="color: #ff4500">$ItemConstraintAction</span> <span style="color: #a9a9a9">=</span> <span style="color: #000000">{</span>
                <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SetResolutionAction</span><span style="color: #000000">(</span><span style="color: #008080">[Microsoft.Synchronization.ConstraintConflictResolutionAction]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">SourceWins</span><span style="color: #000000">)</span>
                <span style="color: #008080">[string[]]</span><span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Constrained</span> <span style="color: #a9a9a9">+=</span> <span style="color: #ff4500">$event</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceEventArgs</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationChange</span><span style="color: #a9a9a9">.</span><span style="color: #000000">ItemId</span>
            <span style="color: #000000">}</span>            

            <span style="color: #006400">#Configure the events for conflicts or constraints for the source and destination providers</span>
            <span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$destinationProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationCallbacks</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConflicting</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConflictAction</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$destinationCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConstraint</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConstraintAction</span>             

            <span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$SourceProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationCallbacks</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConflicting</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConflictAction</span>
            <span style="color: #ff4500">$AppliedChangeJobs</span> <span style="color: #a9a9a9">+=</span> <span style="color: #0000ff">Register-ObjectEvent</span> <span style="color: #000080">-InputObject</span> <span style="color: #ff4500">$sourceCallbacks</span> <span style="color: #000080">-EventName</span> <span style="color: #8a2be2">ItemConstraint</span> <span style="color: #000080">-Action</span> <span style="color: #ff4500">$ItemConstraintAction</span>             

            <span style="color: #006400"># Create the agent that will perform the file sync</span>
            <span style="color: #ff4500">$agent</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span>  <span style="color: #8a2be2">Microsoft.Synchronization.SyncOrchestrator</span>
            <span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">LocalProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$sourceProvider</span>
            <span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">RemoteProvider</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$destinationProvider</span>            

            <span style="color: #006400"># Upload changes from the source to the destination.</span>
            <span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Direction</span> <span style="color: #a9a9a9">=</span> <span style="color: #008080">[Microsoft.Synchronization.SyncDirectionOrder]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">Upload</span>            

            <span style="color: #0000ff">Write-Host</span> <span style="color: #8b0000">"Synchronizing changes from $($sourceProvider.RootDirectoryPath) to replica: $($destinationProvider.RootDirectoryPath)"</span>
            <span style="color: #ff4500">$agent</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Synchronize</span><span style="color: #000000">(</span><span style="color: #000000">)</span><span style="color: #000000">;</span>
        <span style="color: #000000">}</span>
        <span style="color: #00008b">finally</span>
        <span style="color: #000000">{</span>
            <span style="color: #006400"># Release resources.</span>
            <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$sourceProvider</span> <span style="color: #a9a9a9">-ne</span> <span style="color: #ff4500">$null</span><span style="color: #000000">)</span> <span style="color: #000000">{</span><span style="color: #ff4500">$sourceProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Dispose</span><span style="color: #000000">(</span><span style="color: #000000">)</span><span style="color: #000000">}</span>
            <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$destinationProvider</span> <span style="color: #a9a9a9">-ne</span> <span style="color: #ff4500">$null</span><span style="color: #000000">)</span> <span style="color: #000000">{</span><span style="color: #ff4500">$destinationProvider</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Dispose</span><span style="color: #000000">(</span><span style="color: #000000">)</span><span style="color: #000000">}</span>
        <span style="color: #000000">}</span>
    <span style="color: #000000">}</span>            

    <span style="color: #006400"># Set options for the synchronization session. In this case, options specify</span>
    <span style="color: #006400"># that the application will explicitly call FileSyncProvider.DetectChanges, and</span>
    <span style="color: #006400"># that items should be moved to the Recycle Bin instead of being permanently deleted.</span>            

    <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">ExplicitDetectChanges</span>
    <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecycleDeletedFiles</span>
    <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecyclePreviousFileOnUpdates</span>
    <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">=</span> <span style="color: #ff4500">$options</span> <span style="color: #a9a9a9">-bor</span> <span style="color: #008080">[Microsoft.Synchronization.Files.FileSyncOptions]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">RecycleConflictLoserFiles</span>
<span style="color: #000000">}</span>
<span style="color: #00008b">process</span>
<span style="color: #000000">{</span>
    <span style="color: #ff4500">$filter</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">Microsoft.Synchronization.Files.FileSyncScopeFilter</span>
    <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$FileNameFilter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">count</span> <span style="color: #a9a9a9">-gt</span> <span style="color: #800080">0</span><span style="color: #000000">)</span>
    <span style="color: #000000">{</span>
       <span style="color: #ff4500">$FileNameFilter</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">ForEach-Object</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">FileNameExcludes</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Add</span><span style="color: #000000">(</span><span style="color: #ff4500">$_</span><span style="color: #000000">)</span> <span style="color: #000000">}</span>
    <span style="color: #000000">}</span>
    <span style="color: #00008b">if</span> <span style="color: #000000">(</span><span style="color: #ff4500">$SubdirectoryNameFilter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">count</span> <span style="color: #a9a9a9">-gt</span> <span style="color: #800080">0</span><span style="color: #000000">)</span>
    <span style="color: #000000">{</span>
       <span style="color: #ff4500">$SubdirectoryNameFilter</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">ForEach-Object</span> <span style="color: #000000">{</span> <span style="color: #ff4500">$filter</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SubdirectoryExcludes</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Add</span><span style="color: #000000">(</span><span style="color: #ff4500">$_</span><span style="color: #000000">)</span> <span style="color: #000000">}</span>
    <span style="color: #000000">}</span>            

    <span style="color: #006400"># Perform the detect changes operation on the two file locations</span>
    <span style="color: #0000ff">Get-FileSystemChange</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #ff4500">$filter</span> <span style="color: #ff4500">$options</span>
    <span style="color: #0000ff">Get-FileSystemChange</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #ff4500">$filter</span> <span style="color: #ff4500">$options</span>            

    <span style="color: #006400"># Reporting Object - using the global scope so that it can be updated by the event scriptblocks.</span>
    <span style="color: #ff4500">$global:FileSyncReport</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">PSObject</span> <span style="color: #a9a9a9">|</span>
        <span style="color: #0000ff">Select-Object</span> <span style="color: #8a2be2">SourceStats</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">DestinationStats</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Created</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Deleted</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Overwritten</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Renamed</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Skipped</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Conflicted</span><span style="color: #a9a9a9">,</span> <span style="color: #8a2be2">Constrained</span>            

    <span style="color: #006400"># We don't need to pass any filters here, since we are using the file detection that was previously completed.</span>
    <span style="color: #006400"># this will only </span>
    <span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">SourceStats</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">Invoke-OneWayFileSync</span> <span style="color: #000080">-SourcePath</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #000080">-DestinationPath</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #000080">-Filter</span> <span style="color: #ff4500">$null</span> <span style="color: #000080">-Options</span> <span style="color: #ff4500">$options</span>
    <span style="color: #ff4500">$global:FileSyncReport</span><span style="color: #a9a9a9">.</span><span style="color: #000000">DestinationStats</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">Invoke-OneWayFileSync</span> <span style="color: #000080">-SourcePath</span> <span style="color: #ff4500">$DestinationPath</span> <span style="color: #000080">-DestinationPath</span> <span style="color: #ff4500">$SourcePath</span> <span style="color: #000080">-Filter</span> <span style="color: #ff4500">$null</span> <span style="color: #000080">-Options</span> <span style="color: #ff4500">$options</span>            

    <span style="color: #006400"># Write result to pipeline</span>
    <span style="color: #0000ff">Write-Output</span> <span style="color: #ff4500">$global:FileSyncReport</span>
<span style="color: #000000">}</span></pre>
<h3 class="PowerShellColorizedScript"><span style="color: #000000"><a href="http://download.usepowershell.com/Invoke-SyncFrameworkSample.zip" target="_blank">Download Invoke-SyncFrameworkSample.zip</a></span></h3>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/11/using-the-sync-framework-from-powershell/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>So Easy, I Could Kick Myself</title>
		<link>http://blog.usepowershell.com/2009/11/so-easy-i-could-kick-myself/</link>
		<comments>http://blog.usepowershell.com/2009/11/so-easy-i-could-kick-myself/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 21:24:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[Crystal Reports]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>
		<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/11/so-easy-i-could-kick-myself/</guid>
		<description><![CDATA[I’m updating Crystal Reports and trying to determine which reports might have been affected by some schema changes or functional changes in how the data was being stored.  The problem I’ve had is that when there are a large number of reports, it is very time consuming to open each one, look at it, and [...]]]></description>
			<content:encoded><![CDATA[<p>I’m updating Crystal Reports and trying to determine which reports might have been affected by some schema changes or functional changes in how the data was being stored. </p>
<p>The problem I’ve had is that when there are a large number of reports, it is very time consuming to open each one, look at it, and see if it contains any affected tables or views.</p>
<p>I’ve had to deal with this in my previous role as well.  After feeling the pain a few times, I turned my intern loose on the problem and shelved the problem as “just another pain in dealing with Crystal Reports”.</p>
<p>Now, I’m back dealing with Crystal Reports more frequently and in the position to have to possibly update around 30 or 40 reports that were written before I started.</p>
<p>I’ve recently had a bit of exposure to the object model for the <a href="http://msdn.microsoft.com/en-us/library/bb944221.aspx" target="_blank">.NET API for Crystal Reports</a> and thought maybe I could leverage that through <a href="http://msdn.microsoft.com/en-us/library/ms714418(VS.85).aspx" target="_blank">PowerShell</a> and whip together a quick script to help me list out the tables in each report.</p>
<p>It turned out to be painfully easy… </p>
<pre class="PowerShellColorizedScript"><span style="color: #008080">[reflection.assembly]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">LoadWithPartialName</span><span style="color: #000000">(</span><span style="color: #8b0000">'CrystalDecisions.Shared'</span><span style="color: #000000">)</span>
<span style="color: #008080">[reflection.assembly]</span><span style="color: #a9a9a9">::</span><span style="color: #000000">LoadWithPartialName</span><span style="color: #000000">(</span><span style="color: #8b0000">'CrystalDecisions.CrystalReports.Engine'</span><span style="color: #000000">)</span>
<span style="color: #ff4500">$report</span> <span style="color: #a9a9a9">=</span> <span style="color: #0000ff">New-Object</span> <span style="color: #8a2be2">CrystalDecisions.CrystalReports.Engine.ReportDocument</span>
<span style="color: #ff4500">$report</span><span style="color: #a9a9a9">.</span><span style="color: #000000">load</span><span style="color: #000000">(</span><span style="color: #ff4500">$pathToScript</span><span style="color: #000000">)</span>
<span style="color: #ff4500">$report</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Database</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Tables</span> <span style="color: #a9a9a9">|</span> <span style="color: #0000ff">Select-Object</span> <span style="color: #000080">-expand</span> <span style="color: #8a2be2">Name</span>
<span style="color: #ff4500">$report</span><span style="color: #a9a9a9">.</span><span style="color: #000000">Dispose</span><span style="color: #000000">(</span><span style="color: #000000">)</span></pre>
<p> </p>
<p>After I got the basics, I poked around and updated the script further (<a href="http://poshcode.org/1471" target="_blank">and posted it on PoshCode</a>).</p>
<p>The full script also accesses the first level of subreports and retrieves their tables as well.</p>
<p>NOTE: <a href="http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=56787567" target="_blank">Requires either the Crystal Report Runtime (Visual Studio 2008) </a> or Visual Studio to be installed.</p>
<p><strong><a href="http://download.usepowershell.com/get-crystalreporttable.ps1">DOWNLOAD UPDATED SCRIPT </a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/11/so-easy-i-could-kick-myself/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Deep Dive: Error Handling &#8211; Error Types (part 1)</title>
		<link>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/</link>
		<comments>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 12:00:00 +0000</pubDate>
		<dc:creator>Steven Murawski</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Base Class Libraries]]></category>
		<category><![CDATA[Deep Dive]]></category>
		<category><![CDATA[Error Handling]]></category>
		<category><![CDATA[PowerShell Version 1]]></category>
		<category><![CDATA[PowerShell Version 2]]></category>

		<guid isPermaLink="false">http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/</guid>
		<description><![CDATA[I started looking a little deeper at error handling in PowerShell after this StackOverflow question. PowerShell has two kinds of errors – terminating errors and non-terminating errors. Terminating errors are the errors that can stop command execution cold.  Non-terminating errors provided an additional challenge, as you need to be notified of failed operations and continue [...]]]></description>
			<content:encoded><![CDATA[<p>I started looking a little deeper at error handling in PowerShell after <a href="http://stackoverflow.com/questions/1142211/try-catch-does-not-seem-to-have-an-effect" target="_blank">this StackOverflow question</a>.</p>
<p>PowerShell has two kinds of errors – terminating errors and non-terminating errors.</p>
<p>Terminating errors are the errors that can stop command execution cold.  Non-terminating errors provided an additional challenge, as you need to be notified of failed operations and continue with pipeline operations.  To deal with this issue and to provide additional output options, PowerShell employs the concept of streams.  There are three additional streams (other than the primary pipeline stream) available in PowerShell – Verbose, Warning, and Error (and .  We’ll be concerning ourselves with the output from the Error stream.</p>
<p>There are some <a href="http://technet.microsoft.com/en-us/library/dd347675.aspx" target="_blank">automatic variables</a> in the shell that deal with errors as well.  $ErrorActionPreference is a variable that describes how PowerShell will treat non-terminating errors.  $ErrorActionPreference has four options: “Continue” (the default), “SilentlyContinue”, “Inquire”, and “Stop”.  $error is an array of all the errors that have occurred in the current session up to the number specified in $MaximumErrorCount.  $? is a boolean value that is $true if the previous operation succeeded and $false if it did not.</p>
<p>PowerShell does have some built in flexibility to turn non-terminating errors into terminating errors.  Based on our setting of $ErrorActionPreference, PowerShell will treat a non-terminating error differently.  If $ErrorActionPreference is set to &#8216;”Stop”, PowerShell will treat the errors from that scope and all sub scopes (unless explicitly overridden) as terminating errors.  If “Inquire” is chosen as the $ErrorActionPreference, then PowerShell will prompt the user at the console upon each error to ask whether it should continue (treat as non-terminating) or halt (treat as terminating), as well as providing an option to drop into a nested prompt.</p>
<p>$ErrorActionPreference is a global preference that can be set, but the PowerShell runtime also allows more granular control by providing <a href="http://technet.microsoft.com/en-us/library/dd315352.aspx" target="_blank">common parameters</a> for -ErrorAction and -ErrorVariable (shorthand -EA and –EV) which allow you to determine per cmdlet (and in V2 per advanced function) how errors should be handled.  The –ErrorAction parameter takes the same options as $ErrorActionPreference.</p>
<p>So what happens when you encounter an error?</p>
<p>If you hit a terminating error, your script, function, or command will stop.  The error will be logged to the $error variable and written to the error stream.  The $? will be set to false.  You can use the <a href="http://technet.microsoft.com/en-us/library/dd347548.aspx" target="_blank">trap construct</a> (or the <a href="http://technet.microsoft.com/en-us/library/dd315350.aspx" target="_blank">try/catch/finally construct</a> in V2) to deal with terminating errors and we’ll cover that further in the fourth post in this series.</p>
<p>If you hit a non-terminating error, the result will depend on the $ErrorActionPreference in the scope or the –ErrorAction parameter.  If the $ErrorActionPreference or –ErrorAction parameter is set to ‘Continue’, the error will be written to the error stream, added to the $error array, and the $? will be set to $false.  ‘SilentlyContinue’ will not write an error to the Error stream, but $error and $? are updated.  Finally, ‘Inquire’ provides you an option at each error as to whether to treat it as terminating or non-terminating.  If treated as a non-terminating error, the error will be treated like the $ErrorActionPreference or –ErrorAction parameter is ‘Continue’.</p>
<p>Now, I’ve mentioned that some terminating and non-terminating errors both write to the Error stream, so where does that actually get reported back to the user?  If an error has been written to the error stream, it will be written to the output stream (which may surface differently based on the PowerShell host) separately from the pipeline output unless it is redirected.  This means that the exception objects, which are how the errors are represented, are not saved to a variable if you are assigning the pipeline output to a variable.</p>
<p>For example:</p>
<p>$results = Get-WMIObject Win32_Bios –ComputerName localhost, ComputerThatDoesNotExist<br />
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)<br />
At line:1 char:22<br />
+ $results = Get-WmiObject  &lt;&lt;&lt;&lt; Win32_Bios -ComputerName localhost, ComputerThatDoesNotExist</p>
<p>creates this output to the screen, but $results will only hold the results of the successful WMI query.  This is where the –ErrorVariable comes in.  You can specify a variable to hold the exceptions generated by a particular cmdlet (or in V2 advanced function).  Specifying a variable does not remove an item from being written to the error stream and displayed as output.</p>
<p>In a console session, you can redirect the error stream with the redirection operators ( 2&gt; or 2&gt;&gt;) or you can merge the error stream with the output stream (2&gt;&amp;1) and write the output to a file, the pipeline, or assign it to a variable.</p>
<p>Up next:</p>
<ul>
<li>Error Objects</li>
<li>Tracing Errors</li>
<li>Trapping Errors</li>
<li>Reporting Errors</li>
</ul>
<p>Other great <a href="http://support.microsoft.com/kb/968929" target="_blank">PowerShell</a> Error Handling posts:</p>
<ul>
<li><a href="http://huddledmasses.org/trap-exception-in-powershell/">http://huddledmasses.org/trap-exception-in-powershell/</a></li>
<li><a href="http://blogs.msdn.com/powershell/archive/2006/11/03/erroraction-and-errorvariable.aspx">http://blogs.msdn.com/powershell/archive/2006/11/03/erroraction-and-errorvariable.aspx</a></li>
<li><a href="http://tfl09.blogspot.com/2009/01/error-handling-with-powershell.html">http://tfl09.blogspot.com/2009/01/error-handling-with-powershell.html</a></li>
<li><a href="http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx">http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx</a></li>
<li><a href="http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-11-finding-and-avoiding-errors.aspx">http://powershell.com/cs/blogs/ebook/archive/2009/03/30/chapter-11-finding-and-avoiding-errors.aspx</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.usepowershell.com/2009/07/deep-dive-error-handling-error-types-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
