::::::::: PowerShell :::::::::
Sunday, August 13, 2006
  Add-Member: Extended Example
Add-Member cmdlet provides a way to "Add(s) a user-defined custom member to an object"

There are 3 examples on get-help add-member help file but they do not seem to quite reflect the real power of this cmdlet.

Examples in the offical help document, examples deals with following cases

  1. Adding a NoteProperty to an object

  2. Adding an AliasProperty to an object

  3. Adding a NoteProperty to an object, pass thru the result to a variable

Extended Example
: Add a ScriptProperty for converting From/To a Base64 string of a string

  1. Add "ToBase64String"
    [^_^]PS[217]>$str = "abc" -as [psobject]
    [^_^]PS[218]>$str | Add-Member -MemberType ScriptProperty -Name ToBase64String `
    >> -Value {[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))}

  2. Add "FromBase64String"
    [^_^]PS[220]>$base64 = $str.ToBase64String
    [^_^]PS[222]>$base64 | Add-Member -MemberType ScriptProperty -Name FromBase64String `
    >> -Value {[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))}
Above example shows how to add a new member(ScriptProperty) to easily access a converted Base64 string from string variables
NOTE:-as [psobject] in the first line is necessary since Add-Member works only on the object of type PsObject, so -as [psobject] is added to explicitly convert a string variable to of type PsObject

On 218th statement, We add a script block(We are adding a ScriptProperty, aren't we?) to convert the current string(specified as $this, the current object we are working on) into a Base64 representation
Now retrieving a Base64 bytes of the string is as easy as accesing newly aded Script Property;


On 219~222 statements, we assign the Base64 string to a new variable, $base64 and then add a new ScriptProperty member(FromBase64String) to reverse the process(Converting Base64 string back to orignal string)
We have added the script property the same way as we did for ToBase64String but with different function in the script block.
Now again, you can retrieve the orignal string through From64String


The example is a modified version of PowerShell Team blog entry on http://blogs.msdn.com/powershell/archive/2006/04/25/583265.aspx.
One might think about adding both ToBase64String and FromBase64String on the same variable like the following(yes, i have tried it like hmm not sure how many times...)
[^_^]PS[258]>$str = "abc" -as [PsObject]
[^_^]PS[259]>$str | Add-Member ScriptProperty ToBase64String `
>> {[System.Convert]::ToBase64String([System.Text.Encoding]::UNICODE.GetBytes($this))}
[^_^]PS[260]>$str | Add-Member ScriptProperty FromBase64String `
>> {[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))}

But $str.ToBase64String.FromBase64String does not produce any result because $str.ToBase64String is not $str but a string returned as a result of Script Property method.(Would anyone correct me if I got it wrong?)

In the Team Blog Entry, in which talks about extending Type definition to add above script properties, above scenario works since type definition extends System.String type itself

so even $str.ToBase64String.FromBase64String as well as ScriptProperty being permanent(not just available through the current session but "across" sessions)

You can find more examples and ideas to extend on Mow's blog entry, Some fun With Monads Add-Member, MP3 files and COM and http://del.icio.us/powershell/ExtendedTypeSystem

Tags :
Comments: Post a Comment

Links to this post:

Create a Link

<< Home
Let's get lazy with PowerShell!

Location: Flushing, NY, United States

Experimenting with a different format of blogs...

10/01/2005 - 11/01/2005 / 11/01/2005 - 12/01/2005 / 12/01/2005 - 01/01/2006 / 01/01/2006 - 02/01/2006 / 02/01/2006 - 03/01/2006 / 03/01/2006 - 04/01/2006 / 04/01/2006 - 05/01/2006 / 05/01/2006 - 06/01/2006 / 06/01/2006 - 07/01/2006 / 07/01/2006 - 08/01/2006 / 08/01/2006 - 09/01/2006 / 10/01/2006 - 11/01/2006 / 11/01/2006 - 12/01/2006 /

Powered by Blogger