While getting the value of a static member from inside a dot-sourced ps1 file always works, setting a static member seems inconsistent.
test.ps1:
class A
{
static $val
A()
{
[A]::val = "empty"
}
}
$A = [A]::new()
write-host $([A]::val)
[A]::val = "test"
. .\test2.ps1
write-host $([A]::val)
test2.ps1
[A]::val = "val"
Console output:
empty
test
Console output I expected:
empty
val
So, I added a write-host to test2.ps1:
[A]::val = "val"
write-host $([A]::val)
And ran it again:
empty
val
val
Okay, I say to myself.
I remove the write-host from test2.ps1 and I get another unexpected console output:
empty
val
Am I missing something ?