词汇表:
[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...
有更好的方法可以做到这一点,还是超级模块拆分是唯一的方法?
答案 0 :(得分:0)
查看#requires
关键字,它可能会提供您尚未尝试的一些选项。我不知道NuGet Package Manager Console是否具有唯一的ShellId。
#Requires –ShellId Microsoft.PowerShell