@@ -12,8 +12,40 @@ function Invoke-Interpreter {
12
12
This will happen automatically as you attempt to use commands that have an associated interpreter
13
13
#>
14
14
param ()
15
+
16
+ # Store myInvocation (we'll need it often)
15
17
$myInv = $MyInvocation
18
+
19
+ # Determine the invocation name
16
20
$invocationName = $MyInvocation.InvocationName
21
+
22
+ # If we do not have the correct invocation name, that's because the call operator is being used
23
+ # Unfortunately, this makes this more complicated, as we have to go looking
24
+ if ($invocationName -in ' .' , ' &' ) {
25
+ # Starting off by picking the words after the invocation
26
+ $myLine = $myInv.Line.Substring ($myInv.OffsetInLine ) -replace ' ^\s{0,}'
27
+ $MyWords = @ ($myLine -split ' \s{1,}' )
28
+
29
+ # If the first word is a variable
30
+ if ($MyWords [0 ] -match ' ^\$(?<v>\w+)' ) {
31
+ $firstWordVariableValue = $ExecutionContext.SessionState.PSVariable.Get ($matches.0 -replace ' ^\s\$' ).Value
32
+ # and it has a value
33
+ if ($firstWordVariableValue ) {
34
+ # use that as the InvocationName.
35
+ $invocationName = $firstWordVariableValue
36
+ }
37
+ }
38
+ # If the first word is not a variable,
39
+ elseif ($MyWords [0 ] -match ' ^(?<w>\w+)' ) {
40
+ # see if it's an alias
41
+ $firstWordAlias = $ExecutionContext.SessionState.InvokeCommand.GetCommand ($mywords [0 ], ' Alias' )
42
+ if ($firstWordAlias.ReferencedCommand -ne $myInv.MyCommand.Name ) {
43
+ # and use the referenced command as the invocation name.
44
+ $invocationName = $firstWordAlias
45
+ }
46
+ }
47
+ }
48
+
17
49
# Return if we were called by our real name.
18
50
if ($invocationName -eq $myInv.MyCommand.Name ) { return }
19
51
# If there are no interpreters, obviously return.
@@ -22,6 +54,13 @@ function Invoke-Interpreter {
22
54
if (-not $PSInterpreters.ForFile ) { return }
23
55
$interpreterForFiles = $PSInterpreters.ForFile ($invocationName )
24
56
# or don't find a mapping, return.
57
+ if (-not $interpreterForFiles -and $invocationName -notmatch ' [\&\.]' ) {
58
+ $nameIsAlias = Get-Alias - Name $InvocationName
59
+ if ($nameIsAlias.ReferencedCommand.Name -ne $myInv.MyCommand.Name ) {
60
+ $invocationName = $nameIsAlias.ReferencedCommand.Name
61
+ $interpreterForFiles = $PSInterpreters.ForFile ($nameIsAlias.ReferencedCommand )
62
+ }
63
+ }
25
64
if (-not $interpreterForFiles ) { return }
26
65
27
66
# There can be more than one potential interpreter for each file
0 commit comments