0

I need to call out to a powershell function defined in a file in a separate folder. That function needs to use a dll located in the same folder. Is there any easy way to do this?

Details:

Structure:

  • build\build.ps1
  • tools\myTool\fileWithFunction.ps1
  • tools\myTool\someDll.dll

build.ps1 has Include "...\tools\myTool\fileWithFunction.ps1"

It calls a function in that file. That function needs to load someDll.dll. I could pass the location of the folder (toos\myTool) in the function call, but was wondering if there's a way by which the function in fileWithFunction can figure out the location of the dll by itself (assuming that it's always in the same folder).

ashic
  • 6,367
  • 5
  • 33
  • 54
  • http://stackoverflow.com/questions/801967/how-can-i-find-the-source-path-of-an-executing-script – Raf Apr 28 '14 at 11:46
  • I actually have \tools\psake\psake calling build\build.ps1. Using the code in that post is giving me \tools\psake instead of \tools\myTool. – ashic Apr 28 '14 at 11:53
  • * ... \psake called by build\build.ps1 – ashic Apr 28 '14 at 12:05

2 Answers2

0

Use $PSScriptRoot, this should get what you need.

Raf
  • 9,681
  • 1
  • 29
  • 41
0

And if you're on PowerShell V2 (hopefully you're on at least v3), put this in fileWithFunction.ps1

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

Then access the dir like so:

"ScriptDir is $(&$ScriptDir)"

The reason the scriptblock is used is to ensure safety when dot sourcing script files.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369