注意:PowerShell 1.0
我想获取当前正在执行的PowerShell文件名。也就是说,如果我像这样开始 session :

powershell.exe .\myfile.ps1

我想获取字符串“。\ myfile.ps1” (或类似名称)。 编辑:“myfile.ps1”首选
有任何想法吗?

最佳答案

我尝试在此处总结各种答案,这些答案针对PowerShell 5进行了更新:

  • 如果您仅使用PowerShell 3或更高版本,请使用$PSCommandPath
  • 如果要与旧版本兼容,请插入垫片:if ($PSCommandPath -eq $null) { function GetPSCommandPath() { return $MyInvocation.PSCommandPath; } $PSCommandPath = GetPSCommandPath; }如果$PSCommandPath不存在,则会添加它。
    尽管$PSCommandPath变量受常规作用域规则的约束,但可以在任何位置(顶级或函数内部)执行填充程序代码(例如,将填充程序放入函数中,该变量仅作用于该函数)。

  • 细节
    在各种答案中使用了4种不同的方法,因此我编写了此脚本来演示每种方法(以及$PSCommandPath):
    function PSCommandPath() { return $PSCommandPath; }
    function ScriptName() { return $MyInvocation.ScriptName; }
    function MyCommandName() { return $MyInvocation.MyCommand.Name; }
    function MyCommandDefinition() {
        # Begin of MyCommandDefinition()
        # Note: ouput of this script shows the contents of this function, not the execution result
        return $MyInvocation.MyCommand.Definition;
        # End of MyCommandDefinition()
    }
    function MyInvocationPSCommandPath() { return $MyInvocation.PSCommandPath; }
    
    Write-Host "";
    Write-Host "PSVersion: $($PSVersionTable.PSVersion)";
    Write-Host "";
    Write-Host "`$PSCommandPath:";
    Write-Host " *   Direct: $PSCommandPath";
    Write-Host " * Function: $(ScriptName)";
    Write-Host "";
    Write-Host "`$MyInvocation.ScriptName:";
    Write-Host " *   Direct: $($MyInvocation.ScriptName)";
    Write-Host " * Function: $(ScriptName)";
    Write-Host "";
    Write-Host "`$MyInvocation.MyCommand.Name:";
    Write-Host " *   Direct: $($MyInvocation.MyCommand.Name)";
    Write-Host " * Function: $(MyCommandName)";
    Write-Host "";
    Write-Host "`$MyInvocation.MyCommand.Definition:";
    Write-Host " *   Direct: $($MyInvocation.MyCommand.Definition)";
    Write-Host " * Function: $(MyCommandDefinition)";
    Write-Host "";
    Write-Host "`$MyInvocation.PSCommandPath:";
    Write-Host " *   Direct: $($MyInvocation.PSCommandPath)";
    Write-Host " * Function: $(MyInvocationPSCommandPath)";
    Write-Host "";
    
    输出:
    PS C:\> .\Test\test.ps1
    
    PSVersion: 5.1.19035.1
    
    $PSCommandPath:
     *   Direct: C:\Test\test.ps1
     * Function: C:\Test\test.ps1
    
    $MyInvocation.ScriptName:
     *   Direct:
     * Function: C:\Test\test.ps1
    
    $MyInvocation.MyCommand.Name:
     *   Direct: test.ps1
     * Function: MyCommandName
    
    $MyInvocation.MyCommand.Definition:
     *   Direct: C:\Test\test.ps1
     * Function:
        # Begin of MyCommandDefinition()
        # Note this is the contents of the MyCommandDefinition() function, not the execution results
        return $MyInvocation.MyCommand.Definition;
        # End of MyCommandDefinition()
    
    
    $MyInvocation.PSCommandPath:
     *   Direct:
     * Function: C:\Test\test.ps1
    
    笔记:
  • C:\执行,但是实际脚本是C:\Test\test.ps1
  • 没有方法告诉您传递的调用路径(.\Test\test.ps1)
  • $PSCommandPath是唯一可靠的方法,但已在PowerShell 3中引入
  • 对于3之前的版本,在
  • 函数内部和外部都无法使用单个方法

    关于powershell - 如何获取当前的PowerShell执行文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/817198/

    10-12 02:54