代码之家  ›  专栏  ›  技术社区  ›  Jason Jarrett

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

  •  3
  • Jason Jarrett  · 技术社区  · 15 年前

    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 回复  |  直到 9 年前
        1
  •  4
  •   Keith Hill    15 年前

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

    $MyInvocation.ScriptName
    

    这将返回从哪个脚本文件调用函数。

    推荐文章