I've been testing $MyInvocation
, $MyCommand
and inspecting some other variables but would like a good example of how to do the following:
- Get the name of the current script file
- Get the name of the caller script file that included the current script file
- Get the name of the original script file that ultimately resulted in #1 being executed
I have used this for original but maybe it will also be caller?
$myInvocation.MyCommand.Name #original
Even if there are more files involved, I only need the current, caller and original. Consider three scripts; if you were to execute first.ps1
:
first.ps1
. "$PSScriptRoot\second.ps1"
second.ps1
. "$PSScriptRoot\third.ps1"
#get $Current = second.ps1
#get $Caller = first.ps1
#get $Original = first.ps1
third.ps1
#get $Current = third.ps1
#get $Caller = second.ps1
#get $Original = first.ps1
I have worked out how to get the filename from the path etc...
Also, why is syntax highlighting wonky on SO for PowerShell?