Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Include another script, keep variables in included script?

Reply
 
Old 08-17-2008   #11 (permalink)
Kiron


 
 

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

> I disagree, and I'm no authority
My sarcastic reply was not to you tojo, and I'm not trying to confuse anyone, just sharing what PowerShell's versatility I know of that could be useful.

--
Kiron

My System SpecsSystem Spec
Old 08-17-2008   #12 (permalink)
Alex K. Angelopoulos


 
 

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

If you use the "." invocation operator instead of &, you can do this
directly. Simply change this line in tmp2:

.\tmp.ps1

to this:

. .\tmp.ps1

The (.) tells PowerShell to invoke script tmp.ps1 in the current scope,
instead of creating a new subscope.

"pschmidt" <pschmidt@xxxxxx> wrote in message
news:E720E2CB-2A98-4DE6-A8B1-61ABAE9AB2BE@xxxxxx
Quote:

>
> I want to run or include a PS script from within another PS script. But
> want the values set in the 2nd one to be available in the first one.
>
> For example:
> tmp.ps1----
> $Test = "Here"
> $There = "There"
>
> echo "TEST: $test"
> echo "There: $there"
> ------------------------
>
> Now tmp2.ps1-------------
> echo "TEST: " $Test
>
> .\tmp.ps1
>
> echo "TEST: " $Test
> echo "There: " $there
> -------------
>
> When I run tmp2.ps1, I get:
>
Quote:

>> .\tmp2.ps1
> TEST:
> TEST: Here
> There: There
> TEST:
> There:
> --------------------------
>
> I'd like the values set in 'tmp' to be available in tmp2. How do I do
> this?
>
> Thx,
>
>
My System SpecsSystem Spec
Old 08-17-2008   #13 (permalink)
Alex K. Angelopoulos


 
 

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

Never mind; I didn't read the sub-thread that Tojo started before posting
this. ; )

"Alex K. Angelopoulos" <aka(at)mvps.org> wrote in message
news:uZdDsxFAJHA.3556@xxxxxx
Quote:

> If you use the "." invocation operator instead of &, you can do this
> directly. Simply change this line in tmp2:
>
> .\tmp.ps1
>
> to this:
>
> . .\tmp.ps1
>
> The (.) tells PowerShell to invoke script tmp.ps1 in the current scope,
> instead of creating a new subscope.
>
> "pschmidt" <pschmidt@xxxxxx> wrote in message
> news:E720E2CB-2A98-4DE6-A8B1-61ABAE9AB2BE@xxxxxx
Quote:

>>
>> I want to run or include a PS script from within another PS script. But
>> want the values set in the 2nd one to be available in the first one.
>>
>> For example:
>> tmp.ps1----
>> $Test = "Here"
>> $There = "There"
>>
>> echo "TEST: $test"
>> echo "There: $there"
>> ------------------------
>>
>> Now tmp2.ps1-------------
>> echo "TEST: " $Test
>>
>> .\tmp.ps1
>>
>> echo "TEST: " $Test
>> echo "There: " $there
>> -------------
>>
>> When I run tmp2.ps1, I get:
>>
Quote:

>>> .\tmp2.ps1
>> TEST:
>> TEST: Here
>> There: There
>> TEST:
>> There:
>> --------------------------
>>
>> I'd like the values set in 'tmp' to be available in tmp2. How do I do
>> this?
>>
>> Thx,
>>
>>
My System SpecsSystem Spec
Old 08-18-2008   #14 (permalink)
pschmidt


 
 

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


Excellent info - thanks.

The particular example the . invocation operator works fine. But I
definitely can seem some uses for exporting or declaring different scopes in
my 'called' script.

So both are very useful - thanks. Excellent info.

Perry


"Kiron" wrote:
Quote:

> You can export the variables to tmp2's scope:
>
> -< tmp.ps1 >-
> param ([string[]]$exportVariable)
>
> $Test = "Here"
> $There = "There"
>
> echo "TEST: $test"
> echo "There: $there"
>
> if ($exportVariable) {
> foreach ($var in $exportVariable) {
> set-variable $var (get-variable $var).value -scope 1
> }
> }
> -< tmp.ps1 >-
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
> -< tmp2.ps1 >-
> # $test echo "TEST: $Test"
>
> # assuming both scripts are in same Dir
> $cmd = join-path (split-path $myInvocation.myCommand.path) tmp.ps1
> & $cmd -ex test, there
>
> echo "TEST: $Test"
> echo "There: $there"
> -< tmp2.ps1 >-
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
> .\tmp2
>
> --
> Kiron
>
My System SpecsSystem Spec
Old 08-18-2008   #15 (permalink)
Kiron


 
 

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

> Excellent info - thanks.

You're welcome Perry. There is also a similar way to call a function defined in a script without executing the entire script, or having to dot-source the script to define it in the caller's scope; the called function is not defined in the caller's scope either, it just executes in the called script's scope.
This sort of thing is available in v2 CTP2 through Modules. But if you, or anyone else, would like to do this in v1, I'll be glad to share it.

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Retaining variables after script execution PowerShell
Need help with PHP Script variables Network & Sharing
Pop-up to enter variables to pass to script PowerShell
Passing variables to script PowerShell
Best Approach for Included Script Files PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46