本文介绍了如何获取当前的 PowerShell 执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Note: PowerShell 1.0
I'd like to get the current executing PowerShell file name. That is, if I start my session like this:

powershell.exe .\myfile.ps1

我想得到字符串 ".\myfile.ps1" (或类似的东西).编辑:myfile.ps1" 更可取.
有什么想法吗?

I'd like to get the string ".\myfile.ps1" (or something like that). EDIT: "myfile.ps1" is preferable.
Any ideas?

推荐答案

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

I've tried to summarize the various answers here, updated for PowerShell 5:

  • 如果您只使用 PowerShell 3 或更高版本,请使用 $PSCommandPath

如果想要与旧版本兼容,请插入垫片:

If want compatibility with older versions, insert the shim:

if ($PSCommandPath -eq $null) { function GetPSCommandPath() { return $MyInvocation.PSCommandPath; }$PSCommandPath = GetPSCommandPath;}

如果 $PSCommandPath 尚不存在,则会添加它.

This adds $PSCommandPath if it doesn't already exist.

shim 代码可以在任何地方(顶层或函数内部)执行,尽管 $PSCommandPath 变量受正常范围规则的约束(例如,如果将 shim 放在函数中,变量仅限于该函数).

The shim code can be executed anywhere (top-level or inside a function), though $PSCommandPath variable is subject to normal scoping rules (eg, if you put the shim in a function, the variable is scoped to that function only).

在各种答案中使用了 4 种不同的方法,所以我编写了这个脚本来演示每种方法(加上 $PSCommandPath):

There's 4 different methods used in various answers, so I wrote this script to demonstrate each (plus $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: $(PSCommandPath)";
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 之前的版本,没有一个方法在函数内部和外部都有效
  • Notes:

    • Executed from C:\, but actual script is C:\Test\test.ps1.
    • No method tells you the passed invocation path (.\Test\test.ps1)
    • $PSCommandPath is the only reliable way, but was introduced in PowerShell 3
    • For versions prior to 3, no single method works both inside and outside of a function
    • 这篇关于如何获取当前的 PowerShell 执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 15:43