1# Usage: .\profile.ps1 Isolate|Restore|Status
2# Parks the native OneNote profile for tests. It never changes notebook files.
3param(
4 [Parameter(Mandatory = $true, Position = 0)]
5 [ValidateSet('Isolate', 'Restore', 'Status')]
6 [string]$Command
7)
8
9$ErrorActionPreference = 'Stop'
10$root = 'C:\one-tests'
11$key = 'HKCU:\Software\Microsoft\Office\14.0\OneNote'
12$regKey = 'HKCU\Software\Microsoft\Office\14.0\OneNote'
13$cache = Join-Path $env:LOCALAPPDATA 'Microsoft\OneNote\14.0'
14$originalReg = Join-Path $root 'profile-original.reg'
15$originalCache = Join-Path $root 'profile-original-cache'
16$testReg = Join-Path $root 'profile-test.reg'
17$testCache = Join-Path $root 'profile-test-cache'
18
19function Test-IsolatedProfile {
20 if (-not (Test-Path -LiteralPath "$key\Options\Paths")) { return $false }
21 try {
22 return (Get-ItemProperty -LiteralPath "$key\Options\Paths" -Name UnfiledNotesSection).UnfiledNotesSection -eq "$root\Loose.one"
23 } catch {
24 return $false
25 }
26}
27
28if ($Command -ne 'Status' -and (Get-Process ONENOTE -ErrorAction SilentlyContinue)) {
29 throw 'Close OneNote, then run this command again.'
30}
31
32if ($Command -eq 'Status') {
33 $profile = if (Test-IsolatedProfile) { 'test' } elseif (Test-Path -LiteralPath $key) { 'other' } else { 'missing' }
34 $activeCache = Test-Path -LiteralPath $cache
35 $originals = (Test-Path -LiteralPath $originalReg) -and (Test-Path -LiteralPath $originalCache)
36 $tests = (Test-Path -LiteralPath $testReg) -or (Test-Path -LiteralPath $testCache)
37
38 if ($originals -and $profile -eq 'test') {
39 Write-Output 'State: isolated. Run Restore when native tests finish.'
40 } elseif ($originals -and ($tests -or $profile -eq 'missing')) {
41 Write-Output 'State: restore can resume. Run Restore with OneNote closed.'
42 } elseif ((Test-Path -LiteralPath $originalReg) -and -not (Test-Path -LiteralPath $originalCache) -and $tests -and $activeCache -and $profile -eq 'other') {
43 Write-Output 'State: restored. Test backups remain parked.'
44 } else {
45 Write-Output 'State: unrecognized. Inspect the paths below before changing the profile.'
46 }
47
48 Write-Output "Registry: $profile"
49 Write-Output "Active cache: $activeCache"
50 Write-Output "Original registry backup: $(Test-Path -LiteralPath $originalReg)"
51 Write-Output "Original cache backup: $(Test-Path -LiteralPath $originalCache)"
52 Write-Output "Test registry backup: $(Test-Path -LiteralPath $testReg)"
53 Write-Output "Test cache backup: $(Test-Path -LiteralPath $testCache)"
54 return
55}
56
57if ($Command -eq 'Isolate') {
58 if ((Test-Path -LiteralPath $originalReg) -or (Test-Path -LiteralPath $originalCache)) {
59 throw 'Original backup already exists. Run Status or Restore before isolating again.'
60 }
61 if ((Test-Path -LiteralPath $testReg) -or (Test-Path -LiteralPath $testCache)) {
62 throw 'Test backup already exists. Inspect Status before isolating again.'
63 }
64 if (-not (Test-Path -LiteralPath $key)) { throw 'The OneNote profile registry was not found. Nothing changed.' }
65 if (-not (Test-Path -LiteralPath $cache)) { throw 'The OneNote cache was not found. Nothing changed.' }
66
67 New-Item -ItemType Directory -Path $root -Force | Out-Null
68 & reg.exe export $regKey $originalReg
69 if ($LASTEXITCODE -ne 0) { throw 'The original registry profile was not saved. Nothing changed.' }
70 Move-Item -LiteralPath $cache -Destination $originalCache
71 & reg.exe delete $regKey /f
72 if ($LASTEXITCODE -ne 0) { throw 'The original registry remains saved. Run Restore to recover it.' }
73 New-Item -Path $key -Force | Out-Null
74 New-ItemProperty -Path $key -Name FirstBootStatus -PropertyType DWord -Value 0x1000101 | Out-Null
75 New-Item -Path "$key\Options\Paths" -Force | Out-Null
76 New-ItemProperty -Path "$key\Options\Paths" -Name UnfiledNotesSection -PropertyType ExpandString -Value "$root\Loose.one" | Out-Null
77 Write-Output 'State: isolated. The original profile and cache are parked.'
78 return
79}
80
81if (-not (Test-Path -LiteralPath $originalReg)) { throw 'The original registry backup is missing. Nothing changed.' }
82if (-not (Test-Path -LiteralPath $originalCache)) {
83 if ((Test-Path -LiteralPath $cache) -and (Test-Path -LiteralPath $testReg) -and -not (Test-IsolatedProfile)) {
84 Write-Output 'State: restored. Test backups remain parked.'
85 return
86 }
87 throw 'The original cache backup is missing. Inspect Status before restoring.'
88}
89if ((Test-Path -LiteralPath $cache) -and (Test-Path -LiteralPath $testCache)) {
90 throw 'Both active and parked test caches exist. Inspect Status before restoring.'
91}
92if (-not (Test-Path -LiteralPath $testReg) -and (Test-Path -LiteralPath $key)) {
93 & reg.exe export $regKey $testReg
94 if ($LASTEXITCODE -ne 0) { throw 'The test registry profile was not saved. Nothing changed.' }
95}
96if ((Test-Path -LiteralPath $cache) -and -not (Test-Path -LiteralPath $testCache)) {
97 Move-Item -LiteralPath $cache -Destination $testCache
98}
99if (Test-Path -LiteralPath $key) {
100 & reg.exe delete $regKey /f
101 if ($LASTEXITCODE -ne 0) { throw 'The test profile is parked. Run Restore to try again.' }
102}
103& reg.exe import $originalReg
104if ($LASTEXITCODE -ne 0) { throw 'The test profile is parked. Run Restore to try again.' }
105if (Test-Path -LiteralPath $cache) { throw 'The original cache is still parked. Run Status before restoring.' }
106Move-Item -LiteralPath $originalCache -Destination $cache
107Write-Output 'State: restored. Test backups remain parked.'