Delete a class from memory in PowerShell

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:

# Application Details
# -----------------
  #ID
  $applicationID     = 1;

############################################
#
# Load Supporting Scripts
#
############################################

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;
}

############################################
#
# Load the SharePoint Snapin Module.
#
############################################

LoadSharePointModule;
############################################
#
# Display component details to user.
#
############################################

#Create object of "Application" to get app details based on the ID.
$appDetails = [Application]::new($applicationID);

Write-Host "Ending ......";

APPLICATION CLASS FILE

Class Application
{
    #Class Properties
    [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;

    #Constructor: Setup class properties.
    Application ([INT32] $appID)
    {
        Write-Host "`nGathering application details ..." -ForegroundColor Yellow;

        try
        {
            #Get the SharePoint Developer site Object.
            $this.spDevSite = Get-SPWeb -ErrorAction Stop $GLOBAL:spDevURL;
        }
        catch
        {
            Write-Host "`nUnable to connect to SharePoint Developer site!: $($GLOBAL:spDevURL)";

            #EXIT;
        }

        #Assign class property.
        $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.

+4
source share
1 answer

Famous issue

PowerShell 5.0 5.1 . DongBo Wang PowerShell 6 2016 . :

" PSModuleInfo . LastWriteTime , , ".

, PowerShell 5.0, 5.1 6.0 ( ) , .

PowerShell, . , 100 , . , 17 PowerShell 5.0 5.1 , . , , . , .

, , PowerShell. PowerShell, PowerShell, PowerShell . - PowerShell, powershell.exe:

powershell.exe -Command { Invoke-Pester }

, . , ISE , .

, . . PowerShell.

+4

Source: https://habr.com/ru/post/1672477/


All Articles