Link to home
Start Free TrialLog in
Avatar of ranski
ranski

asked on

Powershell required to backup Environmental Variable

Hi all,

I'm looking for a way to backup a specific variable with the value on machines and then place a temporary one in it's place for a number of weeks for a trial licence. Once this trial is complete I then need to restore that variable to how it was before.

Can anyone help?

thanks
Avatar of Qlemo
Qlemo
Flag of Germany image

The first part:
rename-item env:MyVar MyVar_Save
$env:MyVar = 'Something else'
dir env:MyVar*   # just for test

Open in new window

Reverting:
remove-item env:MyVar
rename-item env:MyVar_Save MyVar

Open in new window

The "time trigger" will need to get implemented somewhere else, e.g. as a scheduled task.
Avatar of ranski
ranski

ASKER

thanks.  How would I add settings to the EV?
Avatar of ranski

ASKER

also will the above work if the EV isn't present on a system?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If the env var does not exist, the rename will fail, but the new var set. And when reverting, the "new" var will get deleted, and the old not found to rename back, so nothing will stay left.

What do you mean exactly with "add settings to the EV"? An env var contains a string, nothing else.
oBdA, that is absolutely correct. How embarrassing ...
Avatar of ranski

ASKER

Thanks both.

The EV I need to backup is called LICENSE_FILE and i plan to run this as a logon script. Does the above achieve this?

Once it's backed up a new item with the same name needs to be created with a different list of licence servers in the value.
A login script runs each time the user logs in. How should that work here?
Avatar of ranski

ASKER

We could use a drop file. If present do nothing?
Check in the GUI whether this is a User variable or a System variable.
Or open a PS and check which one of these does not throw an error:
[Environment]::GetEnvironmentVariable('LICENSE_FILE', 'Machine')
[Environment]::GetEnvironmentVariable('LICENSE_FILE', 'User')

Open in new window

If it's a System variable, using a logon script won't work, as the change requires administrative permissions. You'd then need a Startup script to do that.

Qlemo,
the script won't backup again when the backup variable exists already.
Avatar of ranski

ASKER

Hi oBdA

The EV is System based and is already being set via Group Policy. There may however be locations that have set this manually which is why i need to go that extra but further than just backup the GPO and make a change then revert back.

It's really important that we capture all machines and force then to use the new licence servers but then allow us to revert back a month later.

Any ideas?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial