【问题标题】:How to get the currently executing file within powershell function如何在powershell函数中获取当前正在执行的文件
【发布时间】:2010-09-29 20:49:19
【问题描述】:

以下 S.O. question 解决了问题(当您尝试从脚本中解决问题时)。 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

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    $MyInvocation.ScriptName
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 2010-11-18
      • 2017-04-05
      • 1970-01-01
      • 2011-02-07
      相关资源
      最近更新 更多