2013-09-27 15 views
20

Prima di importare il mio modulo PowerShell (MyModule.psm1), ho scritto una funzione in esso:PowerShell: Modifica di un modulo già importato

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

Nel mio MyModule.psd1:

@{ 

    # Minimum version of the Windows PowerShell engine required by this module 
    PowerShellVersion = '2.0' 

    # Name of the Windows PowerShell host required by this module 
    PowerShellHostName = '' 

    # Minimum version of the Windows PowerShell host required by this module 
    PowerShellHostVersion = '2.0' 

    # Modules that must be imported into the global environment prior to importing this module 
    RequiredModules = @() 

    # Script files (.ps1) that are run in the caller's environment prior to importing this module 
    ScriptsToProcess = @() 

    # Modules to import as nested modules of the module specified in ModuleToProcess 
    NestedModules = @() 

    # Functions to export from this module 
    FunctionsToExport = '*' 

    # Cmdlets to export from this module 
    CmdletsToExport = '*' 

    # Variables to export from this module 
    VariablesToExport = '*' 

    # List of all modules packaged with this module 
    ModuleList = @() 

    # List of all files packaged with this module 
    FileList = @() 

} 

Questo è importata bene, quando ho copiato entrambi i file in:

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

e sono in grado di eseguire T1 nella mia sessione PowerShell.

Ma ora ho voluto aggiungere una nuova funzione nella stesso modulo vale a dire .:

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

Anche dopo aver riavviato la mia sessione di PowerShell, non è mai riconoscono T2, tuttavia T1 funziona ancora.

Come posso modificare il mio modulo già importato in modo tale che le modifiche sono immediatamente disponibili ...

Grazie mille ...

+2

'import-module mymodule -force' non è sufficiente? –

+0

Provato anche quello, ma invano ... :( –

+0

Grazie amico ... Appena rimosso il modulo, importato di nuovo e provato con "import-module MyModule -force" E ha funzionato aggiungendo altre 4 funzioni .. –

risposta

22

Una volta che il modulo è stato importato, le modifiche ad esso non vengono riconosciuti dal momento che la il modulo è caricato in memoria. Tuttavia, sono sempre stato in grado di fare un Remove-Module foo, seguito da un Import-Module foo per caricare nuove funzioni.

Tutto ciò detto, il file PSD1 non sembra corretto. Dovrebbe avere un campo ModuleToProcess impostato su "MyModule.psm1". Quindi, quando si esegue Import-Module MyModule o Import-Module .\mymodule.psd1, PowerShell troverà & caricare il file MyModule.psm1 associato. Mi chiedo se questo ti stia causando l'esecuzione di un po 'di PowerShell nella cache?

+0

Keith, ho postato solo parte del file psd1 che sta configurando la metodologia di esportazione: ha ModuleToProcess, o non sarà in grado di importare lo script del mio modulo, vero? –

+1

Questo è un po 'corretto. quella voce per caricare PSM1 quando viene importato il PSD1.Tuttavia, puoi importare il modulo mymodule.psm1 che bypassa il tuo PSD1.Sono abbastanza sicuro che se esegui mymodule import-module elaborerà il PSD1 che poi istruirà PowerShell che MyModule.psm1 deve essere caricato. –

34

Utilizzare il comando -Force con Import-Module e lo ricaricherà.

Problemi correlati