<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Powershellwizard Blog</title>
  <link rel="alternate" type="text/html" href="http://blog.powershellwizard.com/" />
  <link rel="self" href="http://blog.powershellwizard.com/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2008-08-05T06:38:06.371111+02:00</updated>
  <author>
    <name>Whizgolf AB</name>
  </author>
  <subtitle>Your guide to Powershell</subtitle>
  <id>http://blog.powershellwizard.com/</id>
  <generator uri="http://dasblog.info/" version="2.1.8102.813">DasBlog</generator>
  <entry>
    <title>Workaround: The OS handle's position is not what FileStream expected, for error output as well.</title>
    <link rel="alternate" type="text/html" href="http://blog.powershellwizard.com/2008/08/05/WorkaroundTheOSHandlesPositionIsNotWhatFileStreamExpectedForErrorOutputAsWell.aspx" />
    <id>http://blog.powershellwizard.com/PermaLink,guid,dbbb6d2c-6a7b-436e-8af9-8602f536d81f.aspx</id>
    <published>2008-08-05T06:38:06.371111+02:00</published>
    <updated>2008-08-05T06:38:06.371111+02:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
  
</p>
        <p>
I recently worked with a problem with a powershell bug described over at lee holmes
blog <a href="http://www.leeholmes.com/blog/default.aspx#a45a704a3-739f-41e1-acf2-d9322e644861">here</a>,
but when trying to implement the solution he suggested, i still had problems. As it
turned out my script was generating errors and Lees blog only describes how to amend
the errors with regards to standard output. When it comes to standard error, there
is a similar fix to the problem. Here is what i added to the profile of the server
that i had problems with. 
</p>
        <p>
First the fix suggested by Lee Holmes 
</p>
        <blockquote>
          <p>
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField"<br />
$consoleHost = $host.GetType().GetField("externalHost", $bindingFlags).GetValue($host)<br />
[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost,
@())<br />
$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags)<br />
$field.SetValue($consoleHost, [Console]::Out) 
</p>
        </blockquote>
        <p>
and then my own addition to this, to make the same fix for stderror output 
</p>
        <p>
$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)<br />
$field2.SetValue($consoleHost, [Console]::Out)<br /></p>
        <p>
This was for version 1.0 of powershell. Now for CTP2.
</p>
        <blockquote>
          <p>
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" 
<br />
$objectRef = $host.GetType().GetField("externalHostRef", $bindingFlags).GetValue($host) 
</p>
          <p>
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetProperty" 
<br />
$consoleHost = $objectRef.GetType().GetProperty("Value", $bindingFlags).GetValue($objectRef,
@()) 
</p>
          <p>
[void] $consoleHost.GetType().GetProperty("IsStandardOutputRedirected", $bindingFlags).GetValue($consoleHost,
@()) 
<br />
$bindingFlags = [Reflection.BindingFlags] "Instance,NonPublic,GetField" 
<br />
$field = $consoleHost.GetType().GetField("standardOutputWriter", $bindingFlags) 
<br />
$field.SetValue($consoleHost, [Console]::Out)
</p>
        </blockquote>
        <p>
And the additions again for stderr is actually the same for CTP2 
</p>
        <p>
$field2 = $consoleHost.GetType().GetField("standardErrorWriter", $bindingFlags)<br />
$field2.SetValue($consoleHost, [Console]::Out)<br /></p>
        <p>
After implementing this i was able to actually see the errors my script was generating.
I will spare you of the embarrassing details :-)  
</p>
        <p>
Enjoy!
</p>
        <img width="0" height="0" src="http://blog.powershellwizard.com/aggbug.ashx?id=dbbb6d2c-6a7b-436e-8af9-8602f536d81f" />
        <br />
        <hr />
This weblog is sponsored by <a href="http://www.mydomainboutique.com">MyDomainBoutique</a>. 
</div>
    </content>
  </entry>
  <entry>
    <title>Write your own powershell Cmdlet - Part II</title>
    <link rel="alternate" type="text/html" href="http://blog.powershellwizard.com/2008/06/28/WriteYourOwnPowershellCmdletPartII.aspx" />
    <id>http://blog.powershellwizard.com/PermaLink,guid,8031a6c3-1ed0-45a0-b474-d9cf0a56491d.aspx</id>
    <published>2008-06-28T13:59:24.8342834+02:00</published>
    <updated>2008-06-28T14:16:06.4344436+02:00</updated>
    <category term="CmdLet" label="CmdLet" scheme="http://blog.powershellwizard.com/CategoryView,category,CmdLet.aspx" />
    <category term="Powershell" label="Powershell" scheme="http://blog.powershellwizard.com/CategoryView,category,Powershell.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
 
</p>
        <p>
In <a href="http://blog.powershellwizard.com/2008/06/22/WriteYourOwnPowershellCmdletPartI.aspx">part
I</a> of this article I did show how easy it is to create your own CmdLet using Visual
Studio 2008. We created a simple CmdLet Get-Order that returns orders from the Nortwind
database model.
</p>
        <p>
