I created a class called "Application" and loaded it into my main script using
Import-Module -NAME "C:\PowerShell_Scripts\Class\Application.ps1" -GLOBAL -FORCE;
However, if I ONLY make changes to the class file and run the code in PowerShell ISE, none of these changes apply. It is almost as if the class is still in memory, although I used -FORCE.
I also tried to remove the module before loading it, and the same problem occurred:
Remove-Module "Application" -ErrorAction Ignore -FORCE;
Import-Module -NAME "C:\PowerShell_Scripts\Class\Application.ps1" -GLOBAL -FORCE;
If I make one character change in my main script, then it will reload the class! But I did not need to modify the main script to force PowerShell to reload the class, which just seems silly.
Is there a way to remove an application class from memory, if it exists?
NOTE : files with simple functions in them work. This only applies to importing a class.
Addition . In the console, if I run the Remove-Module command, it runs successfully, but I can STILL create new objects with:
$appDetails = [Application]::new($applicationID);
Doesn't make sense to me ...
MAIN SCRIPT:
$applicationID = 1;
try
{
Remove-Module "Application" -ErrorAction Ignore -FORCE;
Remove-Module "Common" -ErrorAction Ignore -FORCE;
Remove-Module "ServerData" -ErrorAction Ignore -FORCE;
Import-Module -NAME "C:\PowerShell_Scripts\Common.ps1" -GLOBAL -FORCE;
Import-Module -NAME "C:\PowerShell_Scripts\ServerData.ps1" -GLOBAL -FORCE;
Import-Module -NAME "C:\PowerShell_Scripts\Class\Application.ps1" -GLOBAL -FORCE;
}
catch
{
Write-Host "`nError: Cannot load required PowerShell scripts. Ensure C:\PowerShell_Scripts\ exists and has the required files." -ForegroundColor Red;
EXIT;
}
LoadSharePointModule;
$appDetails = [Application]::new($applicationID);
Write-Host "Ending ......";
APPLICATION CLASS FILE
Class Application
{
[STRING] $appName;
[INT32] $appID;
[INT32] $versionMajor;
[INT32] $versionOS;
[INT32] $versionCentraAdmin;
[INT32] $versionMain;
[INT32] $versionGUI;
[INT32] $versionWorkflow;
[INT32] $versionForm;
[INT32] $versionVS;
[INT32] $versionOther;
[INT32] $versionFull;
[OBJECT] $spDevSite;
[OBJECT] $versionList;
Application ([INT32] $appID)
{
Write-Host "`nGathering application details ..." -ForegroundColor Yellow;
try
{
$this.spDevSite = Get-SPWeb -ErrorAction Stop $GLOBAL:spDevURL;
}
catch
{
Write-Host "`nUnable to connect to SharePoint Developer site!: $($GLOBAL:spDevURL)";
}
$this.appID = $appID;
}
}
I intentionally set the URL for $ GLOBAL: spDevURL; so the constructor is not suitable for this test. It does not work normally and displays
Write-Host "`nUnable to connect to SharePoint Developer site!: $($GLOBAL:spDevURL)";
But if I make changes to this line and run the script, this change does not apply.