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?