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