Powershell: Function Pointer

Hi,

Powershell provides also the ability to use function pointers such as C or C++.

Define your functions

function A([string]$Parameter1,[int]$Parameter2)
{

   write-host "Function: A" "Parameter 1:" $Parameter1 "Parameter 2:" $Parameter2
}
function B([string]$Parameter1,[int]$Parameter2)
{
   write-host "Function: B" "Parameter 1:" $Parameter1 "Parameter 2:" $Parameter2
}

Assign the functions to a variable and call the function

$pFunction=(Get-Item "function:A").ScriptBlock
$pFunction.Invoke("Call",2)
$pFunction=(Get-Item "function:B").ScriptBlock
$pFunction.Invoke("Call",2)

That’s it 🙂

Michael

Leave a Reply