![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Setting environments variables I need to loop though all env vars and set their value to "" (to clear the environment before setting a new one). But I can't get ps to accept a variable substitution for the name? gci env: |% { $env:$_Name=$_.Value } Unexpected token '_Name' in expression or statement. At line:1 char:26 + gci env: |% { $env:$_Name= <<<< $_.Value } Thanks, Duncan |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Setting environments variables On Aug 4, 9:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote: > I need to loop though all env vars and set their value to "" (to clear > the environment before setting a new one). But I can't get ps to > accept a variable substitution for the name? > > gci env: |% { $env:$_Name=$_.Value } > > Unexpected token '_Name' in expression or statement. > At line:1 char:26 > + gci env: |% { $env:$_Name= <<<< $_.Value } > Aha, needs the set-item cmdlet... gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""} |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Setting environments variables > > Aha, needs the set-item cmdlet... > > gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""} ....and putting it all together (just in case anyone needs to push and pop environments) we get: # initialize events hashtable for pushing/popping environments $envs=@{} function pushenv { # create a new hashtable of the current environment gci env: |% {$thisenv=@{}}{$thisenv[$_.Name]=$_.Value} # add this hashtable to the main hashtable $envs[$envs.count] = $thisenv # add current env to envs } function popenv { # clear all vars from the current env gci env: |% { &{param($name,$value) set-item -path env:$name $value} $_.Name "" } # populate current env vars from the last pushed env $envs[$envs.count-1] |% { $_.keys |% { &{param($name,$value) set- item -path env:$name $value -ea silentlycontinue} $_ $envs[$envs.count-1].$_ }} # remove last pushed env from the hashtable $envs.Remove($envs.count-1) } pushenv |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Setting environments variables On Aug 4, 4:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote: > I need to loop though all env vars and set their value to "" (to clear > the environment before setting a new one). But I can't get ps to > accept a variable substitution for the name? > > gci env: |% { $env:$_Name=$_.Value } > > Unexpected token '_Name' in expression or statement. > At line:1 char:26 > + gci env: |% { $env:$_Name= <<<< $_.Value } > > Thanks, > > Duncan Environment variables are held in a provider, like most things in Powershell, so you can just treat it as such: PS> del env:* It's not always about the pipeline ;-) - Oisin |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Setting environments variables On Aug 4, 8:03 am, Duncan Smith <DSmith1...@googlemail.com> wrote: > > Aha, needs the set-item cmdlet... > > > gci env: |% {$str = [string]$_.Name; set-item -path env:$str ""} > > ...and putting it all together (just in case anyone needs to push and > pop environments) we get: > > # initialize events hashtable for pushing/popping environments > $envs=@{} > > function pushenv > { > # create a new hashtable of the current environment > gci env: |% {$thisenv=@{}}{$thisenv[$_.Name]=$_.Value} > > # add this hashtable to the main hashtable > $envs[$envs.count] = $thisenv # add current env to envs > > } > > function popenv > { > # clear all vars from the current env > gci env: |% { &{param($name,$value) set-item -path env:$name > $value} $_.Name "" } > > # populate current env vars from the last pushed env > $envs[$envs.count-1] |% { $_.keys |% { &{param($name,$value) set- > item -path env:$name $value -ea silentlycontinue} $_ > $envs[$envs.count-1].$_ }} > > # remove last pushed env from the hashtable > $envs.Remove($envs.count-1) > > } > > pushenv Alternatively, try: "push" $vars = gci env: "pop" $vars | % { set-item "env:\$($_.key)" $_.value } ;-) - Oisin |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Setting environments variables On Aug 4, 4:38 pm, Oisin Grehan <ois...@gmail.com> wrote: > On Aug 4, 4:41 am, Duncan Smith <DSmith1...@googlemail.com> wrote: > > > I need to loop though all env vars and set their value to "" (to clear > > the environment before setting a new one). But I can't get ps to > > accept a variable substitution for the name? > > > gci env: |% { $env:$_Name=$_.Value } > > > Unexpected token '_Name' in expression or statement. > > At line:1 char:26 > > + gci env: |% { $env:$_Name= <<<< $_.Value } > > > Thanks, > > > Duncan > > Environment variables are held in a provider, like most things in > Powershell, so you can just treat it as such: > > PS> del env:* > > It's not always about the pipeline ;-) > > - Oisin A good point! I'll re-factor it for the sake of brevity. Another question: Have you seen the CustomClass samples for Payette's book (using the PSCustomObject ETS system)? I'm using it well, but can't find a way to specify a constructor so that it gets called automatically on object instantiation. Closest I have is something like: customclass axis { note axisType 0 note pt @($pt) method axis { ".ctor called" } } .. $axis = newc axis .. $axis.axis() # <- must call manually Not very satisfying... Thanks, Duncan |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Setting java class variables | mmiked8162 | Vista General | 2 | 03-27-2008 11:25 AM |
| Setting environment variables | =?Utf-8?B?Q2FsbCBtZSBCaWxs?= | Vista General | 2 | 09-07-2006 03:54 PM |
| Sundeep Raina(Iopsis): Is variables being handled in Workflow and How to declare New Variables | sunny | Avalon | 0 | 06-30-2006 01:31 AM |
| Setting environment variables? | Chaz | Vista account administration | 2 | 06-23-2006 11:26 AM |