Discussion:
[PSH] How do I start new process and inherit all current shell var
(too old to reply)
OK
2006-07-06 18:00:01 UTC
Permalink
Guys,
How do I start new process and inherit all current shell variables?
I did read documentation, but still not clear how to do something similar to
CMD start command or UNIX & (background job)

My script looks like this:

Set lots of variables and make other one time initializations.

set databases=A B C D ... Z
@for %%d in (%databases%) do @(
start StartBackupDB.cmd %server% %%d %location%
)

I guess I can write some mixture of CMD & PowerShell.
And it will be absolutely unmanageable.
Is the any easy way to split/fork some task to another shell instance (and
inherit current shell variables)?
Thank you,
Bruce Payette [MSFT]
2006-07-06 18:29:16 UTC
Permalink
In PowerShell there is a difference between shell variables and environment
variables. Shell variables aren't exported to the child process. (They can
be any type of object so exporting them is problematic.) Environment
variables are exported. You set an environment variable by doing
$env:myvar = "Some value"

Here's an example that creates 4 instances of cmd.exe. These processes
display the value of the environment variable "value" on startup and then go
into interactive mode.

$procs = $(foreach ($i in 1..4)
{
$env:value= "I AM NUMBER $i";
[diagnostics.process]::start("cmd.exe", "/k echo %value%")
}
)

The process objects of the 4 procs are captured in $procs. This means that
the script can monitor or terminate these processes. For example, to kill
off all 4 processes, you'd do:

$procs | stop-process

-bruce
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Post by OK
Guys,
How do I start new process and inherit all current shell variables?
I did read documentation, but still not clear how to do something similar to
CMD start command or UNIX & (background job)
Set lots of variables and make other one time initializations.
set databases=A B C D ... Z
@for %%d in (%databases%) do @(
start StartBackupDB.cmd %server% %%d %location%
)
I guess I can write some mixture of CMD & PowerShell.
And it will be absolutely unmanageable.
Is the any easy way to split/fork some task to another shell instance (and
inherit current shell variables)?
Thank you,
OK
2006-07-06 18:48:02 UTC
Permalink
Bruce,
Thanks for the tip!

So the 1st option is:
[diagnostics.process]::start("PowerShell.exe", "-NoLogo -Command myJob.ps1
param1 param2 ... paramN")

The 2nd option will be to export all my variables to environment variables
instead of passing them as command line arguments.

I’m sure, there is a limit on both command line length and environment space
(~32-64KB?). But I think I will fit! :-)

Just wondering is there any plans to make this kind of things easier?
OK
Andrew Watt [MVP]
2006-07-06 18:39:45 UTC
Permalink
Hi,

Is this something that you are going to want to do *exactly* the same
multiple times?

If so, would the following work for you?

Create a script that includes multiple set-variable statements to
create your desired set of variables.

Then when you open a new PowerShell console simply run the script or,
if you will potentially do this in (almost) every console you open,
add the script's location to your profile.ps1 file so that the script
is run before the console opens, that way the same variables are
always available in a PowerShell console.

Andrew Watt MVP
Post by OK
Guys,
How do I start new process and inherit all current shell variables?
I did read documentation, but still not clear how to do something similar to
CMD start command or UNIX & (background job)
Set lots of variables and make other one time initializations.
set databases=A B C D ... Z
@for %%d in (%databases%) do @(
start StartBackupDB.cmd %server% %%d %location%
)
I guess I can write some mixture of CMD & PowerShell.
And it will be absolutely unmanageable.
Is the any easy way to split/fork some task to another shell instance (and
inherit current shell variables)?
Thank you,
OK
2006-07-06 20:46:01 UTC
Permalink
Andrew,
Thanks for suggestion.
As the matter of fact, I have done it exactly as you described already! :-)
OK

Continue reading on narkive:
Search results for '[PSH] How do I start new process and inherit all current shell var' (Questions and Answers)
12
replies
What is the true meaning of life???
started 2007-04-30 14:14:01 UTC
philosophy
Loading...