Now for the second part we will add parameters to our CmdLet to be able to filter
the orders returned by the CmdLet. Secondly we will create a CmdLets to be able to
update orders and remove them. 
</p>
        <p>
Lets see how parameters are created. If we look at the code that was first generated
for our CmdLet, there was a region called Parameters that we did not look at so far.
We will now take a look at this.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="390" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb.png" width="644" border="0" />
          </a>
        </p>
        <p>
As you can see there is one parameter defined, but the code section is commented out.
Let start by commenting the code back in. After that we rename the parameter to OrderID
and change to type to int, so we can use it to only return a specific order. We also
update the HelpMessage. We end up with the following code.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_3.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="390" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_3.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now we also need to handle the fact that an OrderID can be specified in the ProcessRecord
method. We change the code as follows. Note also that i have added a "using System.Linq"
statement in order to use the Linq syntax.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_4.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="390" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_4.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now lets try it out.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_5.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="415" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_5.png" width="644" border="0" />
          </a>
        </p>
        <p>
Looks fine. Now notice that you can still return all orders by just not specifying
an OrderID. So if you type Get-Order it still returns all orders.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_6.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="415" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_6.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now the next feature I will add to our project is a Set-Order CmdLet so we can update
an order and write the changes back to the database. Start by adding a new item to
your project. Choose powershell CmdLet and name it SetOrder. Change the generated
code as follows.
</p>
        <p>
1. Change the Verb from get to Set.<br />
2. Change the Noun from SetOrder to Order.<br />
3. Change the Name and type of the Parameter to type Order and Name Order.<br />
4. Change the mandatory property to true.<br />
5. Change the Helpmessage to something meaningful.
</p>
        <p>
We end up with the following code.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_7.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="390" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_7.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now lets add the code to ProcessRecord(). We check if the Order is already in the
database or if its new. If new we just add it to the database and otherwise we copy
all the properties of the order object. The call to dc.SubmitChanges() saves all changes.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_8.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="390" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_8.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now lets try to see if that works.
</p>
        <p>
#1 Add-PSSnapin Powershell (Adding the snapin to the shell)<br />
#2 $order = Get-Order -OrderID 11075 (Assign the order with OrderID 11075 to variable
$order)<br />
#3 $order.ShipAddress (Show the value of ShipAddress property "Starenweg 5")<br />
#4 $order.ShipAddress = "Starenweg 11" (Updating the ShipAddress)<br />
#5 Set-Order -Order $order (Call Set-Order with the modified order)<br />
#6 Call Get-Order again (We now see that the ShipAddress is modified in the datamodel)
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_9.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="415" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_9.png" width="644" border="0" />
          </a>
        </p>
        <p>
The last feature i will add to our sample is a Remove-Order CmdLet that will just
delete an order from the database. We just add another powershell CmdLet to the project
and changes some of the code generated. Now we have the following code.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_10.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="404" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_10.png" width="644" border="0" />
          </a>
        </p>
        <p>
Please note that we also deleted to Order_Details of the order we are deleting. Now
let's try if that works. 
</p>
        <p>
#1 Add-PSSnapin Powershell (Add the snapin to the shell)<br />
#2 Get-Order 11076 (Get the order with OrderID 11076. Note we use the parameter position
instead of name)<br />
#3 Remove-Order 11076 (Delete the order with OrderID 11076 from database)<br />
#4 Get-Order 11076 (Try again to get the order, but it doesn't return anything because
the order is now deleted)
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_11.png">
            <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="415" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdletPartII_10A72/image_thumb_11.png" width="644" border="0" />
          </a>
        </p>
        <p>
Now in this second part of this tutorial i have show how to add parameters to our
CmdLet. I have also shown how to update and delete data in our datamodel using powershell.
Below i have attached the sample code for both part I and II of this tutorial, so
you can check it out. Sorry for not posting the sample code for part I before now:-)
</p>
        <p>
          <a href="files/PowershellExample.zip">PowershellExample.zip</a>
        </p>
        <p>
This blog is sponsored by <a href="http://www.MyDomainBoutique.com">www.MyDomainBoutique.com</a></p>
        <img width="0" height="0" src="http://blog.powershellwizard.com/aggbug.ashx?id=8031a6c3-1ed0-45a0-b474-d9cf0a56491d" />
        <br />
        <hr />
This weblog is sponsored by <a href="http://www.mydomainboutique.com">MyDomainBoutique</a>. 
</div>
    </content>
  </entry>
  <entry>
    <title>Write your own powershell Cmdlet - Part I</title>
    <link rel="alternate" type="text/html" href="http://blog.powershellwizard.com/2008/06/22/WriteYourOwnPowershellCmdletPartI.aspx" />
    <id>http://blog.powershellwizard.com/PermaLink,guid,e6b60f2d-a9b5-4849-8662-d44da311477c.aspx</id>
    <published>2008-06-22T11:26:56.032+02:00</published>
    <updated>2008-06-29T13:28:16.8814569+02:00</updated>
    <category term="CmdLet" label="CmdLet" scheme="http://blog.powershellwizard.com/CategoryView,category,CmdLet.aspx" />
    <category term="Powershell" label="Powershell" scheme="http://blog.powershellwizard.com/CategoryView,category,Powershell.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
