2

directory に次のスクリプトがありますc:\scripts\

# . c:\scripts\anotherScript.ps1
function GetScriptRoot { Split-Path $script:MyInvocation.MyCommand.Path }
. "$(GetScriptroot)\anotherScript.ps1"

ただし、ISE でエラーが発生します。コンソールと ISE の両方で機能する方法ですか? 完全な絶対パスを使用しないようにしています。

4

1 に答える 1

2

$MyInvocation.MyCommand.Path プロパティは、実行中のスクリプトでのみ使用できます。

ISE で実行されているかどうかを検出するには、$psise変数を確認できます。

if ($psise) {
    "Running in the ISE"
} else {
    "Not running in the ISE"
}

または、$host.Nameプロパティを見てください。

PS C:\Users\andy> $host
Name             : Windows PowerShell ISE Host

PS C:\Users\andy> $host
Name             : ConsoleHost
于 2012-01-13T22:55:14.480 に答える