作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个模块,我叫它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
最佳答案
行为都是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
.
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}
PS C:> Get-Module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 0.0 Test-Module {Test-Import}
-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/
我是一名优秀的程序员,十分优秀!