2

I try to add an existing function as a method to a new created object. Writing an inline function works:

$myObject | Add-Member ScriptMethod -name Calc -value{param([int]$a,[int]$b;$a+$b}

Having a function:

function get-Calc{param([int]$a,[int]$b) $a +$b}

this doesn't work:

$myObject | Add-Member ScriptMethod -name Calc -value(get-Calc)
3
  • What's the point of adding it as a method on a specific object if the function doesn't use the object in any way? Commented Feb 7, 2022 at 14:58
  • but I like to use this method, like that: $myObject.Calc(2,3) Commented Feb 7, 2022 at 15:01
  • 3
    I get that, I just don't understand why :) Normally you'd attach code that references the object itself, eg.: $myObject = [pscustomobject]@{ MyValue = 123 } |Add-Member -Name Times -Value {param([int]$X) return $this.MyValue * $X} -PassThru -MemberType ScriptMethod, which would then make $myObject.Times(2) evaluate to 246 Commented Feb 7, 2022 at 15:03

3 Answers 3

5

You could also do it passing a script block as -Value, see the Constructor for PSScriptMethod Class.

function Get-Calc { param([int] $a, [int] $b) $a + $b }

$myObject = [pscustomobject]@{
    A = 1
    B = 2
}

# If you're referencing the existing Properties of the Object:
$myObject | Add-Member ScriptMethod -Name MyMethod1 -Value {
    Get-Calc $this.A $this.B
}

# If you want the Instance Method to reference the arguments being passed:
$myObject | Add-Member ScriptMethod -Name MyMethod2 -Value {
    param([int] $a, [int] $b)

    Get-Calc $a $b
}

# This is another alternative as the one above:
$myObject.PSObject.Methods.Add(
    [psscriptmethod]::new(
        'MyMethod3', {
            param([int] $a, [int] $b)

            Get-Calc $a $b
        }
    )
)

$myObject.MyMethod1()
$myObject.MyMethod2(1, 2)
$myObject.MyMethod3(3, 4)

Note: All examples in this answer require that the function Get-Calc is defined in the parent scope and will fail as soon as the definition for the function has changed. Instead you should pass in the definition as a Script Block as Mathias's helpful answer is showing.

Sign up to request clarification or add additional context in comments.

1 Comment

@zett42 It was meant to show the alternative to Add-Member
3

Defined functions are stored in the special function:\ drive.

To reference a function definition using variable syntax:

$myObject | Add-Member ScriptMethod -name Calc -Value ${function:get-Calc}

Comments

1

There are 2 problems with your example that do not work.

  • You are not passing Get-Calc as a scriptblock (The expected value for a scriptmethod is a scriptblock)
  • You are not passing your expected parameters

Instead of

$myObject | Add-Member ScriptMethod -name Calc -value(get-Calc)

Do this:

$myObject | Add-Member ScriptMethod -name Calc3 -value {param([int]$a,[int]$b) get-Calc -a $a -b $b}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.