29

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

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 = @()
}

当我将两个文件复制到:

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

我可以T1在我的 PowerShell 会话中运行。但现在我想在同一个模块中添加一个新功能,即:

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

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

4

3 回答 3

55

使用该-Force命令,Import-Module它将重新加载它。

于 2016-08-17T20:04:33.223 回答
33

导入模块后,由于模块已加载到内存中,因此无法识别对其进行的更改。但是,我总是能够执行 a Remove-Module foo,然后执行 aImport-Module foo来加载新功能。

话虽如此,您的 PSD1 文件看起来不正确。它应该有一个ModuleToProcess设置为“MyModule.psm1”的字段。然后,当您执行Import-Module MyModuleor时Import-Module .\mymodule.psd1,PowerShell 将查找并加载关联的MyModule.psm1文件。我想知道这是否会导致您与某些缓存 PowerShell 发生冲突?

于 2013-09-27T15:50:20.990 回答
0

以下是迄今为止对我有用的唯一解决方案,您在其中配置 vscode 以在每次调试时运行一个新会话,我没有找到任何其他解决方案来工作和调试 Powershell 类。

在此处输入图像描述

于 2020-07-02T15:29:08.360 回答