April.Fools

There are two things, that make some operations with classes/ types smooth in PowerShell. First of all we have type conversion, that will do (almost) everything that is possible to “translate” data into the type that we select. Second element that is handy at times are type accelerators, that work as aliases for types.

During PowerShell Deep Dive I found out (thank you, Kirk!) something that I was not aware of before: type accelerators are similar to aliases also when it comes to priority order: if there is type accelerator available that matches to string provided – you will get it instead of “real” type. That basically means that you can fully replace “real” type with your own implementation, all you need to do is to add type accelerator with the name of needed type and redirect it to your own type.

There are some really nice use cases of that, but today I would like to abuse it. Smile

Add-Type -TypeDefinition @'
using System;

namespace April {
    public enum Fools {
        __You__ = 123454321,
        __Have__ = 123454322,
        __Been__ = 123454323,
        __Fooled__ = 123454324,
        __By__ = 123454325,
        __Bartek__  = 123454326 // Replace it with your name. 🙂
    }
}
'@            
            
$xlr8r = [psobject].assembly.gettype("System.Management.Automation.TypeAccelerators")            
            
$Keys = $xlr8r::Get.GetEnumerator() | foreach { $_.Key } |             
    where { $_ -ne 'string'}             
$System = [AppDomain]::CurrentDomain.GetAssemblies() |             
    foreach { $_.GetTypes() } | where { $_.IsPublic } |             
    foreach { if ($_.FullName -match '^System\.([\w]+)$' ) { $Matches[1] }} |             
    where { $_ -notmatch 'String' } | sort -Unique            
            
            
switch ($PSVersionTable.PSVersion.Major) {            
    3 {            
        # AddReplace            
                    
        $Keys | foreach {            
            $xlr8r::AddReplace($_, [April.Fools])            
        }            
            
        foreach ($Basic in $System) {            
            $xlr8r::AddReplace($Basic, [April.Fools])            
        }            
    }            
            
    2 {            
        foreach ($Key in $Keys) {            
            $xlr8r::Remove($Key) | Out-Null            
            $xlr8r::Add($Key, [April.Fools])            
        }            
            
        $xlr8r::Get | Out-Null            
        foreach ($Basic in $System) {            
            $xlr8r::Remove($Basic) | Out-Null            
            $xlr8r::Add($Basic, [April.Fools])            
        }            
            
    }            
}

Put that in your profile, or dot-sources script. Effect? Well, see for yourself:

April.Fools

All you need to do next is to ask your friend to show you something that requires some “basic” types, or type accelerators (such as ADSI). And maybe leave him with that for a minute or two. Winking smile

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s