現在実行中のPowerShellファイルを取得するにはどうすればいいですか? 質問する

現在実行中のPowerShellファイルを取得するにはどうすればいいですか? 質問する

注: PowerShell 1.0
現在実行中の PowerShell ファイル名を取得したいと思います。つまり、次のようにセッションを開始するとします。

powershell.exe .\myfile.ps1

文字列を取得したい"\myfile.ps1"(またはそのようなもの)。編集:「myfile.ps1」が望ましいです。
何かアイデアはありますか?

ベストアンサー1

ここでは、PowerShell 5 用に更新されたさまざまな回答をまとめてみました。

  • PowerShell 3以降のみを使用している場合は、$PSCommandPath

  • 古いバージョンとの互換性が必要な場合は、シムを挿入します。

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

    $PSCommandPathまだ存在しない場合は追加します。

    shim コードはどこでも (トップレベルまたは関数内) 実行できますが、$PSCommandPath変数は通常のスコープ ルールに従います (たとえば、shim を関数内に配置すると、変数のスコープはその関数のみになります)。

詳細

さまざまな回答で 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: $(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より前のバージョンでは、関数の内外両方で機能する単一のメソッドは存在しません。

おすすめ記事