Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-08-26
3
Medium Priority
?
2,817 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Accepted Solution

by:
soostibi earned 1000 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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

609 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