gpt4 book ai didi

powershell - 如何在不启动新的 Powershell session 的情况下强制重新加载模块函数定义?

转载 作者:行者123 更新时间:2023-12-02 23:35:47 25 4
gpt4 key购买 nike

我有一个模块,我叫它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 的情况下刷新功能?

最佳答案

行为都是about scopes . TLDR:

Sessions, modules, and nested prompts are self-contained environments, but they are not child scopes of the global scope in the session.



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

让我们测试一个模块内的范围:

sample 模块.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}

成功!

关于powershell - 如何在不启动新的 Powershell session 的情况下强制重新加载模块函数定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55736597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com