本文介绍了编辑已经导入的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在导入Powershell模块(MyModule.psm1)之前,我已经在其中编写了一个函数:

Before importing my powershell module (MyModule.psm1), I have written one function in it:

Function T1()
{
    Write-Host "T1 is just called" -ForegroundColor red
}

在我的MyModule.psd1中:

@{
    PowerShellVersion = '2.0'
    PowerShellHostName = ''
    PowerShellHostVersion = '2.0'
    RequiredModules = @()
    ScriptsToProcess = @()
    NestedModules = @()
    FunctionsToExport = '*'
    CmdletsToExport = '*'
    VariablesToExport = '*'
    ModuleList = @()
    FileList = @()
}

当我将两个文件复制到以下位置时,这很好导入:

This is imported fine, when I copied both files in:

C:\Users\fwaheed\Documents\WindowsPowerShell\Modules\MyModule

,并且能够在PowerShell会话中运行T1.但是现在我想在同一模块中添加新功能,即:

and I'm able to run T1 in my PowerShell session. But now I wanted to add a new function in same module i.e.:

Function T2()
{
    Write-Host "Its now T2.." -ForegroundColor red
}

即使重新启动PowerShell会话后,它也无法识别T2,但是T1仍在工作.如何编辑已经导入的模块,以便可以立即进行更改.

Even after restarting my PowerShell session, it never recognize T2, however T1 is still working. How can I edit my already imported module such that changes are available immediately.

推荐答案

一旦导入了模块,由于该模块已加载到内存中,因此无法识别对其所做的更改.但是,我一直能够执行Remove-Module foo,然后执行Import-Module foo来加载新功能.

Once a module has been imported, changes to it are not recognised since the module is loaded into memory. However, I've always been able to do a Remove-Module foo, followed by an Import-Module foo to load new functions.

所有这些,您的PSD1文件看起来不正确.它应该具有一个ModuleToProcess字段设置为'MyModule.psm1'.然后,当您执行Import-Module MyModuleImport-Module .\mymodule.psd1时,PowerShell将找到&加载关联的MyModule.psm1文件.我想知道这是否使您无法运行某些缓存PowerShell?

All that said, your PSD1 file doesn't look right. It should have a ModuleToProcess field set to 'MyModule.psm1'. Then when you do Import-Module MyModule or Import-Module .\mymodule.psd1, PowerShell will find & load the associated MyModule.psm1 file. I wonder if that is causing you to run afoul of some caching PowerShell does?

这篇关于编辑已经导入的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 22:25