I've written function 'A' that will call one of a number of other functions. To save re-writing function 'A', I'd like to pass the function to be called as a parameter of function 'A'. For example:
function A{
Param($functionToCall)
Write-Host "I'm calling : $functionToCall"
}
function B{
Write-Host "Function B"
}
Function C{
write-host "Function C"
}
A -functionToCall C
Returns: I'm calling: C
I am expecting it to return: I'm calling: Function C.
I've tried various things such as:
Param([scriptblock]$functionToCall)
Cannot convert System.String to ScriptBlock
A -functionToCall $function:C
Returns "Write-Host "Function C"
A - functionToCall (&C)
This evaluates before the rest of it:
Function C
I'm Calling :
I'm sure this is programming 101, but I can't work out the correct syntax or what it is I'm doing wrong.
Is this what you need?
function A{
Param($functionToCall)
Write-Host "I'm calling : $functionToCall"
#access the function-object like this.. Ex. get the value of the StartPosition property
(Get-Item "function:$functionToCall").ScriptBlock.StartPosition
}
function B{
Write-Host "Function B"
}
Function C{
write-host "Function C"
}
PS> a -functionToCall c
I'm calling : c
Content : Function C{
write-host "Function C"
}
Type : Position
Start : 307
Length : 43
StartLine : 14
StartColumn : 1
EndLine : 16
EndColumn : 2