Sunday, September 23, 2007

Howto: Invoking cmdlets within a cmdlet using C# (part I: the intro)

As part of my involvement with the  PowerGadgets MVP program, I have seen users requesting a way to hide sensitive information.  They aren't looking for fool-proof methods, but something that at least hides plain text information like usernames, passwords and other sensitive information.

The Windows PowerShell SDK has some information on invoking cmdlets within a cmdlet.  I set out to figure out a way to create cmdlets that invoke other cmdlets using the SDK information.  I had some issues figuring out a few things, but with the help of Bruce Payette of the Windows PowerShell development team, I was able to find a working solution.

I'm going to use 2 cmdlets from PowerGadgets to show how this can be done.

I'm going to do this howto in 5 major parts.  First, I will do this with PowerGadgets' invoke-webservice cmdlet (and later do their invoke-sql cmdlet perhaps all in one post or over a few):

  1. Introduction (this message you're reading).
  2. When dealing with cmdlets that derive from the cmdlet class (using csc.exe).
  3. When dealing with cmdlets that *do not* derive from the cmdlet class (using csc.exe).
  4. When dealing with cmdlets that derive from the cmdlet class (using Visual Studio 2005 C# Express).
  5. When dealing with cmdlets that *do not* derive from the cmdlet class (using Visual Studio 2005 C# Express).

If there's any kind of demand for it, I could also shows how to do this via a screencast.