|
1 | | - |
2 | 1 | function Merge-EnvFromBackup { |
3 | | -<# |
4 | | -.SYNOPSIS |
5 | | - Compara um backup JSON e aplica adições/alterações seletivas nas variáveis de ambiente. |
6 | | -
|
7 | | -.PARAMETER BackupDir |
8 | | - Pasta contendo SystemEnvironment.json e UserEnvironment.json gerados pelo Export-EnvBackup. |
9 | | -
|
10 | | -.EXAMPLE |
11 | | - Merge-EnvFromBackup -BackupDir D:\Backups\EnvBackup-20250428-235812 |
12 | | -#> |
13 | | - [CmdletBinding(SupportsShouldProcess)] |
14 | | - param( |
15 | | - [Parameter(Mandatory)][string]$BackupDir |
16 | | - ) |
17 | | - |
18 | | - $bkSysPath = Join-Path $BackupDir 'SystemEnvironment.json' |
19 | | - $bkUsrPath = Join-Path $BackupDir 'UserEnvironment.json' |
20 | | - if (!(Test-Path $bkSysPath) -or !(Test-Path $bkUsrPath)) { |
21 | | - throw "BackupDir não contém os arquivos JSON necessários." |
22 | | - } |
23 | | - |
24 | | - $bkSys = Get-Content $bkSysPath -Raw | ConvertFrom-Json |
25 | | - $bkUsr = Get-Content $bkUsrPath -Raw | ConvertFrom-Json |
26 | | - |
27 | | - $stamp = Get-Date -Format 'yyyyMMdd-HHmmss' |
28 | | - $rollbackDir = "EnvMergeRestore-$stamp" |
29 | | - New-Item -ItemType Directory -Path $rollbackDir | Out-Null |
30 | | - reg export "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "$rollbackDir\System_before.reg" /y |
31 | | - reg export "HKCU\Environment" "$rollbackDir\User_before.reg" /y |
32 | | - |
33 | | - function Merge-Scope { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Compara um backup JSON e aplica adições/alterações seletivas nas variáveis de ambiente. |
| 5 | + |
| 6 | + .PARAMETER BackupDir |
| 7 | + Pasta contendo SystemEnvironment.json e UserEnvironment.json gerados pelo Export-EnvBackup. |
| 8 | + |
| 9 | + .EXAMPLE |
| 10 | + Merge-EnvFromBackup -BackupDir D:\Backups\EnvBackup-20250428-235812 -WhatIf |
| 11 | + #> |
| 12 | + [CmdletBinding(SupportsShouldProcess)] |
34 | 13 | param( |
35 | | - [string]$ScopeName, |
36 | | - [psobject]$Backup, |
37 | | - [string]$RegPath |
| 14 | + [Parameter(Mandatory)] |
| 15 | + [string]$BackupDir |
38 | 16 | ) |
39 | | - $current = Get-ItemProperty -Path $RegPath | Select-Object -ExcludeProperty PS* |
40 | | - foreach ($prop in $Backup.PSObject.Properties) { |
41 | | - $name = $prop.Name |
42 | | - $value = $prop.Value |
43 | | - if ($current.PSObject.Properties.Name -notcontains $name) { |
44 | | - Write-Host "[$ScopeName] + $name" -ForegroundColor Green |
45 | | - if ($PSCmdlet.ShouldProcess($name,"Add")) { |
46 | | - Set-ItemProperty -Path $RegPath -Name $name -Value $value -Force |
| 17 | + |
| 18 | + # ---------------------- Carrega backup ---------------------- |
| 19 | + $bkSys = Get-Content (Join-Path $BackupDir 'SystemEnvironment.json') -Raw | ConvertFrom-Json |
| 20 | + $bkUsr = Get-Content (Join-Path $BackupDir 'UserEnvironment.json') -Raw | ConvertFrom-Json |
| 21 | + |
| 22 | + # ---------------------- Snapshot rollback ------------------ |
| 23 | + $rollback = 'EnvMergeRestore-{0:yyyyMMdd-HHmmss}' -f (Get-Date) |
| 24 | + New-Item -Path $rollback -ItemType Directory | Out-Null |
| 25 | + reg export 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' "$rollback\System_before.reg" /y |
| 26 | + reg export 'HKCU\Environment' "$rollback\User_before.reg" /y |
| 27 | + |
| 28 | + # ---------------------- Função de merge -------------------- |
| 29 | + function Invoke-MergeScope { |
| 30 | + param( |
| 31 | + [string] $ScopeName, |
| 32 | + [psobject]$Backup, |
| 33 | + [string] $RegPath |
| 34 | + ) |
| 35 | + |
| 36 | + $current = Get-ItemProperty -Path $RegPath | Select-Object -ExcludeProperty PS* |
| 37 | + |
| 38 | + foreach ($prop in $Backup.PSObject.Properties) { |
| 39 | + |
| 40 | + $name = $prop.Name |
| 41 | + $newValue = $prop.Value |
| 42 | + $id = "[$ScopeName] $name" |
| 43 | + |
| 44 | + if ($current.PSObject.Properties.Name -notcontains $name) { |
| 45 | + if ($PSCmdlet.ShouldProcess($id, 'Add')) { |
| 46 | + Set-ItemProperty -Path $RegPath -Name $name -Value $newValue -Force |
| 47 | + } |
47 | 48 | } |
48 | | - } |
49 | | - elseif ($current.$name -ne $value) { |
50 | | - Write-Host "[$ScopeName] ~ $name" -ForegroundColor Yellow |
51 | | - Write-Host " Atual: $($current.$name)" |
52 | | - Write-Host " Backup: $value" |
53 | | - $ans = Read-Host 'Sobrescrever? (S/N)' |
54 | | - if ($ans -match '^[sSyY]') { |
55 | | - if ($PSCmdlet.ShouldProcess($name,"Replace")) { |
56 | | - Set-ItemProperty -Path $RegPath -Name $name -Value $value -Force |
| 49 | + elseif ($current.$name -ne $newValue) { |
| 50 | + Write-Host "$id difere." -ForegroundColor Yellow |
| 51 | + Write-Host " Atual : $($current.$name)" -ForegroundColor DarkGray |
| 52 | + Write-Host " Backup: $newValue" -ForegroundColor DarkGray |
| 53 | + |
| 54 | + $ans = Read-Host 'Sobrescrever? (S/N)' |
| 55 | + if ($ans -match '^[sSyY]') { |
| 56 | + if ($PSCmdlet.ShouldProcess($id, 'Replace')) { |
| 57 | + Set-ItemProperty -Path $RegPath -Name $name -Value $newValue -Force |
| 58 | + } |
57 | 59 | } |
58 | 60 | } |
59 | 61 | } |
60 | 62 | } |
61 | | - } |
62 | | - |
63 | | - Merge-Scope 'SISTEMA' $bkSys 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' |
64 | | - Merge-Scope 'USUÁRIO' $bkUsr 'HKCU:\Environment' |
65 | | - |
66 | | - # Broadcast WM_SETTINGCHANGE |
67 | | - Add-Type @" |
| 63 | + |
| 64 | + Invoke-MergeScope 'SISTEMA' $bkSys 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' |
| 65 | + Invoke-MergeScope 'USUÁRIO' $bkUsr 'HKCU:\Environment' |
| 66 | + |
| 67 | +# Broadcast WM_SETTINGCHANGE |
| 68 | +Add-Type @" |
68 | 69 | using System; |
69 | 70 | using System.Runtime.InteropServices; |
70 | | -public class NativeMethods{ |
71 | | -[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] |
72 | | -public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, |
73 | | - uint fuFlags, uint uTimeout, out UIntPtr lpdwResult); |
| 71 | +public class NativeMethods { |
| 72 | + [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] |
| 73 | + public static extern IntPtr SendMessageTimeout( |
| 74 | + IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, |
| 75 | + uint fuFlags, uint uTimeout, out UIntPtr lpdwResult); |
74 | 76 | } |
75 | 77 | "@ |
76 | | - [NativeMethods]::SendMessageTimeout([intptr]0xFFFF,0x1A,[UIntPtr]::Zero,'Environment',2,1000,[ref]([UIntPtr]::Zero)) | Out-Null |
| 78 | + [UIntPtr]$r = [UIntPtr]::Zero |
| 79 | + [NativeMethods]::SendMessageTimeout( |
| 80 | + [IntPtr]0xFFFF, 0x1A, [UIntPtr]::Zero, 'Environment', |
| 81 | + 2, 1000, [ref]$r |
| 82 | + ) | Out-Null |
77 | 83 | } |
0 commit comments