Many of us are involved in writing scripts, be it for development or testing or deployment.
We make use of different scripting languages. One of them is Powershell.
As the name suggests, it’s really powerful.
You can accomplish so many things in Powershell. But what if you already have something developed in .NET and have an Assembly (remember *.dll file) available with you.
Would you like to mimmick everything in Powershell? Or, would you wish if same .NET assembly can be reused?
I fall in latter category wherever possible. 🙂
Yes, You can reuse .NET library.
Aahhh!!! Great!!! Sounds interesting!!!!
Many of us are aware of this and may be, few of us are not.
Why am i writing this?
I was working on Automating or writing a workflow to deploy Virtual Machines (aka, Persistent VM Role) on Microsoft Azure Cloud.
I did it using Powershell script(You can see a lot of support and sample Powershell scripts already available on MS community sites).
That became simple. However, that’s not all for me.
I am hungry :), Hunger to understand things, go till the roots.
I wanted to understand the code working behind the scenes.
Read this post further…
What are Powershell CmdLets?
In actual, Powershell cmdlets are actually exposed through .NET assemblies only. Bunch of assemblies targetting .NET framework execute to get as results which we want.
If you have worked in .NET, you would have came across Attributes. Yeah, that is how Powershell CMDLETS are exposed.
Classes and fields/parameters are attributed with CmdLet and Parameter.
That’s it. Powershell execution engine can now load these types and execute them.
Bottomline is: Cmdlets are classes annotated with Cmdlet attribute.
How to decompile?
Now, we know that it’s actually a .NET Type in .NET assembly which is getting things done. We all know how to decompile .NET assembly.
We may use 3rd Party Tools, some are free while some are not.
This is not a big deal.
However, how do you identify and locate the Assembly containing this specific CMDLET?
You may say that you are not CLR which is responsible for locating, loading and executing the types besides other things.
Then HOW, you may ask.
For this, we’ll again make use of Powershell Command Prompt.
Open up Command Prompt and execute the following command:
$commandDetailsObj is how you declare a variable in Powershell,
Get-Command is another Powershell cmdlet, gcm is an alias of this cmdlet,
nameOfCommand is the name of cmdlet which you want to decompile. Say, Add-AzureAccount
The above command will get the details about the cmdlet and store it in $commandDetailsObj variable.
Since cmdlet name can actually be an alias to an actual cmdlet, we keep on doing the below till we get the actual command.
while($commandDetailsObj.CommandType -eq "Alias")
$commandDetailsObj = Get-Command ($commandDetailsObj.definition)
Next is, we want to get the Type exposed as CMDLET. Issue the following command:
The above command after executing will print the Full Qualified Class Name in the console.
Next is, we want to get the Assembly(DLL) name containing the Type exposed as CMDLET. Issue the following command:
The above command after executing will print the Full path of the Assembly in the console.
With the above information, we can now open this dll in any of the .NET Decompilation Tool to view the code.
This article doesn’t tell what you want? Need help? Contact me.