2014-04-28 75 views
0

我需要調用在單獨文件夾中的文件中定義的PowerShell函數。該功能需要使用位於同一文件夾中的dll。有沒有簡單的方法來做到這一點?獲取包含外部函數的Powershell文件的位置

詳情:

結構:

  • 構建\ build.ps1
  • 工具\ myTool \ fileWithFunction.ps1
  • 工具\ myTool \ someDll.dll

構建.ps1包含「... \ tools \ myTool \ fileWithFunction.ps1」

它調用該文件中的函數。該功能需要加載someDll.dll。我可能在函數調用中傳遞文件夾的位置(toos \ myTool),但想知道fileWithFunction中的函數是否可以自己計算出dll的位置(假設它始終位於相同的文件夾)。

+0

http://stackoverflow.com/questions/801967/how-can-i-find-the-source-path-of-an-executing-script – Raf

+0

我其實有\工具\ psake \ psake調用ing build \ build.ps1。使用該文章中的代碼給我\ tools \ psake而不是\ tools \ myTool。 – ashic

+0

* ... \ psake由build \ build.ps1調用 – ashic

回答

0

使用$PSScriptRoot,這應該得到你需要的。

0

如果你在PowerShell中V2(希望你至少V3),把這個fileWithFunction.ps1

$ScriptDir = {Split-Path $MyInvocation.ScriptName –Parent} 

然後訪問DIR像這樣:

"ScriptDir is $(&$ScriptDir)" 

使用scriptblock的原因是ensure safety when dot sourcing script files.