To start out this blog i will show how easy it is to write your own powershell Cmdlet
in a few easy to follow steps.
</p>
        <p>
We already made a sample datamodel for the Northwind sample database using the "Linq
to SQL Classes" template. It looks like this in Visual Studio 2008.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image.png">
            <img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb.png" border="0" height="384" width="634" />
          </a>
        </p>
        <p>
As you can see we have three tables from the Northwind database. The Order, Order_Detail
and the Customer tables. This will be sufficient for our demonstration.
</p>
        <p>
        </p>
        <p>
To be able to easily create a powershell Cmdlet, i have installed the Visual Studio
Windows Powershell templates from <a href="http://mschnlnine.vo.llnwd.net/d1/ch9/4/0/9/9/4/2/256835_PSTemplates.zip" target="_blank">here</a>.
It is also necessary to download and install the Windows SDK for Vista. You will need
this to install the assemblies required for Windows PowerShell. This gives me the
option to add a powershell Cmdlet to my project. First i add a new project to the
solution to hold the powershell Cmdlets. I simply call this project Powershell. I
will add all powershell Cmdlets to this project. I need also to add a reference to
my PowershellExample project here to get access to the datamodel. Next i will add
a Cmdlet using the following dialog.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_3.png">
            <img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_3.png" border="0" height="257" width="425" />
          </a>
        </p>
        <p>
I will call the first Cmdlet class GetOrder. This gives me the following code.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_4.png">
            <img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_4.png" border="0" height="382" width="629" />
          </a>
        </p>
        <p>
We need to change the string GetOrder to "Order" so that our noun will be "Order"
and our verb we can keep as VerbsCommon.Get.
</p>
        <p>
To start out i will just make my new Cmdlet return all the orders from the Order table.
After that i will show you how to add parameters and i will use this to be able to
get only specific orders.
</p>
        <p>
First of all we need a using clause, so type "using PowershellExample;" after the
rest of the using clauses. Then I add the following code to the ProcessRecord method
</p>
        <p>
DataModelDataContext dc = new DataModelDataContext(); 
</p>
        <p>
var orderlist = dc.Orders; 
<br />
foreach (var order in orderlist) 
<br />
{ 
<br />
    WriteObject(order); 
<br />
} 
</p>
        <p>
Thats all the code needed to return all orders from the Northwind database.
</p>
        <p>
          <a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_5.png">
            <img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_5.png" border="0" height="380" width="626" />
          </a>
        </p>
        <p>
So now lets get into powershell and try it out! open a powershell prompt and type
the following command. First cd into the directory which contains the powershell.dll.
in this case 
<br />
cd 'C:\Users\John Petersen\Documents\Visual Studio 2008\Projects\PowershellExample\Powershell\bin\Debug' 
<br />
and after that use installutil to register the powershell dll. 
<br />
c:\windows\Microsoft.net\Framework\v2.0.50727\installutil.exe Powershell.dll 
<br /><a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_6.png"><img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_6.png" border="0" height="408" width="633" /></a></p>
Now check if the Snapin is registered by typing the following command 
<br />
Get-PSSnapin Powershell -registered 
<p><a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_7.png"><img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_7.png" border="0" height="410" width="636" /></a></p><p>
Now to add the snapin to the active host you can type this command 
<br />
Add-PSSnapin Powershell
</p><p><a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_8.png"><img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb79ad1173f027463a9f705ef07b4c5314.png" border="0" height="168" width="640" /></a></p><p>
Now we are ready to try out the Cmdlet. Just type Get-Order and check out what happens.
</p><blockquote><p><a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_9.png"><img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumba4d34a72b6f747e29fe098fb71788179.png" border="0" height="454" width="643" /></a>  
</p><p>
Now we can see all the orders from the Northwind Order table. We can also query for
specific orders by filtering the output from Get-Order.
</p><p><a href="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_10.png"><img style="border-width: 0px;" alt="image" src="http://blog.powershellwizard.com/images/WriteyourownpowershellCmdLet_C40F/image_thumb_8.png" border="0" height="461" width="653" /></a></p><p>
Now i have shown how to easily create your own Cmdlet in visual studio 2008. Our next
task will be to extend the Cmdlet with parameters so we can get only the orders we
need, but i will save that for part II. So for now, have fun with your own Cmdlet.
</p><p>
 
</p><p><font size="2">This blogpost is sponsored by </font><a href="http://www.MyDomainBoutique.com"><font color="#000000" size="2">www.MyDomainBoutique.com</font></a></p></blockquote><img width="0" height="0" src="http://blog.powershellwizard.com/aggbug.ashx?id=e6b60f2d-a9b5-4849-8662-d44da311477c" /><br /><hr />
This weblog is sponsored by <a href="http://www.mydomainboutique.com">MyDomainBoutique</a>. 
</div>
    </content>
  </entry>
</feed>