4

powerGUIスクリプトエディター(2.0.0.1082)でPowerShellスクリプトをデバッグしようとすると、$MyInvocation.MyCommand.Pathは$nullになります。PowerShellを介してスクリプトを実行するときに機能します。Powershell_ise.exe(サーバーの1つ)で実行することもできます。

他の誰かが同じ問題を抱えているか、何が悪いのか知っていますか?

これが私のPowerShellバージョンです:

名前の値
---- -----
CLRバージョン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

サーバーバージョン:

名前の値
---- -----
CLRバージョン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
4

4 に答える 4

4

オブジェクトは$MyInvocation.MyCommand、その実行のコンテキストに応じて変化します。この場合、コードが実行されたディレクトリを決定する唯一の有効なスコープであるため$MyInvocation.MyCommand.Path、スコープから呼び出された場合にのみ何かを返します。$script:

したがって、ここでの解決策は、$script:MyInvocation.MyCommand.Pathまたはを使用すること$MyInvocation.ScriptNameです。


編集

これを PowerShell コンソールで実行すると同じ結果が得られるため、PowerShell ISE でこれを実行すると期待どおりに動作します。

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)

出力は次のとおりです。

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

PowerGUI を使用したことはありませんが、同じ出力が得られない場合は、おそらくバグです。

于 2010-06-07T15:24:33.763 に答える
1

この問題は修正されました。PowerGUIの最新バージョンをhttp://powergui.orgからダウンロードすると、問題は解消されます。

于 2011-01-20T18:52:36.143 に答える
1

別のスクリプト ファイルからスクリプトを呼び出します: http://powergui.org/message.jspa?messageID=28988#28988

于 2010-06-15T09:59:39.187 に答える
0

Get-PSCallStack を使用してみてください。コールスタックの各レイヤーの InvocationInfo プロパティは $myInvocation と同等です

お役に立てれば。

于 2010-06-07T17:23:46.230 に答える