如何在powershell函数中获取当前正在执行的文件

时间:2010-09-09 19:01:26

标签: powershell

以下S.O.问题解决了问题(当你试图从脚本中解决问题时)。 How can I get the current PowerShell executing file?

如果你在一个职能部门内,你会怎么做呢。

以下示例在函数定义之外工作,不在内部。

echo ''
echo '******** outside function scope'
echo "Path: $($MyInvocation.MyCommand.Path)"
echo "Definition: $($MyInvocation.MyCommand.Definition)"
echo '*******************************'
echo ''

function myHelper()
{
    echo '******** inside function scope'
    #EMPTY
    echo "Path: $($MyInvocation.MyCommand.Path)"
    #Prints the string definition of the function itself
    echo "Definition: $($MyInvocation.MyCommand.Definition)"
    echo '******************************'
}

myHelper

1 个答案:

答案 0 :(得分:4)

您可以通过以下方式获取此信息:

$MyInvocation.ScriptName

这将返回调用该函数的任何脚本文件。

相关问题