Editing an already imported module
Asked Answered
W

3

29

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

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

In my 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

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
}

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.

Westbound answered 27/9, 2013 at 7:39 Comment(3)
import-module mymodule -force is not enough?Baecher
Tried that as well, but in vain.. :(Westbound
Thanks buddy... Just removed the module, Imported again and tried with "import-module MyModule -force" And it worked by adding 4 more functions...Westbound
G
34

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.

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?

Grampositive answered 27/9, 2013 at 15:50 Comment(2)
Keith, I only posted part of psd1 file which is setting up the Export methodology. It does have ModuleToProcess, or it won't be able to import my module script, Right?Westbound
That is sort of correct. You have to have that entry to load the PSM1 when your PSD1 is imported. However, you can import-module mymodule.psm1 which bypasses your PSD1. I'm pretty sure though if you do import-module mymodule it will process the PSD1 which would then instruct PowerShell that MyModule.psm1 needs to be loaded.Grampositive
E
58

Use the -Force command with the Import-Module and it will reload it.

Eugenieeugenio answered 17/8, 2016 at 20:4 Comment(0)
G
34

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.

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?

Grampositive answered 27/9, 2013 at 15:50 Comment(2)
Keith, I only posted part of psd1 file which is setting up the Export methodology. It does have ModuleToProcess, or it won't be able to import my module script, Right?Westbound
That is sort of correct. You have to have that entry to load the PSM1 when your PSD1 is imported. However, you can import-module mymodule.psm1 which bypasses your PSD1. I'm pretty sure though if you do import-module mymodule it will process the PSD1 which would then instruct PowerShell that MyModule.psm1 needs to be loaded.Grampositive
T
0

Below is the only solution that worked for me so far, in which you configure vscode to run a new session on each debug, I didnt find any other solution to work and debug Powershell classes.

enter image description here

Tetter answered 2/7, 2020 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.