Categories

Passing function names

A question asked about passing a function name into another function which then called the function. It sounds worse than it is. if you need to pass the name of a command and then call it try using invoke-expression

function ffour {            
Get-Random            
}            
            
            
function fthree {            
Get-Date            
}            
            
            
function ftwo {            
param(            
 [string]$fname            
)            
            
Invoke-Expression $fname            
}            
            
            
"date"            
ftwo fthree            
            
"random"            
ftwo ffour

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>