Starting with version 18.104.22.168, ISESteroids has the capability to convert static .NET methods into PowerShell functions. Let's see what that is and why it could be useful.
Accessing Static .NET Methods
To access a static .NET method, not much is needed. You need the .NET type that is providing the methods, add two colons (::), and immediately, the ISE editors IntelliSense displays what's in that type.
Here are a couple of examples. The "Math" type helps with arithmetic operartions:
Getting the lower of two numbers
The "IO.Path" type assists with common path operations:
|PS> [IO.Path]::ChangeExtension('c:\test.txt', 'bak')
Changing the file extension of a path
And "Net.DNS" helps you query the DNS. This example finds the currently assigned IP address(es):
HostName Aliases AddressList
Getting the current IP address(es)
To find out more, send a .NET type off to Get-Member:
|PS> [IO.Path] | Get–Member –Static
Name MemberType Definition
Listing static members of a type
Cmdlets are .NET
The results you get from .NET methods look surprisingly similar to what cmdlets output, and that's actually not that much of a surprise. Cmdlets are .NET code. And if you run ISESteroids, you can actually "see" this code.
Simply right-click any cmdlet, then in the enhanced context menu, choose "Source Code" and then "Decompile". You will be automatically taken to the relevant source code.
Funny enough, that also works with .NET methods like the ones you have just seen in the examples. So if you enter the examples into ISESteroids, and right-click for example "GetHostByName", then you can also look at the source code of these methods.
So the truth is: PowerShell is really working most of the time with .NET code. It is a thin wrapper around it, and cmdlets "civilize" .NET code and make it behave in a predictable and error-handled way.
Turning .NET Code into PowerShell
While you can happily use .NET code directly in your scripts (like shown in the examples before), it may sometimes be a better idea to wrap them into PowerShell functions. PowerShell functions really are cmdlets. They are just not binary, but otherwise provide the very same functionality and end-user experience.
This is when ISESteroids comes into play. It can do the work for you. Try this:
Enter the following code:
Pick a static .NET method…
The code above is pretty much what ISEs IntelliSense will type for you. Now to the magic part.
Right-click "SetEnvironmentVariable", and in the enhanced context menu, choose "Refactor", and then "Turn Method into Function". That's it. ISESteroids quickly generates the following for you:
Converting method "SetEnvironmentVariable" of type [System.Environment] to PowerShell
2 overloads found.
created 2 PowerShell functions.
PowerShell code auto-created by ISESteroids 2.0 Enterprise
# Overload #1:
# Overload #2:
…autogenerate fully error-handled and pipeline-aware PowerShell functions
Now the resulting functions work just like cmdlets. Simply run the script to define the functions.
Wrapping .NET code into PowerShell functions has a number of advantages. Aside from error handling and pipeline-awareness, you now even get Intellisense!
Try entering this (after you ran the script above and defined the new functions):
|PS> Set–EnvironmentVariable –variable a –value 12 –target <#WATCH INTELLISENSE KICK IN HERE!#>|
Full Intellisense Support
Completing the Magic: Turn Function Into Module
Now that you have PowerShell functions, all that's missing is how to save them to a PowerShell module, so in the future, the new functions will be available for any PowerShell instance on your system (of course, it can be nice at times to show off a module to friends and familiy, too).
To create a new PowerShell module and store the function in it, first pick the function you want to export to a module. Note that the Code autogenerator in ISESteroids may create a bunch of functions with different signatures. Since they are all named equal, you need to choose one (or rename the others).
Next, right-click the keyword "function" in ISESteroids, and choose "Export to Module". A dialog opens, and you can now export the function to a brand new module, or add it to an existing one. Isn't that cool?
The coolness even rises, because once the module is created, the exported function is immediately available in all open PowerShell environments.
ISESteroids – Get Your Copy!
ISESteroids grows every day, and you can be part of it. Grab yourself a free trial, and if you like what you see, get a license. Licenses are cheap, compared to what you get and what others charge, and license revenues are vital for us to be able to constantly expand and improve ISESteroids.
So since ISESteroids licenses come with free unlimited updates for the version you buy, your license investment is actually coming back to you through free updates and extensions.
May the PowerShell be with you,