2014-10-08 2 views
4

해당 모듈 내에서 모듈을 가져온 스크립트의 경로를 가져 오는 방법이 있습니까?모듈 내에서 가져 오기 스크립트의 경로를 가져 옵니까?

내가 쓰고있는 스크립트 모듈은 가져 오기 스크립트와 관련된 파일의 설정을로드하기위한 것입니다. 나는 많은 프로젝트를 위해 모듈을 재사용 할 계획이므로, 모듈이 어디에서 가져올 것인지에 대한 가정을하지 못한다면 더 선호 할 것이다.

이것은 좋은 것으로, 모듈이 가능한 한 암시적일 수 있다는 점에서 좋을 것입니다. 다른 모든 것이 실패하더라도, 나는 그 위치에서 호출자 패스를 가질 수 있습니다.

불행히도 내가 시도한 모든 것은 모듈로의 경로를 반환합니다 (가져온 것이 아닙니다).


테스트 - RelativeModule.ps1, 에 저장 : C : C : \

import-module "$PSScriptRoot\mod\Test.psm1" 

Test.psm1, 가에 저장 \ 테스트 여기에 간단한 예제입니다 : \ test \ mod \

# returns 'c:\test\mod' 
write-host "`$PSScriptRoot: $PSScriptRoot" 

# returns 'c:\test\mod' 
# The value of $MyInvocation.MyCommand.Path is 'c:\test\mod\Test.psm1' 
write-host "Split Invoation: $(Split-Path $MyInvocation.MyCommand.Path)" 

# returns whatever path the console is currently residing 
write-host "Resolve Path: $((resolve-path '.\').path)" 

# what I'm looking for is something to return 'c:\test' from within the module 
# without making any assumptions about the folder structure 

답변

4

:

Write-Host "My invoker's PSScriptRoot: $($MyInvocation.PSScriptRoot)" 
+0

* 너무 가까이 *! 'Split-Path'는 필요하지 않습니다. Just :'Write-Host "호출자 : $ ($ MyInvocation.PSScriptRoot)"'- 편집하고 동의 할 것입니다. 감사! – klyd

+0

답변을 업데이트했습니다. –