PowerShell 模块 : How to enforce minimum . NET Framework 版本?

标签 powershell

我有一个针对 Windows PowerShell 5.1 和 PowerShell Core 6.0 的 PowerShell 模块。在 Windows PowerShell(桌面版)方面,它额外依赖于 .NET Framework 4.7.1 才能正常工作。
当我最初编写模块时,我认为使用 DotNetFrameworkVersion 就足够了。进入 module manifest认为 PowerShell 会强制执行该最低要求。

DotNetFrameworkVersion key

Specifies the minimum version of the .NET Framework that is required by the module.


从那以后,我了解到没有强制执行,我什至不确定该字段的用途。在任何情况下,我都在尝试减少人们在未安装最低 .NET Framework 版本的情况下尝试使用该模块时出现的支持问题。
在最近的版本中,我在模块的 PSM1 文件中添加了一些代码,这些代码将 check the .NET Framework version加载模块时,如果未找到最低版本,则抛出错误。
这似乎很好用,但是 仅限 如果您使用 Import-Module 手动导入模块.如果您只是尝试运行其中一个模块函数并让 PowerShell 自动加载进行导入,则导入错误将被抑制,而您会得到一个相对一般的错误,例如:

Get-Blah : The 'Get-Blah' command was found in the module 'MyModule', but the module could not be loaded. For more information, run 'Import-Module MyModule'.


我想相信用户会阅读该消息,通过手动导入模块来执行它所说的操作,然后阅读/理解真正的错误消息。但是我已经有一个用户提交了一个相反的问题。
所以现在我想知道。有没有更好的方法来解决这个问题?我是否将我的版本检查代码从 PSM1 移到每个公共(public)函数中?我缺少一些更简单的东西吗?

最佳答案

我也遇到了这个,觉得很沮丧。我阅读了上面链接到 Microsoft documents how to get 的文档。使用 C# 和 PowerShell 代码的 .NetFramework 客户端安装版本。我写了this script ,您可以在另一个系统上执行:

$Url = 'https://gist.githubusercontent.com/ChrisLynch
HPE/e2f276eadd5ec96c2c2e5b5835a444eb/raw/05a2d1c98839d3c253a66fb6469c840fec9ea496/Get-InstalledDotNetFramework.ps1'
iex (([System.Net.WebClient]::new()).DownloadString($Url))

这是我在 PSM1 的标题中提出的解决方案:
if ($PSVersionTable.PSVersion -match '5.1')
{

    $ReleaseKey = Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' |  Get-ItemPropertyValue -Name Release

    if ($ReleaseKey -lt 461808)
    {
        [System.String]$Exception                                  = 'InvalidOperationException'
        [System.String]$ErrorId                                    = 'UnableToLoadModuleMissingDependancy'
        [System.Object]$TargetObject                               = 'Import-Module ModuleName'
        [System.Management.Automation.ErrorCategory]$ErrorCategory = 'ResourceUnavailable'
        [System.String]$Message                                    = 'The library is unable to load due to this sytem missing the required .NetFramework 4.7.2 client.  Please visit https://go.microsoft.com/fwlink/?LinkId=863265 to download the .NetFramework 4.7.2 Offline Installer.'

        throw [Management.Automation.ErrorRecord]::new((New-Object $Exception $Message), $ErrorID, $ErrorCategory, $TargetObject)

    }   

}

因此,当客户端尝试导入安装了较旧 .NetFramework 的模块时,会引发以下异常并且模块无法加载:

Import-Module: The library is unable to load due to this sytem missing the required .NetFramework 4.7.2 client.  Please visit https://go.microsoft.com/fwlink/?LinkId=863265 to download the
.NetFramework 4.7.2 Offline Installer.
At line:1 char:1
+ ipmo hponeview.500
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (Import-Module ModuleName:String) [Import-Module], InvalidOperationException
    + FullyQualifiedErrorId : UnableToLoadModuleMissingDependancy,Microsoft.PowerShell.Commands.ImportModuleCommand

关于PowerShell 模块 : How to enforce minimum . NET Framework 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51427789/

相关文章:

perl - 从Perl迁移到Powershell是否有很好的引用?

powershell - 终止多台远程计算机上的进程

PowerShell 模块未按预期提供点源/导入功能

powershell - 使用 powershell 将 com+ 组件添加到现有的空 com+ 应用程序包?

找不到Powershell位置参数

powershell - 如何使用 Powershell 将 .Net Web 应用程序部署到 Azure

Powershell - 将日志文件转换为 CSV

powershell - 用于CSV修改的慢速Powershell脚本

powershell - Set-ADGroup 找不到刚刚在同一脚本中创建的组 (PowerShell)

javascript - Powershell网站自动化: Javascript Popup Box Freeze