<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The Technical Adventures of Adam Weigert</title><link>http://weblogs.asp.net/adweigert/default.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>PowerShell: Threading Enhancements FTW!</title><link>http://weblogs.asp.net/adweigert/archive/2008/04/30/powershell-threading-enhancements-ftw.aspx</link><pubDate>Wed, 30 Apr 2008 13:11:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6144660</guid><dc:creator>adweigert</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=6144660</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2008/04/30/powershell-threading-enhancements-ftw.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;To build on the threading library I mentioned &lt;A class="" title="PowerShell: Threading for PowerShell v1.0" href="http://weblogs.asp.net/adweigert/archive/2008/04/29/powershell-threading-for-powershell-v1-0.aspx" target=_blank mce_href="http://weblogs.asp.net/adweigert/archive/2008/04/29/powershell-threading-for-powershell-v1-0.aspx"&gt;here&lt;/A&gt;, I've added some functionality to make it easier to communicate with the seperate thread. Still, keep in mind that PowerShell will only allow one pipeline to be executing in a runspace at any given time. So, these new functions can only be used while the thread is inactive. But, they provide power into setting up the thread to be run and communicating with the original runspace.&lt;/P&gt;
&lt;P mce_keep="true"&gt;The new functions are as follows:&lt;/P&gt;
&lt;P mce_keep="true"&gt;Get-ThreadVariable - Accesses available variables from within the thread.&lt;BR&gt;Set-ThreadVariable - Sets a variable for use within the thread.&lt;BR&gt;Invoke-ThreadExpression - Allows synchronous execution of a script block within the thread.&lt;/P&gt;
&lt;P mce_keep="true"&gt;I also updated the Join-Thread function to include an optional -timeout parameter so that you could return control back if the thread didn't complete in the desired time. I also updated the Running property to IsRunning and changed it so that it will only return true while the asynchronous invoke command is executing.&lt;/P&gt;
&lt;P mce_keep="true"&gt;With this new example you can do even more now, as shown here.&lt;/P&gt;&lt;PRE mce_keep="true"&gt;$thread = New-Thread&lt;BR&gt;Invoke-ThreadExpression $thread { function foo($bar) { echo "$bar!" } }&lt;BR&gt;Set-ThreadVariable $thread "name" "PowerShell Rocks"&lt;BR&gt;Start-Thread -thread $thread { $value = foo $name } | Out-Null&lt;BR&gt;Join-Thread $thread&lt;BR&gt;Get-ThreadVariable $thread "value"&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;This should return "PowerShell Rocks!" when Get-ThreadVariable is called. I've already used this script to multi-thread our Exchange backup from a single script. It is working quite nicely (nice as in have cut our 8 hour backup down to 4 hours)&amp;nbsp;and I can already imagine several other wonderful places it will be used.&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6144660" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/adweigert/attachment/6144660.ashx" length="1105" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/adweigert/archive/tags/powershell/default.aspx">powershell</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category></item><item><title>PowerShell: Threading for PowerShell v1.0</title><link>http://weblogs.asp.net/adweigert/archive/2008/04/29/powershell-threading-for-powershell-v1-0.aspx</link><pubDate>Tue, 29 Apr 2008 20:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6141977</guid><dc:creator>adweigert</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=6141977</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2008/04/29/powershell-threading-for-powershell-v1-0.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;Ok, so this solution really isn't threading, but a neat way to get async scripts to run! The important thing to remember is that each "thread" is actually a PowerShell "runspace". Meaning, scripts run within the thread don't have access to objects or functions defined outside the thread. The script is composed of the following commands:&lt;/P&gt;
&lt;P mce_keep="true"&gt;New-Thread: Creates a new instance of a thread&lt;BR&gt;Start-Thread: Invokes an async execution of a script block&lt;BR&gt;Stop-Thread: Stops a thread from running if it is still executing&lt;BR&gt;Read-Thread: Reads all errors and output to the current runspace&lt;BR&gt;Join-Thread: Waits for the thread to complete and reads off the errors and output to the current runspace&lt;/P&gt;
&lt;P mce_keep="true"&gt;A simple example of how to use this is as follows.&lt;/P&gt;&lt;PRE mce_keep="true"&gt;$thread = Start-Thread {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach ($i in (1..5)) {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo "Sleeping for $i seconds..."&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Start-Sleep&amp;nbsp;$i&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/PRE&gt;&lt;PRE mce_keep="true"&gt;# ... do some work here or spawn other threads&lt;/PRE&gt;&lt;PRE mce_keep="true"&gt;Join-Thread $thread&lt;/PRE&gt;
&lt;P mce_keep="true"&gt;I have some other ideas around injecting "variables" and being able to run script blocks within the new "thread" but I'll save that for a later post ...&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6141977" width="1" height="1"&gt;</description><enclosure url="http://weblogs.asp.net/adweigert/attachment/6141977.ashx" length="841" type="application/x-zip-compressed" /><category domain="http://weblogs.asp.net/adweigert/archive/tags/powershell/default.aspx">powershell</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category></item><item><title>Windows Server 2008 / Vista - Network Scalability "Feature"</title><link>http://weblogs.asp.net/adweigert/archive/2008/03/15/windows-server-2008-vista-network-scalability-quot-feature-quot.aspx</link><pubDate>Sun, 16 Mar 2008 00:05:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5978476</guid><dc:creator>adweigert</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=5978476</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2008/03/15/windows-server-2008-vista-network-scalability-quot-feature-quot.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I highly recommend, if you are running Windows Server 2008 or Windows Vista, to disable the network scalability features. If you don't, you run the chance of one day running into "slow" performance of an application between one client or server and another. You will spend countless hours trying to debug why the network communication is so slow to that feature from that one client when it works fine for you and serveral others in the same area and with the same configuration. As far as I am concerned, TCP offloading has been a failure since the beginning, and I am not sure who is to blame, Microsoft or the hardware vendors. Oddly enough, I even saw the "symptoms" on a virtual server.&lt;/P&gt;
&lt;P mce_keep="true"&gt;From an Administrator command prompt:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;&lt;FONT size=2&gt;netsh int tcp set global rss=disabled chimney=disabled autotuninglevel=disabled&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P mce_keep="true"&gt;Enjoy!&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5978476" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Network+Scaliblity/default.aspx">Network Scaliblity</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Windows+Server+2008/default.aspx">Windows Server 2008</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Windows+Vista/default.aspx">Windows Vista</category></item><item><title>C#: My First Lambda Expression</title><link>http://weblogs.asp.net/adweigert/archive/2007/12/16/c-my-first-lamba-expression.aspx</link><pubDate>Sun, 16 Dec 2007 12:56:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5461335</guid><dc:creator>adweigert</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=5461335</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2007/12/16/c-my-first-lamba-expression.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;I don't know about anyone else, but I found it annoying to have to put on&amp;nbsp;three-lines of code (or one ugly one-line of code) for an&amp;nbsp;IF statement for argument validation. Mostly, I want to check a simple condition and if true, throw an exception. Well, lambda to the rescue! I find the lambda version much more readable than a one-line IF statement, but that is just me -- mainly because I dislike one-line IF statements.&lt;/P&gt;
&lt;P mce_keep="true"&gt;Another advantage of the lamba expression is that the "new Exception" is only created if the delegate is called when the condition is true. Who knows, maybe I'll change my mind on liking this after I have more experience with .NET 3.5, but for now I think this is very cool...&lt;/P&gt;
&lt;P mce_keep="true"&gt;I guess this will have to do until we have MACRO replacement in C# ... :)&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: Lucida Console, Courier New, Courier, monospace"&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;delegate&lt;/FONT&gt; T &lt;/FONT&gt;&lt;FONT color=#2b91af&gt;CreateObjectDelegate&lt;/FONT&gt;&amp;lt;T&amp;gt;();&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;Validator&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; ThrowIf(&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;bool&lt;/FONT&gt; condition, &lt;/FONT&gt;&lt;FONT color=#2b91af&gt;CreateObjectDelegate&lt;/FONT&gt;&amp;lt;&lt;/FONT&gt;&lt;FONT color=#2b91af&gt;Exception&lt;/FONT&gt;&amp;gt; createObject)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (condition)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;throw&lt;/FONT&gt; createObject();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;Example&lt;BR&gt;&lt;/FONT&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;void&lt;/FONT&gt; ShowName(&lt;/FONT&gt;&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; name)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;// LAMBA expression&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#2b91af&gt;Validator&lt;/FONT&gt;.ThrowIf(&lt;/FONT&gt;name.IsNullOrEmpty(), () =&amp;gt; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;ArgumentException&lt;/FONT&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515&gt;"The parameter is required."&lt;/FONT&gt;, &lt;/FONT&gt;&lt;FONT color=#a31515&gt;"name"&lt;/FONT&gt;));&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#008000&gt;// IF statement&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;if&lt;/FONT&gt; (&lt;/FONT&gt;name.IsNullOrEmpty()) &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;throw&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;ArgumentException&lt;/FONT&gt;(&lt;/FONT&gt;&lt;FONT color=#a31515&gt;"The parameter is required."&lt;/FONT&gt;, &lt;/FONT&gt;&lt;FONT color=#a31515&gt;"name"&lt;/FONT&gt;);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#2b91af&gt;Console&lt;/FONT&gt;.WriteLine(name);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5461335" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>C#: My First Extension Method</title><link>http://weblogs.asp.net/adweigert/archive/2007/12/08/c-my-first-extension-method.aspx</link><pubDate>Sat, 08 Dec 2007 15:01:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:5422348</guid><dc:creator>adweigert</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=5422348</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2007/12/08/c-my-first-extension-method.aspx#comments</comments><description>&lt;P&gt;I will find many, many uses for this ... maybe someone else will too! &lt;/P&gt;
&lt;P style="FONT-FAMILY: Lucida Console; Courier: "&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System;&lt;BR&gt;&lt;FONT color=#0000ff&gt;using&lt;/FONT&gt; System.Diagnostics;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;internal&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;class&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;StringExtensions&lt;/FONT&gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;public&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;static&lt;/FONT&gt; T ToEnum&amp;lt;T&amp;gt;(&lt;FONT color=#0000ff&gt;this&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;string&lt;/FONT&gt; value)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;where&lt;/FONT&gt; T : &lt;FONT color=#0000ff&gt;struct&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#2b91af&gt;Debug&lt;/FONT&gt;.Assert(!&lt;FONT color=#0000ff&gt;string&lt;/FONT&gt;.IsNullOrEmpty(value));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;return&lt;/FONT&gt; (T)&lt;FONT color=#2b91af&gt;Enum&lt;/FONT&gt;.Parse(&lt;FONT color=#0000ff&gt;typeof&lt;/FONT&gt;(T), value, &lt;FONT color=#0000ff&gt;true&lt;/FONT&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=5422348" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Orcas/default.aspx">Orcas</category></item><item><title>PowerShell: Try...Catch...Finally Comes To Life</title><link>http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx</link><pubDate>Wed, 10 Oct 2007 21:18:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4518796</guid><dc:creator>adweigert</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=4518796</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2007/10/10/powershell-try-catch-finally-comes-to-life.aspx#comments</comments><description>&lt;P mce_keep="true"&gt;So, PowerShell has some good error handling, but being so used to .NET, I really missed my Try...Catch...Finally statements. Especially when I needed to make sure a block of code always executed. Well, after some playing, I think I have the solution! I've tested this function in a few different ways. I hope this turns out to be as helpful to someone else as it is for me. Maybe Microsoft will add this functionality&amp;nbsp;to the core of PowerShell.&lt;/P&gt;
&lt;BLOCKQUOTE style="FONT-FAMILY: courier,monospace"&gt;
&lt;P mce_keep="true"&gt;function Try&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; param&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ScriptBlock]$Command = $(throw "The parameter -Command is required."),&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ScriptBlock]$Catch&amp;nbsp;&amp;nbsp; = { throw $_ },&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [ScriptBlock]$Finally = {}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $local:ErrorActionPreference = "SilentlyContinue"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trap&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trap&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trap { throw $_ }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;$Finally&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; throw $_&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $_ | &amp;amp; { &amp;amp;$Catch }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;$Command&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P mce_keep="true"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; trap { throw $_ }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;$Finally&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/P&gt;
&lt;P mce_keep="true"&gt;# Example usage&amp;nbsp;&lt;/P&gt;
&lt;P mce_keep="true"&gt;Try {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo " ::Do some work..."&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo " ::Try divide by zero: $(0/0)"&lt;BR&gt;} -Catch {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo "&amp;nbsp; ::Cannot handle the error (will rethrow): $_"&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #throw $_&lt;BR&gt;} -Finally {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; echo " ::Cleanup resources..."&lt;BR&gt;}&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4518796" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/powershell/default.aspx">powershell</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category></item><item><title>PowerShell: Using PowerShell to Debug .NET Class Libraries</title><link>http://weblogs.asp.net/adweigert/archive/2007/08/20/powershell-using-powershell-to-debug-net-class-libraries.aspx</link><pubDate>Mon, 20 Aug 2007 19:50:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:3568563</guid><dc:creator>adweigert</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=3568563</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2007/08/20/powershell-using-powershell-to-debug-net-class-libraries.aspx#comments</comments><description>&lt;P&gt;PowerShell is so wicked cool. I love it more and more each day I use it. Latest trick is using it to debug or my class libraries without having to have a supporting unit test or console project.&lt;/P&gt;
&lt;P&gt;Though this method of testing / debuging process isn't as good as unit testing, it sure is handy for some small client utilities or where you just want to "try" something in one of your class libraries without changing all your unit tests.&lt;/P&gt;
&lt;P&gt;What you need to do is set your project to launch an external command and point it to your PowerShell installation, typically: &lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;The command line arguments for your debug external program should be: &lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;-NoExit -command "&amp;amp; { . .\Debug.ps1 }"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Then, add a file to your project called Debug.ps1, make sure you set it to copy to the output directory.&lt;/P&gt;
&lt;P&gt;When you run your class library in debug mode it will launch the PowerShell console and then execute the Debug.ps1 file. My file contains something like the following to load my assembly and a few functions to automate some of the things I might do while debugging. After this script runs, you will be left with a fully functioning PowerShell instance that you can use to further test your class library.&lt;/P&gt;
&lt;P&gt;Did I mention this is an awesome trick for building custom PowerShell commands? Well, I'm pretty sure it is, but I have not gotten that far, yet.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;#Debug.ps1&lt;BR&gt;[System.AppDomain]::CurrentDomain.AppendPrivatePath($PWD)&lt;BR&gt;[System.Reflection.Assembly]::LoadFrom("DotDeploy.dll")&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;function global:New-DeploymentDefinition&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return New-Object DotDeploy.DeploymentDefinition&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;function global:New-DeploymentCopyFileTask&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; param (&amp;nbsp;[string]$sourcePath, [string]$destinationPath )&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return New-Object DotDeploy.DeploymentCopyFileTask $source,$destination&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;function global:New-DeploymentServer&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; param ([string]$server, [int]$port)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return New-Object DotDeployt.DeploymentServer $server,$port&lt;BR&gt;}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: 'Calibri','sans-serif'"&gt;$source = New-DeploymentServer "MARIO",9216&lt;BR&gt;$destination = New-DeploymentServer "LUIGI",9216&lt;BR&gt;$deployment = New-DeploymentDefinition&lt;BR&gt;$task = New-DeploymentCopyFileTask "C:\Program Files\DotDeploy\v1.0\Configuration" "C:\Program Files\DotDeploy\v1.0\Configuration"&lt;BR&gt;$task.Recursive = $true&lt;BR&gt;$task.InputFilter = "*.xml"&lt;BR&gt;$deployment.Tasks.Add($task)&lt;BR&gt;$deployment.Source = $source&lt;BR&gt;$deployment.Destination = $destination&lt;BR&gt;$destination.Lock()&lt;BR&gt;$deployment.Deploy()&lt;BR&gt;$destination.Unlock()&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;EM&gt;Update Note: Make sure when you load your assembly you use &lt;STRONG&gt;LoadFrom&lt;/STRONG&gt;&amp;nbsp;and not &lt;STRONG&gt;LoadFile&lt;/STRONG&gt; becasue LoadFile will not resolve dependencies but LoadFrom does. You can also append a private path to the current AppDomain as shown (added)&amp;nbsp;in the example.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=3568563" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/powershell/default.aspx">powershell</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/Debugging/default.aspx">Debugging</category></item><item><title>PowerShell: Convert Active Directory IADSLargeInteger to System.Int64</title><link>http://weblogs.asp.net/adweigert/archive/2007/03/23/powershell-convert-active-directory-iadslargeinteger-to-system-int64.aspx</link><pubDate>Fri, 23 Mar 2007 14:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:2083767</guid><dc:creator>adweigert</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=2083767</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2007/03/23/powershell-convert-active-directory-iadslargeinteger-to-system-int64.aspx#comments</comments><description>&lt;p&gt;This PowerShell function will convert an IADSLargeInteger ComObject to a long/Int64 value. Extremely helpful when trying to work with Active Directory attributes like &amp;quot;pwdLastSet&amp;quot; or &amp;quot;lastLogonTimestamp&amp;quot;.&lt;/p&gt;&lt;pre style="font-size: 90%"&gt;function ConvertADSLargeInteger([object] $adsLargeInteger)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp; $highPart = $adsLargeInteger.GetType().InvokeMember(&amp;quot;HighPart&amp;quot;, [System.Reflection.BindingFlags]::GetProperty, $null, $adsLargeInteger, $null)
&amp;nbsp;&amp;nbsp;&amp;nbsp; $lowPart&amp;nbsp; = $adsLargeInteger.GetType().InvokeMember(&amp;quot;LowPart&amp;quot;,&amp;nbsp; [System.Reflection.BindingFlags]::GetProperty, $null, $adsLargeInteger, $null)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $bytes = [System.BitConverter]::GetBytes($highPart)
&amp;nbsp;&amp;nbsp;&amp;nbsp; $tmp&amp;nbsp;&amp;nbsp; = [System.Byte[]]@(0,0,0,0,0,0,0,0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; [System.Array]::Copy($bytes, 0, $tmp, 4, 4)
&amp;nbsp;&amp;nbsp;&amp;nbsp; $highPart = [System.BitConverter]::ToInt64($tmp, 0)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; $bytes = [System.BitConverter]::GetBytes($lowPart)
&amp;nbsp;&amp;nbsp;&amp;nbsp; $lowPart = [System.BitConverter]::ToUInt32($bytes, 0)
&amp;nbsp;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return $lowPart + $highPart
}&lt;/pre&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=2083767" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/active+directory/default.aspx">active directory</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/powershell/default.aspx">powershell</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/.net/default.aspx">.net</category></item><item><title>SqlViewState - The Path To Better ViewState Storage</title><link>http://weblogs.asp.net/adweigert/archive/2004/03/09/sqlviewstate-the-path-to-better-viewstate-storage.aspx</link><pubDate>Tue, 09 Mar 2004 17:14:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:86628</guid><dc:creator>adweigert</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/adweigert/rsscomments.aspx?PostID=86628</wfw:commentRss><comments>http://weblogs.asp.net/adweigert/archive/2004/03/09/sqlviewstate-the-path-to-better-viewstate-storage.aspx#comments</comments><description>&lt;p&gt;A while ago, several colleges of mine were having a terrible time with the loading times of some pages of their web application. Come to find out, they were suffering from ViewState bloat. ViewState was something I always tried to stay away from in the past because of this factor. Sure, it was very helpful but I found ways around it.&lt;/p&gt;&lt;p&gt;However, I have come to a change of heart to realize ViewState is a wonderful thing, except that it can still cause pages to suffer from the ViewState bloat symptom. You could be a frugal programmer and control the ViewState more by only allowing certain pieces into but there are times when you want to store large amounts of data in the ViewState. For example, you may have search results that were very costly to find and you need to persist the results for paging.&lt;/p&gt;&lt;p&gt;So, I started looking at different devices to store the ViewState in. I thought of putting it into our session state which is stored in a SQL database, but the timeout settings for session and page view state were very different in some cases and this method would not allow for the flexibility that is required. Or perhaps someone avoids session state like the plague or it isn&amp;#39;t stored in a central repository in case of a web farm approach.&lt;/p&gt;&lt;p&gt;The decision was easy, I would create a small table that stores ViewState in it much like the session state. I actually put the table and procedures in the same database as our session state just for consistency. It was also easy to script the job that cleans up timed out ViewState since we already had one for the session state. Here is the install script you will need to run to setup the table, stored procedures, and job to clean up the table every so often.&lt;/p&gt;&lt;p&gt;&lt;a href="http://weblogs.asp.net/adweigert/archive/2005/07/11/419029.aspx" target="_blank"&gt;[Database Code]&lt;/a&gt;&lt;/p&gt;&lt;p&gt;To keep it simple I simply look for one value, the connection string to use in an appSetting key called &amp;ldquo;ViewStateConnectionString&amp;ldquo;. Optionally, you can define a &amp;ldquo;ViewStateTimeout&amp;ldquo; with a value being the number of minutes to wait before clearing the ViewState data. You can also set this value per page if you have certain pages that will live longer on the client before a post back. If the connection string is not provided, it will not affect the ViewState processing as it will resort to the old method. The following is an example of the appSettings section.&lt;/p&gt;&lt;p align="left" class="MsoNormal" style="margin: 0in 0in 0pt"&gt;&lt;span style="font-size: 9pt; color: blue; font-family: 'Lucida Console'"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-size: 9pt; color: maroon; font-family: 'Lucida Console'"&gt;appSettings&lt;/span&gt;&lt;span style="font-size: 9pt; color: blue; font-family: 'Lucida Console'"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p align="left" class="MsoNormal" style="margin: 0in 0in 0pt"&gt;&lt;span style="font-size: 9pt; font-family: 'Lucida Console'"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;add&lt;/span&gt;&lt;span style="color: fuchsia"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ViewStateConnectionString&amp;quot;&lt;/span&gt;&lt;span style="color: fuchsia"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Server=(local);Database=ASPState;Trusted_Connection=yes;&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p align="left" class="MsoNormal" style="margin: 0in 0in 0pt"&gt;&lt;span style="font-size: 9pt; font-family: 'Lucida Console'"&gt;&lt;span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon"&gt;add&lt;/span&gt;&lt;span style="color: fuchsia"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;key&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;ViewStateTimeout&amp;quot;&lt;/span&gt;&lt;span style="color: fuchsia"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;value&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;20&amp;quot;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p align="left" class="MsoNormal" style="margin: 0in 0in 0pt"&gt;&lt;span style="font-size: 9pt; color: blue; font-family: 'Lucida Console'"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="font-size: 9pt; color: maroon; font-family: 'Lucida Console'"&gt;appSettings&lt;/span&gt;&lt;span style="font-size: 9pt; color: blue; font-family: 'Lucida Console'"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Here is the class that you will need to inherit from if you want to take advantage of this ViewState change, provided for you in the wonderful language of C#. I apologize for the lack of comments, hopefully it is self explanatory.&lt;/p&gt;&lt;p&gt;&lt;a href="http://weblogs.asp.net/adweigert/archive/2005/07/11/419030.aspx" target="_blank"&gt;[C# Code]&lt;/a&gt;&lt;/p&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=86628" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/adweigert/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/viewstate/default.aspx">viewstate</category><category domain="http://weblogs.asp.net/adweigert/archive/tags/sql/default.aspx">sql</category></item></channel></rss>