View Single Post
Old 08-16-2008   #6 (permalink)
Joel (Jaykul) Bennett


 
 

Re: Include another script, keep variables in included script?

On Aug 16, 4:54*am, tojo2000 <tojo2...@xxxxxx> wrote:
Quote:

> Sweet. *Thanks. *The control over scope is unprecedented in PowerShell
> as far as I can tell, but the potential for confusion is unprecedented
> as well.
The problem is, the solution being offered is waaay more complicated
than what you need. It's like the game of mousetrap.
All you had to do was dot-source the second script from the first,
which makes the second script execute in the first script's scope, so
any variables it defines are left behind when it's done.

For example:
## tmp1.ps1 ############
$Test = "Here"
$There = "There"

echo "TEST: $test"
echo "There: $there"
###########################

## tmp2.ps1 ############
echo "TEST: " $Test

# a dot, a space, then the path to the second script
. .\tmp1.ps1

echo "TEST: " $Test
echo "There: " $there
###########################

That's all. no need to mess with $global, or explicit scoping or any
of that.
My System SpecsSystem Spec