As a Windows PowerShell enthusiast, I am often thinking about PowerShell. Seems logical. Can I do this? What will happen if I do that? Why doesn’t this work!? Well, I had one of those thoughts recently. Can I determine if a function was called using an alias, or if it was called using the function name?
This thought occurred to me because of a change I considered adding to one of my functions. The idea was, that I wanted to prompt the user for confirmation to run a function (within the function), if an alias was used, and not prompt the user when the full function name was used. This lined up with the assumption that an alias could be accidentally entered more easily than the function name, and that we may want to take precautions when the alias was used.
My first consideration was to get the last command entered by the user, by using: (Get-History -Count 1).CommandLine. The problem with that “option,” is that the history isn’t updated until after the function has ended. This means that I wouldn’t know what was entered by the user while the function was running. Take a look at the example below. When I run the function Get-TMHistory the first time, nothing is displayed (in a fresh console), but something is displayed after the second run.
Function Get-TMHistory { Get-History -Count 1 } PS C:\> Get-TMHistory PS C:\> Get-TMHistory Id CommandLine -- ----------- 1 Get-TMHistory
I came up with a another idea. Let’s start by creating a new function called Get-TMHowCalled. In addition, we’ll need to create an alias for the function – let’s use ghc. I’ve included the New-Alias cmdlet’s -Force parameter in case there’s already an alias with that name. If there is, then New-Alias acts like Set-Alias, and updates the alias (instead of throwing an error). Note: This option could be problematic if it replaced ghc, and ghc was used for a different purpose.
New-Alias -Name ghc -Value Get-TMHowCalled -Force Function Get-TMHowCalled { }
The next thing to do is to construct the logic that will check and determine if the alias was used, or if the full function name was used. This is simple function: check, and echo what was entered by the user.
Function Get-TMHowCalled { If () { Write-Output -Verbose 'Alias was used.' } ElseIf () { Write-Output -Verbose 'Function name was used.' } }
The secret here is the $MyInvocation automatic variable and its Line property. This stores what was entered, and so it can be used to determine how the function was called – by alias or by name. The best part, is that the variable is populated when the function is called and can, therefore, be queried inside the function. This variable can only be used in “scripts, functions, and script blocks.” You can read more about it using this command: Get-Help about_Automatic_Variables.
Function Get-TMHowCalled { If ($MyInvocation.Line -eq 'ghc') { Write-Output -Verbose 'Alias was used.' } ElseIf ($MyInvocation.Line -eq 'Get-TMHowCalled') { Write-Output -Verbose 'Function name was used.' } }
Hope this proves to be helpful for someone. It might be for me.