1param([Parameter(Mandatory=$true)][string]$Root,
2 [Parameter(Mandatory=$true)][string]$ExpectedText,
3 [Parameter(Mandatory=$true)][string]$Text)
4Set-StrictMode -Version Latest
5$ErrorActionPreference = 'Stop'
6$root = [IO.Path]::GetFullPath($Root).TrimEnd('\')
7if ([IO.Path]::GetDirectoryName($root) -ne 'C:\one-tests\runs') { throw 'Choose a run directly below C:\one-tests\runs.' }
8& "$PSScriptRoot\cold-current.ps1" -Root $root
9$app = New-Object -ComObject OneNote.Application
10$notebook = ''
11try {
12 $app.OpenHierarchy("$root\notebook", '', [ref]$notebook, 0)
13 $section = ''
14 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
15 $deadline = [DateTime]::UtcNow.AddSeconds(30)
16 $matches = @()
17 do {
18 $hierarchy = ''
19 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
20 [xml]$tree = $hierarchy
21 $pages = @($tree.SelectNodes('//*[local-name()="Page"]'))
22 if ($pages.Count -gt 1) { throw 'Expected a single demo page.' }
23 if ($pages.Count -eq 1) {
24 $content = ''
25 $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1)
26 [xml]$page = $content
27 $matches = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object { $_.InnerText -eq $ExpectedText })
28 if ($matches.Count -eq 1) { break }
29 }
30 Start-Sleep -Milliseconds 250
31 } while ([DateTime]::UtcNow -lt $deadline)
32 if ($matches.Count -ne 1) { throw 'Expected one matching demo text object.' }
33 [IO.File]::WriteAllText("$root\before.xml", $content, [Text.Encoding]::UTF8)
34 $matches[0].InnerText = [Security.SecurityElement]::Escape($Text)
35 $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false)
36 $app.SyncHierarchy($notebook)
37} finally {
38 try { if ($notebook) { $app.CloseNotebook($notebook, $false) } }
39 finally {
40 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
41 $app = $null
42 [GC]::Collect()
43 [GC]::WaitForPendingFinalizers()
44 Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 10
45 }
46}