2012-01-13 63 views
2

我在目錄c:\scripts\中有以下腳本。在PowerShell控制檯和ISE中源代碼腳本目錄中的文件?

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

但是,它會在ISE中引發錯誤。這是一種可以在控制檯和ISE中使用的方式嗎?我試圖不使用完整的絕對路徑。

回答

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 
+0

好吧,也許我不得不使用ISE全路徑。如何檢測腳本在ISE/Console中運行? – ca9163d9 2012-01-14 00:01:44

+0

@NickW檢查我的更新答案。 – 2012-01-14 00:26:09

相關問題