PowerShell for Windows Admins

Jan 16 2013   4:33PM GMT

Passing function names

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: