# Saturday, June 28, 2008

 

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.

image

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.

image

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.

image

Now lets try it out.

image

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.

image

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.

image

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.

image

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)

image

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.

image

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)

image

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

Saturday, June 28, 2008 12:59:24 PM (W. Europe Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Sunday, June 22, 2008

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.

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.

image

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.

To be able to easily create a powershell Cmdlet, i have installed the Visual Studio Windows Powershell templates from here. 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.

image

I will call the first Cmdlet class GetOrder. This gives me the following code.

image

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.

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.

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

DataModelDataContext dc = new DataModelDataContext();

var orderlist = dc.Orders;
foreach (var order in orderlist)
{
    WriteObject(order);
}

Thats all the code needed to return all orders from the Northwind database.

image

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
cd 'C:\Users\John Petersen\Documents\Visual Studio 2008\Projects\PowershellExample\Powershell\bin\Debug'
and after that use installutil to register the powershell dll.
c:\windows\Microsoft.net\Framework\v2.0.50727\installutil.exe Powershell.dll
image

Now check if the Snapin is registered by typing the following command
Get-PSSnapin Powershell -registered

image

Now to add the snapin to the active host you can type this command
Add-PSSnapin Powershell

image

Now we are ready to try out the Cmdlet. Just type Get-Order and check out what happens.

image 

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.

image

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.

 

This blogpost is sponsored by www.MyDomainBoutique.com

Sunday, June 22, 2008 10:26:56 AM (W. Europe Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |