-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowsEnvironmentVariables_Backup.py
78 lines (63 loc) · 2.68 KB
/
WindowsEnvironmentVariables_Backup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Script Developer: Gabriel Mihai Sandu
# GitHub Profile: https://github.com/Gabrieliam42
import os
import winreg
import ctypes
import sys
import time
import json
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin():
params = " ".join([f'"{arg}"' for arg in sys.argv])
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, params, None, 1)
def save_environment_variables(file_path):
env_vars = {"system": {}, "user": {}}
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment') as key:
i = 0
while True:
try:
name, value, _ = winreg.EnumValue(key, i)
env_vars["system"][name] = value
i += 1
except OSError:
break
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Environment') as key:
i = 0
while True:
try:
name, value, _ = winreg.EnumValue(key, i)
env_vars["user"][name] = value
i += 1
except OSError:
break
with open(file_path, 'w') as file:
json.dump(env_vars, file, indent=4)
file.write('\n')
with open(file_path, 'r') as file:
lines = file.readlines()
with open(file_path, 'w') as file:
for line in lines:
file.write(line)
if line.strip().endswith(','):
file.write('\n')
def restore_environment_variables(file_path):
with open(file_path, 'r') as file:
env_vars = json.load(file)
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 0, winreg.KEY_ALL_ACCESS) as key:
for name, value in env_vars["system"].items():
winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r'Environment', 0, winreg.KEY_ALL_ACCESS) as key:
for name, value in env_vars["user"].items():
winreg.SetValueEx(key, name, 0, winreg.REG_SZ, value)
if __name__ == "__main__":
if not is_admin():
print("Requesting administrative privileges...")
run_as_admin()
sys.exit()
file_path = "env_vars_backup.json"
save_environment_variables(file_path)
print(f"Environment variables saved to {file_path}")