![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #11 (permalink) |
| | 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 Specs![]() |
| | #12 (permalink) |
| | 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: 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 Specs![]() |
| | #13 (permalink) |
| | 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: 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 Specs![]() |
| | #14 (permalink) |
| | 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 Specs![]() |
| | #15 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||