Powershell: Function Pointer

Hi,

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

Define your functions

1
2
3
4
5
6
7
8
9
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

1
2
3
4
$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