Solved

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

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

22 Experts available now in Live!

Get 1:1 Help Now