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
- Adding a NoteProperty to an object
- Adding an AliasProperty to an object
- Adding a NoteProperty to an object, pass thru the result to a variable
Extended Example
Description: Add a ScriptProperty for converting From/To a Base64 string of a string
- 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))}
>>
[^_^]PS[219]>$str.ToBase64String
YQBiAGMA
- Add "FromBase64String"
[^_^]PS[220]>$base64 = $str.ToBase64String
[^_^]PS[221]>$base64
YQBiAGMA
[^_^]PS[222]>$base64 | Add-Member -MemberType ScriptProperty -Name FromBase64String `
>> -Value {[System.Text.Encoding]::UNICODE.GetString([System.Convert]::FromBase64String($this))}
>>
[^_^]PS[223]>$base64.FromBase64String
abc
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;
$str.ToBase64String
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$str.FromBase64String
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))}
>>
[^_^]PS[261]>$str.ToBase64String
YQBiAGMA
[^_^]PS[262]>$str.ToBase64String.FromBase64String
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/ExtendedTypeSystemTags :
Monad MSH PowerShell cmdlet:Add-Member