1

C:\tmp\run.ps1:

function buildOne()
{
    param(
        [Parameter(Mandatory=$true)][string]$a,
        [Parameter(Mandatory=$true)][string]$b
    )
    Write-Host -ForegroundColor yellow "$a $b"
}

C:\tmp\_build.ps1 {$Function:buildOne}

C:\tmp_build.ps1:

param(
    [Parameter(Mandatory=$true)]$buildOne
)

#&$buildOne "a" "b"
#Invoke-Command $buildOne -argumentlist "a", "b"
#Invoke-Command -ScriptBlock $buildOne -argumentlist "a", "b"

The idea is to invoke the buildOne function passed as parameter from run.ps1 to _build.ps1. Unfortunately, none of my attempts works. For some reason it just displays the function body rather than invokes it.

What am I doing wrong?

1 Answer 1

2

You can invoke commands (and functions) by name as long as they are in scope:

C:\tmp_build.ps1 buildOne

tmp_build.ps1

& $buildOne a b 

If you really want to pass the function definition then do it like this:

run.ps1

function buildOne()
{
    param(
        [Parameter(Mandatory=$true)][string]$a,
        [Parameter(Mandatory=$true)][string]$b
    )
    Write-Host -ForegroundColor yellow "$a $b"
}

.\tmp_build.ps1 $Function:buildOne

tmp_build.ps1

param(
    [Parameter(Mandatory=$true)]$buildOne
)

$buildOne.Invoke("a", "b")
Sign up to request clarification or add additional context in comments.

2 Comments

So the answer is impossible?
That is much better. Thanks.

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.