Discussion:
Pass environment variables to next instance
(too old to reply)
Martin Zugec
2010-01-22 15:40:08 UTC
Permalink
Hi everyone,

today I run into small problem with PowerShell. I would like to start new
Powershell.exe from existing instance, however new process should see all
variables (similar to Start and Start /I in old cmd).

Any idea?

Martin
Tome Tanasovski
2010-01-22 17:23:02 UTC
Permalink
I'm not sure of a built-in way, but you could easily call get-variable in
your calling script and dump those variables to a txt file. You could then
read the text file in your destination script and recreate the variables.
Post by Martin Zugec
Hi everyone,
today I run into small problem with PowerShell. I would like to start new
Powershell.exe from existing instance, however new process should see all
variables (similar to Start and Start /I in old cmd).
Any idea?
Martin
.
qa_warrior
2010-01-22 21:07:43 UTC
Permalink
On Jan 22, 9:23 am, Tome Tanasovski
Post by Tome Tanasovski
I'm not sure of a built-in way, but you could easily call get-variable in
your calling script and dump those variables to a txt file.  You could then
read the text file in your destination script and recreate the variables.
Post by Martin Zugec
Hi everyone,
today I run into small problem with PowerShell. I would like to start new
Powershell.exe from existing instance, however new process should see all
variables (similar to Start and Start /I in old cmd).
Any idea?
Martin
.
[System.Environment]::SetEnvironmentVariable
("VariableName","VariableValue")
This will create Persistant Environment Variables. To remove them set
the value to emtpy.
Marco Shaw [MVP]
2010-01-22 23:31:46 UTC
Permalink
Agreeing with Tome, there's no "built-in way" to share your variables other
than using some kind of "transfer mechanism" like Tom suggests with saving
to a file to transfer.

You may want to check this for fun:
http://code.msdn.microsoft.com/PowerShellTunnel

Marco
Post by Martin Zugec
Hi everyone,
today I run into small problem with PowerShell. I would like to start new
Powershell.exe from existing instance, however new process should see all
variables (similar to Start and Start /I in old cmd).
Any idea?
Martin
HerbM
2010-01-23 06:48:25 UTC
Permalink
Maybe I am missing exactly where in the calling sequence
you want to get and send this but how about something along
the lines of (in the calling instance):

$env = (get-childitem env:)

Then pass it to the new process and use it to set the env:

The following just prints, but something based on this could
be used to set each variable in the child process:

foreach ($e in $env) {"$($e.key) = $($e.value)" }

There is might be some cute, elegant, trivial way to just
use the hashtable but I don't know it (yet.)

[Note: You can of course print out the $env variable by
just naming it, but the 'foreach' command represents how
you would model setting each variable...but I didn't have
time to test such, especially if my suggestion might not
be where you are headed with this.]
--
HerbM
Post by Martin Zugec
Hi everyone,
today I run into small problem with PowerShell. I would like to start new
Powershell.exe from existing instance, however new process should see all
variables (similar to Start and Start /I in old cmd).
Any idea?
Martin
Martin Zugec
2010-01-26 10:10:27 UTC
Permalink
Hi guys,

there is misunderstanding - I don't want to pass Env variables, but I
want to pass powershell variables.

To better understand problem - I can pretty complex frameworks and for
troubleshooting I am using function Start-Debug - it will
automatically invoke every line and return output. Problem is that
this doesn't behave the same way as in powershell console itself AND
any unexpected error is trapped using trap defined by framework.

What I want to do is that I want to take everything from Variable: and
run new powershell (debug) instance. Once I am finished with
debugging, I would just exit this Powershell.exe and return back to
previous instance.

Martin
Robert Robelo
2010-01-26 17:54:49 UTC
Permalink
# before any variable declaration in your Profile, set a constant array
# of the existing PS automatic variables names
New-Variable psVars $(Get-Variable | % {$_.name} | Sort-Object) `
-option Constant -scope Global

<#
optionaly, if you'd like to have a list of variable names declared in
your Profile, at the end of it, or after the last variable declaration
$myProfileVars = Get-Variable | ? {$psVars -notContains $_.name} |
% {$_.name} | sort
#>

# then, before starting the new session, export your variables to a file
ls variable:\* -exclude $psVars | Export-Clixml C:\Variables.xml

# start new session <!--mind word wrap-->
powershell -noexit 'Import-Clixml C:\Variables.xml | % {set $_.name $_.value -Option $_.options -ea 0}'

--
R
Martin Zugec
2010-01-27 07:45:05 UTC
Permalink
Hi Robert,

looks fine, I will test it tonight :)

I am still pretty curious if there is a chance to combine Export-
CliXML and -InputFormat parameter of Powershell.exe itself.

Martin
Martin Zugec
2010-01-29 13:51:41 UTC
Permalink
Hi Robert,

doesn't work as expected :( All objects are converted to string when
used with *-CliXML, so you cannot use this in case you want to pass WMI
\COM objects :(

Any other idea? What I want is in fact very simple - have debug
console opened in middle of script execution :(

Martin
Martin Zugec
2010-02-01 09:15:45 UTC
Permalink
Hi Robert,

it looks like Export-CliXml is in fact acting as ToString(), that's why WMI
classes are translated to WMI instances :(

My problem is that I cannot rely fully on built-in debugging, because (as
far as I remember), it is available only in Posh V2 and most of my customers
are running Posh v1 :(

Martin
Hi Martin,
Primitive types should be deserialized into 'live' objects. Other objects
are deserialized and their properties should retain their values, but
their methods are gone.
If you want to debug the script, why not try PowrShell ISE's debugger or
Get-Help about_Debuggers
"Debugger Cmdlets
The Windows PowerShell debugger includes the following set of
Set-PsBreakpoint: Sets breakpoints on lines, variables, and
commands.
Get-PsBreakpoint: Gets breakpoints in the current session.
Disable-PsBreakpoint: Turns off breakpoints in the current
session.
Enable-PsBreakpoint: Re-enables breakpoints in the current
session.
Remove-PsBreakpoint: Deletes breakpoints from the current
session.
Get-PsCallStack: Displays the current call stack.
"
In the ISE's help search for 'How to Debug Scripts in Windows PowerShell
ISE'
If you want to read more about Serialization, Deserialization and
http://blogs.msdn.com/powershell/archive/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions.aspx
--
Robert
Continue reading on narkive:
Loading...