2013-09-03 2 views
2

해결 : 다음과 같이 배치자식 powershell 스크립트를 실행하기위한 경로를 올바르게 설정하는 방법은 무엇입니까?

내가 PowerShell은 스크립트를 가지고 어셈블리 파일 : [System.Reflection.Assembly]::LoadFrom((Join-Path (pwd) "MyAssembly.dll")) | out-null

원래의 질문 : 아래의 솔루션은 다음 가입 경로를 사용하는 것입니다

D:\path1\script1.ps1 
D:\path2\script2.ps1 
D:\path2\MyAssembly.dll 

D : \ path1 \ script1.ps1 내에서 D : \ path2 \ script2.ps1을 호출해야합니다. 그런 다음 script2.ps1은 이미 D : \ path2 폴더에있는 일부 C# 어셈블리를로드합니다. 나는 Set-Location 또는 Push-Location 중 하나를 수행하면 script2.ps1 내에서 일어나는 일에 대해 작업 디렉토리가 올바르게 설정된다는 인상 아래에있었습니다.

script1.ps1은 다음과 같습니다

$otherpath = "D:\path2" 

Set-Location -Path $otherpath 

Push-Location -Path $otherpath 

.\script2.ps1 

script2.ps1은 다음과 같습니다

[System.Reflection.Assembly]::LoadFrom("MyAssembly.dll") | out-null 

을 그러나, 나는 D에있을 때 : \ 경로 1, I는 다음과 같이 script1.ps1을 실행 다음, 다음 나는 FileNotFound 예외 얻을 : 내가 제대로 경로를 설정 같은 나는 D에서 script1.ps1 호출 할 때 그렇게 어떻게

D:\path1>powershell .\script1.ps1 

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 
'file:///D:\path1\MyAssembly.dll' or one of its dependencies. The system cannot find the file 
specified." 
At D:\path2\script2.ps1:1 char:1 
+ [System.Reflection.Assembly]::LoadFrom("MyAssembly.dll") | out-null 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : FileNotFoundException 

을 : \ path1이면 환경에 D : \ path2가 D : \ path2에있는 어셈블리 호출에 대한 작업 디렉토리로 올바르게 설정되어 있습니까?

+0

을 당신에게 MyAssembly.dll을 변경하면 어떻게 \ MyAssembly.dll을합니다.? 아니면 더 나은 아직 그것을 전체 경로를 명시 적으로? – EBGreen

+0

. \이 추가되지 않으면 같은 오류가 발생합니다. 그러나'(Join-Path (pwd) $ assemblyName) '을 사용하면 효과적이다. 팁 고마워! 나는 위의 질문을 대답으로 업데이트 할 것이다. 이 답을 입력하면 선택하겠습니다. –

+2

아니, 너 알아 냈어. 당신이 원한다면 몇 시간 후에 (나는 얼마나 오랫동안) 당신이 그것을 받아 들일 수 있는지에 대한 답을 게시합니다. – EBGreen

답변

0

는 다음의 가입 경로 사용

[System.Reflection.Assembly]::LoadFrom((Join-Path (pwd) "MyAssembly.dll")) | out-null 
0

[System.Reflection.Assembly]::LoadFrom((Join-Path (pwd) "MyAssembly.dll")) | out-null을 사용하면 script2의 작업 디렉토리가 변경되지 않는 한 계속 작동합니다.

대체 방법으로는 $MyInvocation.MyCommand.Path을 사용하면 항상 현재 스크립트의 전체 경로를 제공 할 수 있습니다. 이처럼 사용할 수 있습니다

[System.Reflection.Assembly]::LoadFrom((Join-Path (Split-Path $MyInvocation.MyCommand.Path) "MyAssembly.dll")) | out-null

+0

의견으로 추가했지만 필요한 담당자가 없습니다. – peterot

관련 문제