Lately I’ve been working on trying to author PowerShell cmdlets with Visual Studio 2010 to talk to a web service. At first glance, I thought it would be easy. I had the WSDL and XSD files, and the webservice side of things is already working. There’s a web GUI that communicates via SOAP to the underlying hardware & software — building some cmdlets should be a snap, or so I thought.
First up, I must admit that I have zero C# experience. I’ve got a good amount of object oriented programming and scripting experience, so the concept of looping, conditionals, arrays, objects, methods, properties, etc. are not foreign to me. I’ve always believed, all things being equal, that programming is essentially all the same. Just like a spoken language, you have to understand the concepts of communicating effectively with other people and concepts such as I/you/us/them, adjectives, adverbs — only the sounds your mouth makes changes. Programming is very much the same way: same concepts for the most part but the language changes. With that semi-naive belief in hand, I bravely started Googling and reading MSDN.
One thing I learned rather quickly is that there is a wealth of information about .NET frameworks, different types of web service communication models, tons of little tutorials about building cmdlets that talk to webservices to get the weather or cmdlets to get and kill a local process, but there was very little clarification. There’s nothing that I found that simply said I must use .NET framework X and web service model Y to accomplish task Z. There’s very little information out there about building PowerShell cmdlets, too. Sure, there’s a metric ton of great resources that discuss the fine art of PowerShell scripting, but very little in the way of actually teaching someone how to author a PowerShell cmdlet. Not to mention any clarification on which .NET framework to use to maximize the ease of installing. If I target my Visual Studio project towards .NET 4, does that mean someone needs to have .NET 4 installed in order to load my PowerShell module or snapin? I’ve spent a couple days Googling, searching my Safari account, asking various people on Twitter, but I have yet to find “The Definitive Guide To Authoring PowerShell CmdLets for Idiots Like Damian Karlson”. In addition to the macro-confusion I was experiencing, there’s also a ton of micro-confusion. What model should I be using to talk to my webservice? Windows Communication Foundation (WCF)? Do I add a Service Reference to my project in Visual Studio, or a Web Reference, or generate the interface using wsdl.exe and add it as a Reference? How in the heck do I actually REFER to this reference in order to say hello to my little piece of hardware?
If you can’t tell by now, I’m a bit frustrated. I didn’t expect it to be “easy as 1 2 3”, but I didn’t expect to have to spend so much effort searching to find few helpful examples. I know that folks are doing exactly the same thing that I want to do. I know VMware has PowerCLI and NetApp has their PowerShell for OnTAP, etc. and etc. Why is there so few resources on how to develop something like that?
So, in closing, I’d like to thank you for suffering through my complaining. If you happen to know of anything or anyone that might be able to point me in the right direction, please drop me a comment or hit me up on Twitter. I’ll probably end up compiling the helpful resources I have found into a page of sorts for the next person who comes stumbling along, in the hopes that they’ll not feel the same frustration I’ve been feeling. Thanks again. 🙂
D
samit manna says
Hi,
I have a c# script which consumes a web service which have built using svcutil. I have copied both .cs and app.conig file to the program. I want to create powershell cmdlet for this script. I have created cmdlet using system.management.automation. but it’s not working. Pls help me to biuld cmdlet.
Magnus Ullberg says
I agree, there is not a lot of information out there but I did find two links that explain it pretty well.
http://blogs.msdn.com/b/saveenr/archive/2010/03/08/how-to-create-a-powershell-2-0-module-and-cmdlet-with-visual-studio-2010-screencast-included.aspx
and
http://bartdesmet.net/blogs/bart/archive/2008/02/03/easy-windows-powershell-cmdlet-development-and-debugging.aspx
After reading those two pages i was able to create a DLL that i could import in to powershell and have my own commands show up correctly.
Carter Shanklin says
If you’re not a C# sort of person you might consider advanced functions (formerly known as script cmdlets). You can do most of what a compiled cmdlet can do in pure script.
Check out the VI Toolkit Extensions for a bunch of examples. Warning, that stuff hasn’t been updated in a long time but should get you started.
Damian Karlson says
Thanks for the response, Carter. I’ll check it out.