4

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
1
  • FYI: I read somewhere that it would be null when calling it from a function. I haven't done that. Commented Jun 7, 2010 at 11:38

4 Answers 4

4

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:

function Main
{
    Write-Host ("MyCommand.Path from function: " + $MyInvocation.MyCommand.Path)
    Write-Host ("ScriptName from function: " + $MyInvocation.ScriptName)
}

Main

Write-Host ("MyCommand.Path from script scope: " + $MyInvocation.MyCommand.Path)
Write-Host ("ScriptName from script scope: " + $MyInvocation.ScriptName)

Output is:

MyCommand.Path from function: 
ScriptName from function: C:\temp\Test.ps1
MyCommand.Path from script scope: C:\temp\Test.ps1
ScriptName from script scope:

I haven't used PowerGUI, but if you don't get the same output, then its probably a bug.

Sign up to request clarification or add additional context in comments.

1 Comment

None of them gives me anything but $null. And as $MyInvocation.MyCommand.Path is called in the first line in the script outside any function, my limited powershell knowledge tells me it's already in the $script scope. I think it's actually a bug in ScriptEditor, as it works fine when run from the prompt.
1

This issue has been fixed. Download the latest version of PowerGUI from http://powergui.org and the issue will disappear.

2 Comments

Thanks for following this up. :)
I'm running the latest version (3.5.0.2293) and still seem to have this issue.
1

call your script from another script file: http://powergui.org/message.jspa?messageID=28988#28988

Comments

0

You might want to try using Get-PSCallStack. The InvocationInfo property of each layer in the callstack is the equivilent to $myInvocation

Hope this helps.

1 Comment

Thank you, that sounds like a useful tool. When running the script from the ScriptEditor debugger, the Command from Get-PSCallStack is '.', but when running the script from the prompt it is the name of my script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.