1

I know this topic is ooold, but unanswered in all communities I found. It is very indiviual.

I have a script, using classes from an external class file.

The classes are named x.psm1" and imported via Using module ".\x.psm1"

Here is some Code (censored)

Logger.psm1

class Logger 
{
    [string] $LogFile = ""
    ...

    Logger()
    {
        ...
    }

    Logger([string] $logFile)
    {
        if($logFile.Length -gt 0){
            $this.LogFile = $logFile
        }

        ...
    }

    ...

    # Logging works fine now, so I will not list all methods.
}

TestRunner.psm1

# Usings
Using module ".\Logger.psm1"

class TestRunner
{ 
    [Logger] $Logger
    [string] $RootPath = ""
    [string] $LogPath = ""
    [string] $RegExFilter = ""

    TestRunner([Logger] $logger)
    {
        $this.Logger = $logger
    }

    Execute()
    {
       $this.Logger.WriteInformation("Test", $false) 
       # After changing "Test" to "Other" it will still output "Test"
    }
}

StartTestRunner.ps1

# Usings
Using module ".\Logger.psm1"
Using module ".\TestRunner.psm1"

# Parameters
param(
    [switch] $Help,
    [switch] $P1, # ParameterSet "P1"
    [switch] $P2, # ParameterSet "P2"
    [string] $RootPath,
    [string] $LogPath,
    [string] $RegEx
)

# Fields
$LogFile = "$(Get-Location)\TestLog.txt"
if($LogPath.Length -gt 0)
{
    $LogFile = $LogPath
}

[Logger] $Logger = [Logger]::new($LogFile)#

function ValidateParamsAndRun
{
    ValidateParameterSets

    if($Help)   {ShowHelp}
    
    if($RootPath.Length -eq 0){
        $RootPath = Get-Location
        $Logger.LogWarning("No path provided. Using default: $($RootPath)")
    }

    if($P1)    
    {
        $Logger.LogInformation("Starting P1 tests")
        [TestRunner]$runner = [TestRunner]::new($Logger)
        $runner.RootPath = $RootPath
        $runner.RegExFilter = $RegEx

        $runner.Execute()
    }
    if($P2)
    {
        ...
    }

function ValidateParameterSets
{
    $count = 0
    if($Help) {$count++}
    if($P1) {$count++}
    if($P2) {$count++}

    if($count -gt 1)
    {
        $Logger.LogError("Invalid parameters found. Please use '-Help' to see options.
        ")
        Exit(-1)
    }
}

function ShowHelp
{
    WriteInformation "
    Give some 
    multiline Help.
    It works.
    "
}

# Main Program starts here
ValidateParamsAndRun

I tried the MS-PS-ISE and VS Code with PowerShell Extension.

I executed the script with the internal terminals of each and with an external PS-Window.

I tried this solution from SO, this and did not know what to do here.

The only thing that worksis to close and restart the Terminal. But this is not my expectation for writing code :-)

Coming from C#, my thought was, if there are errors in the code, it dosn't compile and executes the old version instead, but without a warning. Does PowerShell behave like that? Another problem is, that most error messages don't show the file or the line of code they occur.

Are there any cache options?

  • When an error occurs not all object are disposed. I found files are left open after a PS script fails and then I cannot run the script again because I do not have permission to write to files that were left open. See following : https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose?force_isolation=true – jdweng Mar 31 '23 at 13:59
  • The short of it: you cannot force-reload a module that was loaded via a `using module` statement - yet you need the latter in order to import _classes_. If classes weren't in the picture, you could use `Import-Module -Force` Otherwise, you either need a workaround that requires modifying the source code _or_, if you're using Visual Studio Code with the PowerShell extension, configure it to start a new, temporary session for every debugging run. See the linked duplicate for details. – mklement0 Mar 31 '23 at 14:13

0 Answers0