Windows Vista Forums

Add-Member -InputObject $s -MemberType ScriptMetho
  1. #1


    B.Williams Guest

    Add-Member -InputObject $s -MemberType ScriptMetho

    Hello I've been working on the following for a while now. I can not get it to
    add the new script method. any ideas are welcome


    $ar = {
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    }

    $s = "One Two Three"
    Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse -Value $ar
    $s.reverse()


    Method invocation failed because [System.String] doesn't contain a method
    named 'reverse'.
    At D:.ps1:10 char:11
    + $s.reverse( <<<< )





      My System SpecsSystem Spec

  2. #2


    Kiron Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    A synthetic member is added to a [psObject], $s is [String]. By casting the initial assignment of $s to [psObject] the ScriptMethod is added to $s.
    Another way -'the best practice' way- is to create a [psObject] from $s while adding the synthetic member, need to use Add-Member's -PassThru parameter:

    # eliminate $s
    rv s -ea 0
    $ar = {
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    }

    # cast $s to [psObject]
    [psObject]$s = "One Two Three"
    Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse -Value $ar
    $s.reverse()


    # or...
    # 'best practice'
    # eliminate $s
    rv s -ea 0
    $ar = {
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    }

    $s = "One Two Three"
    # create a psObject from $s with a synthetic member
    $s = Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse -Value $ar -passThru
    $s.reverse()

    --
    Kiron

      My System SpecsSystem Spec

  3. #3


    B Williams Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    Got it. Thanks for the info.

    Best Regards,
    B
    "Kiron" <Kiron@xxxxxx> wrote in message
    news:5F946FBE-0272-44CB-A0AD-C012BE929CB4@xxxxxx
    A synthetic member is added to a [psObject], $s is [String]. By casting the
    initial assignment of $s to [psObject] the ScriptMethod is added to $s.
    Another way -'the best practice' way- is to create a [psObject] from $s
    while adding the synthetic member, need to use Add-Member's -PassThru
    parameter:

    # eliminate $s
    rv s -ea 0
    $ar = {
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    }

    # cast $s to [psObject]
    [psObject]$s = "One Two Three"
    Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse -Value $ar
    $s.reverse()


    # or...
    # 'best practice'
    # eliminate $s
    rv s -ea 0
    $ar = {
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    }

    $s = "One Two Three"
    # create a psObject from $s with a synthetic member
    $s = Add-Member -InputObject $s -MemberType ScriptMethod -Name
    reverse -Value $ar -passThru
    $s.reverse()

    --
    Kiron


      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    Glad to help.

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Jon Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    Another approach, if you're keen to add your method to the string type, is
    to add a chunk of xml code to the file

    C:\WINDOWS\system32\WindowsPowerShell\v1.0\types.ps1xml

    You could then use your Reverse method for any string within a Powershell
    session or script

    PS (1) > $a="apple"
    PS (2) > $a.Reverse()
    elppa
    PS (3) >



    #-------------------------------------------------
    <Type>
    <Name>System.String</Name>
    <Members>
    <ScriptMethod>
    <Name>Reverse</Name>
    <Script>
    $a = [char[]] $this
    [array]::reverse($a)
    [string]::join('',$a)
    </Script>
    </ScriptMethod>
    </Members>
    </Type>
    #-------------------------------------------------

    NB You'd place the code at the end of another <type> ....</type> block.

    --
    Jon




      My System SpecsSystem Spec

  6. #6


    Shay Levi Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho



    You can extend $s and intreduce new members even though it's not a psObject.
    After all, everything in PowerShell is object based

    PS 1> $s = "llehSrewoP"

    PS 2> $ar = {

    >> $o=$ofs; $ofs=""
    >> "$($this[$this.length..0])"
    >> $ofs=$o
    >> }
    >>
    PS 3> $s = $s | Add-Member -pass ScriptMethod reverse $ar

    PS 4> $s | gm r*


    TypeName: System.String

    Name MemberType Definition
    ---- ---------- ----------
    (...)
    reverse ScriptMethod System.Object reverse();


    PS 5> $s.reverse()
    PowerShell


    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com

    > A synthetic member is added to a [psObject], $s is [String]. By
    > casting the initial assignment of $s to [psObject] the ScriptMethod is
    > added to $s.
    >
    > Another way -'the best practice' way- is to create a [psObject] from
    > $s while adding the synthetic member, need to use Add-Member's
    > -PassThru parameter:
    >
    > # eliminate $s
    > rv s -ea 0
    > $ar = {
    > $a = [char[]] $this
    > [array]::reverse($a)
    > [string]::join('',$a)
    > }
    > # cast $s to [psObject]
    > [psObject]$s = "One Two Three"
    > Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse
    > -Value $ar
    > $s.reverse()
    > # or...
    > # 'best practice'
    > # eliminate $s
    > rv s -ea 0
    > $ar = {
    > $a = [char[]] $this
    > [array]::reverse($a)
    > [string]::join('',$a)
    > }
    > $s = "One Two Three"
    > # create a psObject from $s with a synthetic member
    > $s = Add-Member -InputObject $s -MemberType ScriptMethod -Name reverse
    > -Value $ar -passThru
    > $s.reverse()


      My System SpecsSystem Spec

  7. #7


    Kiron Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    > ...add a chunk of xml code to the file C:\WINDOWS\system32\WindowsPowerShell\v1.0\types.ps1xml

    It's better/safer to create a new ps1xml file and extend the type through the Update-TypeData Cmdlet.

    --
    Kiron

      My System SpecsSystem Spec

  8. #8


    Jon Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    "Kiron" <Kiron@xxxxxx> wrote in message
    news:C99C483B-83A0-4D3D-8243-69A2DB51ED89@xxxxxx

    >> ...add a chunk of xml code to the file
    >> C:\WINDOWS\system32\WindowsPowerShell\v1.0\types.ps1xml
    >
    > It's better/safer to create a new ps1xml file and extend the type through
    > the Update-TypeData Cmdlet.
    >

    Yep, you're right - that's a better way of doing it. Keeping it in a
    separate file also allows you to easily see what changes have been made,
    should you wish to reverse them (please excuse the pun).

    I tried doing something similar before, by adding a types.ps1xml in a
    directory included in path environment variable, thinking that it would be
    picked up automatically. It wasn't. So I suppose you need to explicitly add
    it using Update-TypeData

    Good tip. Thanks Kiron.

    --
    Jon




      My System SpecsSystem Spec

  9. #9


    Keith Hill [MVP] Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    Doh! I was just going to say that.

    --
    Keith
    "Kiron" <Kiron@xxxxxx> wrote in message news:CB84B2FD-1DD3-47B9-9905-1615D31AA27C@xxxxxx


    By the way, the ScriptBlock could be simplified, no need to 'reset' $ofs because its only changed in the ScriptMethod's scope:
    $ar = {
    $ofs=""
    "$($this[-1..-$this.length])"
    }

      My System SpecsSystem Spec

  10. #10


    Keith Hill [MVP] Guest

    Re: Add-Member -InputObject $s -MemberType ScriptMetho

    It wouldn't surprise me if Microsoft just plain overwrote those files with V2. At the moment, the v2 CTP installs into the same directory as V1.

    --
    Keith
    "Kiron" <Kiron@xxxxxx> wrote in message news:C99C483B-83A0-4D3D-8243-69A2DB51ED89@xxxxxx

    > ...add a chunk of xml code to the file C:\WINDOWS\system32\WindowsPowerShell\v1.0\types.ps1xml
    It's better/safer to create a new ps1xml file and extend the type through the Update-TypeData Cmdlet.

    --
    Kiron

      My System SpecsSystem Spec

Page 1 of 2 12 LastLast
Add-Member -InputObject $s -MemberType ScriptMetho problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Add-Member not Adding a member PeterCS PowerShell 3 26 Nov 2008
Help with "Sort-Object -InputObject xxx" David PowerShell 2 13 Aug 2008
-inputobject vs. receiving from the pipe... Hans Dingemans PowerShell 7 08 Oct 2007
ForEach-Object and -InputObject - Any use for it? Alex K. Angelopoulos [MVP] PowerShell 8 27 Oct 2006
Get-Member -InputObject returns generic data; bug? Alex K. Angelopoulos [MVP] PowerShell 3 20 Jun 2006