In part I 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.
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.
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.
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.
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.
Now lets try it out.
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.

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.
1. Change the Verb from get to Set.
2. Change the Noun from SetOrder to Order.
3. Change the Name and type of the Parameter to type Order and Name Order.
4. Change the mandatory property to true.
5. Change the Helpmessage to something meaningful.
We end up with the following code.

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.

Now lets try to see if that works.
#1 Add-PSSnapin Powershell (Adding the snapin to the shell)
#2 $order = Get-Order -OrderID 11075 (Assign the order with OrderID 11075 to variable $order)
#3 $order.ShipAddress (Show the value of ShipAddress property "Starenweg 5")
#4 $order.ShipAddress = "Starenweg 11" (Updating the ShipAddress)
#5 Set-Order -Order $order (Call Set-Order with the modified order)
#6 Call Get-Order again (We now see that the ShipAddress is modified in the datamodel)

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.
Please note that we also deleted to Order_Details of the order we are deleting. Now let's try if that works.
#1 Add-PSSnapin Powershell (Add the snapin to the shell)
#2 Get-Order 11076 (Get the order with OrderID 11076. Note we use the parameter position instead of name)
#3 Remove-Order 11076 (Delete the order with OrderID 11076 from database)
#4 Get-Order 11076 (Try again to get the order, but it doesn't return anything because the order is now deleted)
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
PowershellExample.zip
This blog is sponsored by www.MyDomainBoutique.com