Solved

Powershell - Calling multiple PS1 scripts from batch file keeping same session and global variables

Posted on 2010-08-26
3
2,586 Views
Last Modified: 2012-05-10
I have several ps1 files:
1.ps1
2.ps1
3.ps1

I have variables that I would like 2 to use which are set by 1.  I can set them global but that makes them available for the same session only.  If I run the scripts via a batch file it creates new sessions for each ps1.  Is there a way to open each ps1 using a single session via a batch file?

inside 1.ps1
$a = 20

inside 2.ps1
write-host $a

copy of current batch:
powershell . c:\powershell\powershellscripts\trial\1.ps1
powershell . c:\powershell\powershellscripts\trial\2.ps1

Thanks again,
C
0
Comment
Question by:kabaam
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 250 total points
ID: 33535414
Put the two script call into another script, and call that from batch (I assume that the current path is the path of all the ps1 files):

inside: coll.ps1
. .\1.ps1
. .\2.ps1

batch:

powershell .\coll.ps1
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33535420
The extra '.' inside coll.ps1 makes script variables global.
0
 
LVL 11

Author Comment

by:kabaam
ID: 33535477
oh bother.  again I get hit with the forest and trees :-)
I was too focused on using the batch file to call all of the ps files.  
Thanks again.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question