1

词汇表:

  • 主机:PowershellHost 会话
  • 交互的:[Environment]::UserInteractive -eq $True

设想:

创建一个 powershell 模块,它只会在失败的情况下正确中止并且不会出错。在这种情况下,某些命令/模块只能在完全交互式主机(如 ISE 和控制台)中正常工作,但不能在像 NuGet 包管理器控制台这样的假交互式主机中正常工作

失败解决方案:

# 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 = ''
    ....
}

失败解决方案:

# 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...

有没有更好的方法来做到这一点,或者超级模块拆分是唯一的方法?

4

1 回答 1

0

查看#requires关键字,它可能会提供一些您尚未尝试过的选项。我不知道 NuGet 包管理器控制台是否有唯一的 ShellId。

#Requires –ShellId Microsoft.PowerShell

http://technet.microsoft.com/en-us/library/hh847765.aspx

于 2014-12-06T06:24:23.927 回答