I have a pretty straightforward PS script, that refers to a bunch of self written modules (psm1). When I run my script from PowerShell ISE I am often pulling my hair out because due to the fact that the latest version of my module is not executed (but some outdated-somewhere-stored-in-memory-version).
I have google-d, and google-d, and read, and tried, and now comes the time that I'd really appreciate to understand what the hell is going on, instead of working around these issues all the time.
Some example code :
(the script will configure PCs that will serve in our machines, setting shares, creating local users, configuring NICs, stuff like that)
I start with adding the location of my modules to the module path as follows:
# Make sure .\Modules is part of the PSModulePath environment variable
$currentPSModulePath = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
If (-Not ($currentPSModulePath -Like '*.\Modules*'))
{
[Environment]::SetEnvironmentVariable("PSModulePath", $currentPSModulePath + ";.\Modules", "Machine")
}
Write-Host ("Environment variable for PSModulePath = {0}" -f [Environment]::GetEnvironmentVariable("PSModulePath", "Machine"))
Then, I load the required modules:
### Import Modules
Import-Module -DisableNameChecking ConfigureSystemPC
Import-Module -DisableNameChecking ConfigureChipPC
Import-Module -DisableNameChecking ConfigureEngravePC
Import-Module -DisableNameChecking ConfigureCInkPC
Import-Module -DisableNameChecking ConfigureIPPC
Import-Module -DisableNameChecking ConfigureNPPC
And finally, I ask the user which PC should be configured:
### Start configuring new PC ###
Write-Host "`nChoose the PC type form the options below:`n"
Write-Host "1. System PC"
Write-Host "2. Chip PC"
Write-Host "3. Engrave PC"
Write-Host "4. CInkjet PC"
Write-Host "5. IP PC"
Write-Host "6. NP PC"
Write-Host "7. Output PC"
$pcType = Read-Host "Please enter the PC type and press [enter]"
Write-Host ("You choose option: {0}" -f $pcType)
switch ($pcType)
{
1 { Configure-SystemPC }
2 { Configure-ChipPC }
3 { Configure-EngravePC }
4 { Configure-CInkPC }
5 { Configure-IPPC }
6 { Configure-NPPC }
7 { Configure-OutputPC }
}
The problem arises, when I change something in my Configure- module. When I simply add (e.g.) Write-Host "bla bla"
, press the Save button, and debug my main script again (the script shown above), PowerShell will run the OLD version of the module.
Unless I reload the module via either:
rmo Configure-EngravePC
followed byipmo Configure-EngravePC
ipmo Configure-EngravePC -Force
the exact same "old" version of my module will be executed.
Who can point me out how to normally deal with this? And why, why oh why, do I even have to reload Modules when I run my script via the debugger? Why would it "store" Modules that were run in a different session?? Am I doing something wrong?
Many many thanks in advance, I hope somebody can elaborate on this, I get stuck way to often..
I highly prefer an answer with proper explanation (or reference to good documentation on this topic)