2014-12-04 3 views
1

용어 :파워 쉘 모듈이 지원되지 않는 호스트에 로딩을 중단

  • 호스트 : PowershellHost 세션
  • 대화 : [Environment]::UserInteractive -eq $True

시나리오 :

만 propertly 중단하는 파워 쉘 모듈을 만들고 실패한 상태에 오류없이.

# Add value to Powershell manifest(psd1) 
# Issue: Only supports a string for the `PowerShellHostName` property. How to specify both `ConsoleHost` and `Windows PowerShell ISE Host`? Unknown if this property supports regex, and even if it does, will the behavior change since it's not documented? 
@{ 
    .... 
    # Name of the Windows PowerShell host required by this module 
    # PowerShellHostName = '' 
    .... 
} 

실패 해결 방법 :이 경우, 일부 명령은/모듈은 NuGet 패키지 관리자 콘솔 같은 가짜 상호 작용하는 호스트에

실패 솔루션 ISE와 콘솔처럼, 전체 대화 형 호스트에서 제대로 작동 있지만

# Check for interactive shell 
# Issue: UserInteractive is still set in embedded shells like NuGet package manager 
# console. Commands that expect user input directly often hang. 
if([Environment]::UserInteractive) { 
    # Do stuff, dotsource, etc 
} 

실패 해결 방법 :

# Issue: returning still leaves module loaded, and it appears in Get-Module list 
# Even if value is supplied for return, powershell's return statement is 'special' 
# and the value is ignored 
if($Host.Name -inotmatch '(ConsoleHost|Windows PowerShell ISE Host)') { 
    Write-Warning "Host [$($Host.Name)] not supported, aborting" 
    return 
} 

실패 솔루션 :

# Issue: Module isn't loaded, so it can't be unloaded 
if($Host.Name -inotmatch '(ConsoleHost|Windows PowerShell ISE Host)') { 
    Remove-Module ThisModuleName 
} 

실패 해결 방법 :

# Issue: Powershell module error output is just passthrough, import-module 
# still reports success, even though $Error is has more stuff than before 
if($Host.Name -inotmatch '(ConsoleHost|Windows PowerShell ISE Host)') { 
    Write-Error "Unsupported Host:" $Host.Name 
} 

성가신 솔루션 :

# Issue: Leaves two errors on the stack, one for the throw, one for the module not 
# loading successfully 
if($Host.Name -inotmatch '(ConsoleHost|Windows PowerShell ISE Host)') { 
    throw "Host [$($Host.Name)] not supported, aborting" 
} 

아니 솔루션 :

Force user to wrap the import every time. 

의심 솔루션 :

모듈을 중첩 된 서브 모듈로 분할합니다 (하나는 '공통'용이고 다른 하나는 지원되는 호스트 용입니다). 각각에 대해 하위 폴더를 사용하고 각각에 대해 psd1을 복제하십시오. 마치 중첩 된 종속성과 관련하여 유지 보수의 악몽이 될 것으로 보입니다.

UberModule 
    /ModuleCommon 
     /ModuleCommon.(psd1|psm1) 
    /ConsoleHostSpecific 
     /ConsoleHostSpecific.(psd1|psm1) 
    /IseHostSpecific 
     /IseHostSpecific.(psd1|psm1) 
    /etc... 

이렇게하는 더 좋은 방법이 있습니까? 아니면 위버 모듈이 유일한 방법입니까?

답변