Answer the question
In order to leave comments, you need to log in
How to make a function closure in PowerShell similar to JavaScript?
Started learning PowerShell. I'm trying to return a function from a block that closes to another function like this:
$myFunction = Invoke-Command -ScriptBlock {
function Bar {
Write-Host World
}
function Foo {
Write-Host Hello
Bar
}
return ${function:Foo}
}
. $myFunction
Hello
foreach : The term 'Bar' is not recognized as the name of a cmdlet, function, script file, or operable program.
Hello
World
Answer the question
In order to leave comments, you need to log in
I got it like this:
$myFunction = Invoke-Command -ScriptBlock {
function Bar
{
Write-Host("World")
}
function Foo
{
Write-Host("Hello");
Bar
}
return Foo
}
$myFunction
PS C:\Users\%username%> C:\Users\%username%\Desktop\Untitled2.ps1
Hello
World
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question