Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2906
  • Last Modified:

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

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
chad
Asked:
chad
  • 2
1 Solution
 
soostibiCommented:
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
 
soostibiCommented:
The extra '.' inside coll.ps1 makes script variables global.
0
 
chadAuthor Commented:
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now