One of the coolest things about scripting in PowerShell is that they’ve enabled the addition of extension methods to any class (object). Essentially, this means that you can add whatever methods This enables all sorts of utility functions that would normally end up in a library to instead be added to the base objects. Just for example, this week I was working on a script that needed to do HTTP basic authentication, which requires Base64 encoding a string. Now, that’s not very hard to do, really:


$name = "Joel Bennett"
$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes( $name ))

 

But really, that’s kind of a pain to remember, nevermind type each time. I mean, tab-completion helps, but what with all the square brackets and double-colons … So, instead, wouldn’t it be cool if I could just take my original string and call $name.AsBase64? Well, I can, with a pretty simple custom type.


<Types>
  <Type>
    <Name>System.String</Name>
    <Members>
      <ScriptProperty>
        <Name>AsBase64</Name>
        <GetScriptBlock>
          [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($this))
        </GetScriptBlock>
      </ScriptProperty>
      <ScriptProperty>
        <Name>FromBase64</Name>
        <GetScriptBlock>
          [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($this))
        </GetScriptBlock>
      </ScriptProperty>
    </Members>
  </Type>
</Types>

 

We take this XML, put it in a file with the .ps1xml extension, and then call Update-TypeData MyTypes.ps1xml … Now we can call .AsBase64 or .FromBase64 on any string.

So then I ran into this simple problem. I wanted (in my script) to have the user type a password in… but not have it show up in their console. It turns out there’s a very over-engineered, and extremely elegant solution for this in PowerShell: Read-Host -AsSecureString "Prompt". It’s exactly what I wanted, in that it results in the console showing ***** as you type your password in. The only problem is, what you get out is a System.Security.SecureString which is essentially … well, useless. ;)

(more…)