2012-07-18 59 views
8

如何檢索當前在powershell中運行的功能的名稱?下面是我想要的一個例子:顯示當前正在執行的功能的名稱

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

然後當我執行它,它會顯示這樣的:

>write-FunctionName 

The name of this function is: write-FunctioName 

> 

可以這樣做?如果是這樣如何?

+1

[有沒有辦法從函數內檢索PowerShell函數名?](http://stackoverflow.com/questions/3689543/is-there-a-way-to-retrieve-a- powershell-function-name-from-within-a-function) – 2017-01-13 00:40:17

回答

9

$MyInvocation變量包含任何當前執行的信息:

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

欲瞭解更多信息,請參閱get-help about_automatic_variables,或在TechNet網站here

+0

謝謝!那正是我期待的! – Winfred 2012-07-18 15:14:47

相關問題