Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不启动新的Powershell会话的情况下强制重新加载模块函数定义?_Powershell_Module_Powershell Module - Fatal编程技术网

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

如何在不启动新的Powershell会话的情况下强制重新加载模块函数定义?,powershell,module,powershell-module,Powershell,Module,Powershell Module,我有一个模块,我叫它xyz.ps.core。它导出一个函数-Get pullrequestsfromcommitds 我修复了函数中的一个bug,重新发布了模块,重新安装并重新导入了它,但函数仍然引用了模块的旧版本 请注意: C:\xyz\tip [master ≡]> Get-Command Get-PullRequestsFromCommitIds | ft -AutoSize CommandType Name Version

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

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

请注意:

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的情况下刷新函数?

行为就是全部。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
如果我们从一个简单的空白测试开始(为了简洁起见,我删除了无关的模块):

在这里,我们注意到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}      

成功

你的意思是
导入模块modulename-Force
?我已经在做了-注意
在我用来刷新模块的函数体
中导入模块$Name-Force
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}