This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
I had some problems creating Sytem-wide environment variables :
Setting the Path / system variables from MSH I found a workaround,
Create System Variable From MSH but also I got some Excelent answers from the NG (both better) :
One from Keith Hill :
Take a look at .NET's System.Environment.SetEnvironmentVariable() methods.
One of the overloads takes an enum that lets you specify User or System
environment variable.
--
KeithI did And yes :
this Works :
[System.Environment]::SetEnvironmentVariable("NewSysVar","Val",[System.EnvironmentVariableTarget]"Machine")
and a tip from Jeffrey Snover :
MSH> $env = (new-object -com "WScript.Shell").Environment("SYSTEM")
MSH> ,$env gm
TypeName: System.__ComObject#{f935dc29-1cf0-11d0-adb9-00c04fd58a0b}
Notice the leading ",". The problem that you had was the $env is ENUMERABLE
so putting it into the pipeline unrolls it into its parts (strings), By
putting a "," in front you are creating a LIST of 2 elements
1) $NULL
2) $ENV (the IENUMERABLE)
This list gets unrolled and passes on its parts ($ENV) so that GM reports
correctly.
The less esoteric way to address this is:
MSH> gm -input $env
In any case, the Setter works:
MSH> $env.item("TMP") = "c:\temp",$env.count()
and this one really got me, that little comma solves alot of the problems I was having exploring .NET collections etc. from MSH.
E.g. the problem I had while working with The DataTable :
see
Strange behavour of get-member on DataTables I will blog more about this later I think, but for now this works :
$dt = new-object system.data.dataTable
,$dt gm
thx Both.
gr /\/\o\/\/