.NET Framework - Pass environment variables to next instance

Asked By Martin Zugec
22-Jan-10 10:40 AM
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
IHNvcnQNCiM
(1)
System.Environment
(1)
CliXML
(1)
SetEnvironmentVariable
(1)
PowerShellTunnel
(1)
VariableValue
(1)
VariableName
(1)
InputFormat
(1)
  Tome Tanasovski replied to Martin Zugec
22-Jan-10 12:23 PM
I am 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.
  qa_warrior replied to Tome Tanasovski
22-Jan-10 04:39 PM
then
ew
ll

[System.Environment]::SetEnvironmentVariable
("VariableName","VariableValue")
This will create Persistant Environment Variables. To remove them set
the value to emtpy.
  Marco Shaw [MVP] replied to Martin Zugec
22-Jan-10 06:31 PM
Agreeing with Tome, there is 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
  HerbM replied to Martin Zugec
23-Jan-10 01:48 AM
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 do not 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 did not have
time to test such, especially if my suggestion might not
be where you are headed with this.]

--
HerbM
  Martin Zugec replied to HerbM
26-Jan-10 07:44 AM
Hi guys,

there is misunderstanding - I do not 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 does not 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 replied to Martin Zugec
26-Jan-10 12:54 PM
IyBiZWZvcmUgYW55IHZhcmlhYmxlIGRlY2xhcmF0aW9uIGluIHlvdXIgUHJvZmlsZSwgc2V0IGEg
Y29uc3RhbnQgYXJyYXkNCiMgb2YgdGhlIGV4aXN0aW5nIFBTIGF1dG9tYXRpYyB2YXJpYWJsZXMg
bmFtZXMNCk5ldy1WYXJpYWJsZSBwc1ZhcnMgJChHZXQtVmFyaWFibGUgfCAlIHskXy5uYW1lfSB8
IFNvcnQtT2JqZWN0KSBgDQogLW9wdGlvbiBDb25zdGFudCAtc2NvcGUgR2xvYmFsDQoNCjwjDQog
b3B0aW9uYWx5LCBpZiB5b3UnZCBsaWtlIHRvIGhhdmUgYSBsaXN0IG9mIHZhcmlhYmxlIG5hbWVz
IGRlY2xhcmVkIGluDQogeW91ciBQcm9maWxlLCBhdCB0aGUgZW5kIG9mIGl0LCBvciBhZnRlciB0
aGUgbGFzdCB2YXJpYWJsZSBkZWNsYXJhdGlvbg0KICRteVByb2ZpbGVWYXJzID0gR2V0LVZhcmlh
YmxlIHwgPyB7JHBzVmFycyAtbm90Q29udGFpbnMgJF8ubmFtZX0gfA0KICAlIHskXy5uYW1lfSB8
IHNvcnQNCiM+DQoNCiMgdGhlbiwgYmVmb3JlIHN0YXJ0aW5nIHRoZSBuZXcgc2Vzc2lvbiwgZXhw
b3J0IHlvdXIgdmFyaWFibGVzIHRvIGEgZmlsZQ0KbHMgdmFyaWFibGU6XCogLWV4Y2x1ZGUgJHBz
VmFycyB8IEV4cG9ydC1DbGl4bWwgQzpcVmFyaWFibGVzLnhtbA0KDQojIHN0YXJ0IG5ldyBzZXNz
aW9uICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA8IS0tbWluZCB3b3JkIHdyYXAtLT4N
CnBvd2Vyc2hlbGwgLW5vZXhpdCAnSW1wb3J0LUNsaXhtbCBDOlxWYXJpYWJsZXMueG1sIHwgJSB7
c2V0ICRfLm5hbWUgJF8udmFsdWUgLU9wdGlvbiAkXy5vcHRpb25zIC1lYSAwfScNCg0KLS0gDQpS
b2JlcnQ=
  Martin Zugec replied to Robert Robelo
27-Jan-10 07:09 AM
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 replied to Martin Zugec
30-Jan-10 01:44 PM
Hi Robert,

does not 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
  Robert Robelo replied to Martin Zugec
