I'm having some scope issues when dot sourcing powershell scripts. Suppose I have one script 'A.ps1':
$VERSION = "1.0"
# Dot source B.ps1
. .\B.ps1
function Write-Version { Write-Host "A.ps1 version $VERSION" }
Write-Version
And a script B.ps1
$VERSION = "2.0"
function Write-Version { Write-Host "B.ps1 version $VERSION" }
Write-Version
The output of running A.ps1 will be:
B.ps1 version 2.0
A.ps1 version 2.0
Why this happens is quite obvious. The $VERSION variable from B.ps1 is put into the scope of A.ps1 and overwrites that variable. Indeed, this happens with Write-Version as well, but here A.ps1 overwrites B's version, but because Write-Version is called in B.ps1 before that happens, we can still see the output of B's Write-Version function.
The question, of course, is how to prevent this?? I've tried various scope options, but this doesn't seem to work when dot-sourcing. And since there are functions in B.ps1 that I do need in A's scope, just invoking B.ps1 is probably not an option.
Does anyone have any ideas?