If you've been following this blog recently, you've read about my fun with PowerShell type extensions. This technique lets you make PowerShell give you the information you want without a lot of work on your part. Well, there is some work but you only have to do it once. To make it even easier, I have been working on a module to simplify this even further. The module is still in beta so I'm hoping some of you will kick it around before I publish it to the PowerShell Gallery.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
The module, PSTypeExtensionTools, is available on Github at https://github.com/jdhitsolutions/PSTypeExtensionTools. Download the latest beta release zip file and extract it to a working directory. Unless you put it in one of the pre-defined module directories, import the module: Import-Module c:\work\pstypeextensiontools.
The command to create new type extensions is Add-PSTypeExtension. You specify the type name, the member type, a name and a value. Underneath the hood this command is running Update-TypeData but I wanted to simplify the process a bit and take advantage of some pipeling processes. For example, you may not know off the top of your head the type name of the object you want to enhance, or it is a long type name and you prefer not to type it. Get-PSType will return the type name.
I did this so I (meaning you) could run a command like this to add a new type extension.
123 | Get-PSType | Add-PSTypeExtension -MemberType ScriptProperty -MemberName SquareRoot -Value { [math]::Sqrt($this)}
You can create any property type except CodeProperty and CodeMethod. These are bit more complicated and move, in my opinion, outside the realm of scripting. If you need to define these types of members, you don't need this module. Of course, once you know the type name you can create as many extensions as you'd like.
Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Squared -value { $this*$this} Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Cubed -value { [math]::Pow($this,3)} Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptProperty -MemberName Value -value { $this} Add-PSTypeExtension -TypeName system.int32 -MemberType ScriptMethod -MemberName GetPercent -value {Param([int32]$Total,[int32]$Round=2) [math]::Round(($this/$total)*100,$round)}
Want to know what has been defined for a given type? Use Get-PSTypeExtension. The default is to prompt you for a member name, or use the –All parameter.
I'm thinking I need to modify the behavior so that showing all members is the default behavior. Now for the fun part. In order for these extensions to persist you would need to re-define them in your PowerShell profile. But perhaps you don't remember how you defined them. Or you want an easier way. You can use Export-PSTypeExtension and export the desired members to either a json or xml file.
Get-PSTypeExtension system.int32 -all | Export-PSTypeExtension -TypeName system.int32 -Path c:\work\int32-types.json
Do this for as many different types as you'd like.
You should check types first and only export members you have defined.
There's no reason to export anything else that is defined automatically. The export command will use with ConvertTo-Json or Export-Clixml depending on the file extension. In your profile all you need to do is import the type extensions.
Import-PSTypeExtension -Path C:\work\int32-types.json
Easy and it keeps my profile script simple. I've created a Samples folder in the GitHub repo if you want to download and try. The samples are not part of the beta zip file but that will probably change with the next release.
I hope a few of you will give this a try and let me know what you think. Use the Issues section to report any problems, comments or suggestions.
I really like this. The samples are great, too — I had a need to randomize a string just this week.
A couple of small things:
1. The link in the “I’ve created a Samples folder in the GitHub repo…” gets a 404.
2. In the samples (I got there from the main GitHub page), the cimlogicaldisk-extensions.json file contains what appears to be three identical copies of the three extensions. I’ve gone so far as to save two of the copies to disk and diff’d them to make sure I wasn’t missing something (I assumed I was). Is that a glitch in the export, or cut-and-paste error, or something else?
Hah, I just noticed the samples link appears to have been fixed while I was typing this, so thanks for that!
Looks like I botched that Samples link. I’ll double check the json files as well.