Solved

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

Posted on 2010-08-26
3
2,494 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now