Yikes, 1100 word read... little overkill...
darthchocobo,
You are correct you ran into a scope problem. Posh, however behaved it self...
Focus in on the TheFunction portion of your script. When you dot source anything in powershell it executes it into your current scope. therefor...
function TheFunction{
Write-Host $var1
$var1 = "GoodBye"
Write-Host $var1
}
write-Host $var1
##### the culprit #####
. TheFunction
Write-Host $var1
By dot sourcing your function it is the same as doing the following...
write-Host $var1
Write-Host $var1 # This is
$var1 = "GoodBye" # Your Function
Write-Host $var1 # Running
Write-Host $var1
In effect the code contained within the function is copied into your current session and then executed. So when do you use '&'? Poshoholic did a fantastic job expaining that little bugger the other day scroll all the way down...
since your calling a function without any wild conditions just call it... posh know's what your trying to do here...
$var1 = "Hello"
function TheFunction {
Write-Host $var1
$var1 = "GoodBye"
Write-Host $var1
}
write-Host $var1
TheFunction
Write-Host $var1
Scoping still kicks me in the teeth some times. No such problem with vbscript ;) still getting used to it all... ~Glenn
BTW, having to edit this three times to get the code tags to work properly is a pain.