When trying to debug my powershell script in the powerGUI script editor (2.0.0.1082) the $MyInvocation.MyCommand.Path is $null.
It works when running the script via powershell.
Running it in Powershell_ise.exe (on one of our servers) also works fine.
Have anyone else had the same problem or know what's wrong?
Here's my powershell version:
Name Value ---- ----- CLRVersion 2.0.50727.4927 BuildVersion 6.1.7600.16385 PSVersion 2.0 WSManStackVersion 2.0 PSCompatibleVersions {1.0, 2.0} SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1
Server version:
Name Value ---- ----- CLRVersion 2.0.50727.3082 BuildVersion 6.0.6002.18111 PSVersion 2.0 WSManStackVersion 2.0 PSCompatibleVersions {1.0, 2.0} SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1
Best Answer
The
$MyInvocation.MyCommand
object changes depending on the context of its execution. In this case,$MyInvocation.MyCommand.Path
will only return something if called from the$script:
scope, since that is the only valid scope to determine the directory from which the code was executed.Therefore, the solution here would either be to use
$script:MyInvocation.MyCommand.Path
or$MyInvocation.ScriptName
.EDIT
Running this in the PowerShell ISE works as expected, as you get the same result if you run it in the PowerShell console:
Output is:
I haven't used PowerGUI, but if you don't get the same output, then its probably a bug.