| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Gets the path of the entry script that invoked the current script. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function inspects the PowerShell call stack to determine the script path |
| | | 6 | | of the entry script that invoked the current script. It excludes the current |
| | | 7 | | script's path from consideration. |
| | | 8 | | .OUTPUTS |
| | | 9 | | [string] - The path of the entry script, or $null if not found. |
| | | 10 | | .NOTES |
| | | 11 | | This function is part of the Kestrun PowerShell module. |
| | | 12 | | #> |
| | | 13 | | function Get-EntryScriptPath { |
| | | 14 | | [CmdletBinding()] |
| | | 15 | | [OutputType([string])] |
| | | 16 | | param() |
| | | 17 | | |
| | 2 | 18 | | $self = $PSCommandPath ? (Resolve-Path -LiteralPath $PSCommandPath).ProviderPath : $null |
| | | 19 | | |
| | | 20 | | # Take the last real script caller in the stack |
| | 2 | 21 | | $stack = @(Get-PSCallStack) |
| | 1 | 22 | | [System.Array]::Reverse($stack) |
| | 1 | 23 | | foreach ($f in $stack) { |
| | 1 | 24 | | $p = $f.InvocationInfo.ScriptName |
| | 1 | 25 | | if (-not $p) { continue } |
| | | 26 | | |
| | | 27 | | try { |
| | 2 | 28 | | $resolved = (Resolve-Path -LiteralPath $p -ErrorAction Stop).ProviderPath |
| | | 29 | | } catch { |
| | 0 | 30 | | Write-Debug "Failed to resolve path '$p': $_" |
| | | 31 | | continue |
| | | 32 | | } |
| | | 33 | | |
| | 1 | 34 | | if ($resolved -and $resolved -ne $self) { |
| | 1 | 35 | | return $resolved |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | return $null |
| | | 40 | | } |