Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display the name of the currently executing function

Tags:

How can I retrieve the name of the function that is currently running in powershell? Here is an example of what I want:

Function write-FunctionName
{
write-host "The name of this function is: *SomethingGoesHereButWhat?*"
}

Then when I execute it, it will display this:

>write-FunctionName

The name of this function is: write-FunctioName

>

Can this be done? If so how?

like image 417
Winfred Avatar asked Jul 18 '12 14:07

Winfred


People also ask

How do you get a function name inside a function in Python?

Method 1: Get Function Name in Python using function. func_name.

How do you print a function name in Python?

Use the __name__ Property to Get the Function Name in Python __name__ . It will then return the function name as a string. The below example declares two functions, calls them, and prints out their function names. Note that this solution also works with the imported and pre-defined functions.


1 Answers

The $MyInvocation variable contains information about whatever is currently executing:

Function write-FunctionName {     write-host ("The name of this function is: {0} " -f $MyInvocation.MyCommand) } 

For more information, see get-help about_automatic_variables, or the technet site here.

like image 69
goric Avatar answered Sep 20 '22 09:09

goric