【问题标题】:How to force a module function definition to reload without starting a new Powershell session?如何在不启动新的 Powershell 会话的情况下强制重新加载模块函数定义?
【发布时间】:2019-04-17 22:05:05
【问题描述】:

我有一个模块,我叫它xyz.ps.core。它导出一个函数 - Get-PullRequestsFromCommitIds

我修复了函数中的一个错误,重新发布了模块,重新安装并重新导入了它,但该函数仍然引用了旧版本的模块。

请注意:

C:\xyz\tip [master ≡]> Get-Command Get-PullRequestsFromCommitIds | ft -AutoSize

CommandType Name                          Version     Source
----------- ----                          -------     ------
Function    Get-PullRequestsFromCommitIds 1.0.19107.4 xyz.ps.core

如您所见,该功能来自版本1.0.19107.4

C:\xyz\tip [master ≡]> get-module xyz.ps.core | ft -AutoSize

ModuleType Version     Name             ExportedCommands
---------- -------     ----             ----------------
Manifest   1.0.19107.7 xyz.ps.core {Assert-ExtractionDestFolder, Assert-PullRequestMatchesFolder, Backup-Database, Connect-OctopusToTfs...}


C:\xyz\tip [master ≡]> get-module xyz.ps.core -ListAvailable | ft -AutoSize


    Directory: C:\Users\mkharitonov\Documents\WindowsPowerShell\Modules


ModuleType Version     Name             ExportedCommands
---------- -------     ----             ----------------
Manifest   1.0.19107.7 xyz.PS.Core {Assert-ExtractionDestFolder, Assert-PullRequestMatchesFolder, Backup-Database, Connect-OctopusToTfs...}

但是模块版本已经在1.0.19107.7。但是好的,我有一个刷新模块的功能,即使它已经安装到相同的版本:

C:\xyz\tip [master ≡]> (get-command Use-Module).ScriptBlock
param([Parameter(Mandatory)]$Name)

    if ($VerbosePreference -ne 'Continue')
    {
        Write-Host -ForegroundColor Cyan -NoNewline "Using the latest version of $Name ... "
    }

    Write-Verbose "Uninstalling all the versions of $Name ..."
    Uninstall-Module $Name -AllVersions -Force -ErrorAction SilentlyContinue
    Remove-Module $Name -Force -ErrorAction SilentlyContinue

    Write-Verbose "Installing the latest version of $Name ..."
    Install-Module $Name -Scope CurrentUser -Force

    Write-Verbose "Importing $Name into the current session ..."
    Import-Module $Name -Force

    if ($VerbosePreference -ne 'Continue')
    {
        Write-Host -ForegroundColor Cyan (Get-Module $Name).Version
    }

让我们现在使用它:

C:\xyz\tip [master ≡]> use-module xyz.ps.core
Using the latest version of xyz.ps.core ... 1.0.19107.7

让我们检查一下函数来源:

C:\xyz\tip [master ≡]> Get-Command Get-PullRequestsFromCommitIds | ft -AutoSize

CommandType Name                          Version     Source
----------- ----                          -------     ------
Function    Get-PullRequestsFromCommitIds 1.0.19107.4 xyz.ps.core

还是旧的。请注意,在新的 Powershell 窗口中,该函数取自模块的当前版本。

是否可以在不关闭powershell的情况下刷新功能?

【问题讨论】:

  • 你的意思是Import-Module modulename -Force 吗?
  • 我已经在这样做了 - 注意我用来刷新模块的函数 Use-Module 中的 Import-Module $Name -Force

标签: powershell module powershell-module


【解决方案1】:

行为都是about scopes。 TLDR:

会话、模块和嵌套提示是自包含的环境, 但它们不是会话中全局范围的子范围。

基本上,由于模块是自包含环境而不是子范围,因此它们无法将模块导入“父”脚本范围。 即使你使用-Force

让我们测试一个模块内的作用域:

sampleModule.psm1

Function Test-Import { 
param([Parameter(Mandatory)]$Name)
    Write-Host "List Loaded modules before"
    Get-Module

    Write-Host "Importing $Name into the current session ..."
    Import-Module $Name -Force

    Write-Host "Module Version $((Get-Module $Name).Version)"

    Write-Host "Loaded Modules After"
    #List Loaded modules after
    Get-Module
}

#Only present desired functions
Export-ModuleMember -Function Test-Import

如果我们从一个简单的白板测试开始(为简洁起见,我删除了无关的模块):

PS C:> #Clean state - Nothing Loaded for demonstration
PS C:> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------

PS C:> Import-Module .\sampleModule.psm1
PS C:> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Test-Module                         {Test-Import}

PS C:> Test-Import ActiveDirectory
List Loaded modules before

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Test-Module                         {Test-Import}

Importing ActiveDirectory into the current session ...
Module Version 1.0.1.0

Loaded Modules After

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicySubject...}
Script     0.0        Test-Module                         {Test-Import}      

这里我们注意到 ActiveDirectory 模块在函数开始时不存在,但确实在函数结束时加载,并报告了正确的版本。现在让我们看看它是否加载:

PS C:> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Test-Module                         {Test-Import}

正如我们所见,由于模块在它们自己的自包含环境中运行,我们成功地将模块(本例中为 ActiveDirectory)导入到模块作用域中,但没有像您期望的那样导入到本地作用域中。

解决此范围问题的唯一方法是将模块导入全局范围 通过添加-Global 喜欢:

Import-Module $Name -Force -Global

更改示例脚本中的那一行,然后重新导入:

PS C:> Import-Module .\sampleModule.psm1 -Force

PS C:> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Test-Module                         {Test-Import}

PS C:> Test-Import ActiveDirectory
List Loaded modules before

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Test-Module                         {Test-Import}

Importing ActiveDirectory into the current session ...
Module Version 1.0.1.0

Loaded Modules After

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicySubject...}
Script     0.0        Test-Module                         {Test-Import}      

和以前一样...现在让我们检查它是否正确加载:

PS C:> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   1.0.1.0    ActiveDirectory                     {Add-ADCentralAccessPolicyMember, Add-ADComputerServiceAccount, Add-ADDomainControllerPasswordReplicationPolicy, Add-ADFineGrainedPasswordPolicySubject...}
Script     0.0        Test-Module                         {Test-Import}      

成功了!

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2015-05-09
    • 1970-01-01
    • 2012-05-13
    • 2014-03-11
    相关资源
    最近更新 更多