29-Jan-10 04:26 PM
SGkgTWFydGluLA0KDQpQcmltaXRpdmUgdHlwZXMgc2hvdWxkIGJlIGRlc2VyaWFsaXplZCBpbnRv
ICdsaXZlJyBvYmplY3RzLiBPdGhlciBvYmplY3RzIGFyZSBkZXNlcmlhbGl6ZWQgYW5kIHRoZWly
IHByb3BlcnRpZXMgc2hvdWxkIHJldGFpbiB0aGVpciB2YWx1ZXMsIGJ1dCB0aGVpciBtZXRob2Rz
IGFyZSBnb25lLg0KDQpJZiB5b3Ugd2FudCB0byBkZWJ1ZyB0aGUgc2NyaXB0LCB3aHkgbm90IHRy
eSBQb3dyU2hlbGwgSVNFJ3MgZGVidWdnZXIgb3IgdGhlIERlYnVnZ2VyIENtZGxldHM6DQoNCkdl
dC1IZWxwIGFib3V0X0RlYnVnZ2Vycw0KDQoiRGVidWdnZXIgQ21kbGV0cw0KICAgICAgVGhlIFdp
bmRvd3MgUG93ZXJTaGVsbCBkZWJ1Z2dlciBpbmNsdWRlcyB0aGUgZm9sbG93aW5nIHNldCBvZiBj
bWRsZXRzOg0KDQoNCiAgICAgICAgICBTZXQtUHNCcmVha3BvaW50OiAgICAgU2V0cyBicmVha3Bv
aW50cyBvbiBsaW5lcywgdmFyaWFibGVzL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robert,

it looks like Export-CliXml is in fact acting as ToString(), that is 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
Create New Account
help
Me.FileDialog.InitialDirectory = system.Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer) ? .NET Framework Hallo, lt. Doku geht folgendes nicht: Me.FileDialog.InitialDirectory = system.Environment.GetFolderPath(System.Environment.SpecialFolder.MyComputer) Computer physisch vorhanden ist, andernfalls eine leere Zeichenfolge (""). Ein Ordner ist physisch nicht
it is, is there a workaround? Thanks for the help everyone. Anthony VB.NET Discussions System.Environment.SpecialFolder.ApplicationData (1) Environment.GetSpecialFolder.ApplicationData (1) Enviroment.GetSpecialFolder.ApplicationData (1) Environment.SpecialFolder.ApplicationData (1) System.Environment.GetFolderPath (1) System.Enviroment.GetFolderPath (1) System.Enviroment.GetFolder (1) Environment.GetFolderPath (1) Anthony Papillion so many typos and using a method instead of the enum Friend AppDataPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) Success Cor keywords: Question, about, System.Enviroment.GetFolder() description: So I am trying
Drawing.Text.InstalledFontCollection $objFonts.Families To get the folder path for a special folder use: [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Favorites) greetings, Peter Thanks, Peter, for your reply in general and especially for the tip about using the [System.Environment+SpecialFolder] enumeration to get the folder path for a special folder. This enumeration provides access to 23 of the 38 folder paths provided by the [ShellSpecialFolderConstants] enumeration: [System.Environment+SpecialFolder] | Get-Member -Static -MemberType Property Remains the question how to enumerate the [ShellSpecialFolderConstants] properties
System.Environment.OSVersion .NET Framework Hi, On my application, i would display the operating system name, but, when i display "System.Environment.OSVersion.ToString()", the result is . . . and CE OS 5.2.1620" Question : How can i 77 Detailed list can be found at http: / / www.pocketpcfaq.com / wce / versions.htm keywords: System.Environment.OSVersion description: Hi, On my application, i would display the operating system name, but, when i display System.Environment.OSVersion.ToString(), the result is . . . Microso
compilers it was in ms, is that still the case? Thanks, Alain VB.NET Discussions System.Environment.TickCount (1) GetTickCount (1) VB.NET (1) VB (1) Environment (1) Visual (1) LPreviousTimeValue (1 still ms. And, BTW, have a look here: http: / / msdn.microsoft.com / en-us / library / system.environment.tickcount.aspx - - Armin What Armin said. . . Plus, there is always System.Environment.TickCount which internally calls GetTickCount anyway :) - - Tom Shelton Strange oversight not to include a unsigned