有没有办法从函数中检索PowerShell函数名称? [英] Is there a way to retrieve a PowerShell function name from within a function?
本文介绍了有没有办法从函数中检索PowerShell函数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
function Foo {
[string] $ functionName = commandRetrievesFoo
Write- Host这个函数被称为$ functionName
输出:
PS> Foo
这个函数被称为foo
解决方案
您可以使用 $ MyInvocation
,其中包含一些有关当前执行的信息。
'这个函数被称为{0}。'-f $ MyInvocation.MyCommand
}
For example:
function Foo {
[string]$functionName = commandRetrievesFoo
Write-Host "This function is called $functionName"
}
Output:
PS > Foo
This function is called foo
解决方案
You can use $MyInvocation
which contains some useful information about what is currently executed.
function foo {
'This function is called {0}.' -f $MyInvocation.MyCommand
}
这篇关于有没有办法从函数中检索PowerShell函数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文