This is only a code fragment, so it is difficult to give a definite answer.
Generally, PowerShell scoping is determined at runtime and is what I call 'downhill scoping'.
So if function a() uses a variable and calls function c() the variable exists.
Function b() does not use the variable and when it calls function c() it will be uninitialized.
Add to that event based calling, and you can get random results. Microsoft's PowerShell debugger engine has some additional 'helpers' to make variables visible that may not be in scope.
I find it best to define and initialize variables that need to be of global or script scope in a location that is guaranteed to be called first.
It is also very important that you use the same scope modifier on all instance of using said variable. Otherwise PowerShell may decide that you mean a variable of local scope.
Which seems what you are doing in the posted code fragment. The 'Import-Profile -Path $Selection' should be
Import-Profile -Path $global:Selection
Generally, PowerShell scoping is determined at runtime and is what I call 'downhill scoping'.
So if function a() uses a variable and calls function c() the variable exists.
Function b() does not use the variable and when it calls function c() it will be uninitialized.
Add to that event based calling, and you can get random results. Microsoft's PowerShell debugger engine has some additional 'helpers' to make variables visible that may not be in scope.
I find it best to define and initialize variables that need to be of global or script scope in a location that is guaranteed to be called first.
It is also very important that you use the same scope modifier on all instance of using said variable. Otherwise PowerShell may decide that you mean a variable of local scope.
Which seems what you are doing in the posted code fragment. The 'Import-Profile -Path $Selection' should be
Import-Profile -Path $global:Selection
Statistics: Posted by Alexander Riedel — Tue Jul 02, 2024 11:45 am