# Sunday, June 22, 2008
| Main | Write your own powershell Cmdlet - Part ... »

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]  |  Related posts:
Write your own powershell Cmdlet - Part II