1param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost)
2Set-StrictMode -Version Latest
3$ErrorActionPreference = 'Stop'
4& "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost
5$namespace = 'http://schemas.microsoft.com/office/onenote/2010/onenote'
6$app = New-Object -ComObject OneNote.Application
7$notebookId = ''
8$log = @()
9try {
10 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0)
11 $hierarchy = ''
12 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
13 [IO.File]::WriteAllText((Join-Path $Root 'structure-00-opened.xml'), $hierarchy, [Text.Encoding]::UTF8)
14 $second = ''
15 $app.OpenHierarchy('Second.one', $notebookId, [ref]$second, 3)
16 $third = ''
17 $app.OpenHierarchy('Third.one', $notebookId, [ref]$third, 3)
18 $group = ''
19 $app.OpenHierarchy('Group', $notebookId, [ref]$group, 2)
20 $inner = ''
21 $app.OpenHierarchy('Inner.one', $group, [ref]$inner, 3)
22 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
23 [IO.File]::WriteAllText((Join-Path $Root 'structure-01-created.xml'), $hierarchy, [Text.Encoding]::UTF8)
24 $app.SyncHierarchy($notebookId)
25 $app.CloseNotebook($notebookId, $false)
26 $notebookId = ''
27 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0)
28 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
29 [xml]$tree = $hierarchy
30 $manager = New-Object Xml.XmlNamespaceManager($tree.NameTable)
31 $manager.AddNamespace('one', $namespace)
32 $sections = @($tree.SelectNodes('//one:Section', $manager))
33 foreach ($section in $sections) {
34 switch ($section.GetAttribute('name')) {
35 'Second' { $section.SetAttribute('name', 'Renamed'); $section.SetAttribute('color', '#FFD75E') }
36 'Third' { $section.SetAttribute('color', '#B7C997') }
37 }
38 }
39 [IO.File]::WriteAllText((Join-Path $Root 'structure-02-update.xml'), $tree.OuterXml, [Text.Encoding]::UTF8)
40 $app.UpdateHierarchy($tree.OuterXml, 1)
41 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
42 [IO.File]::WriteAllText((Join-Path $Root 'structure-03-renamed.xml'), $hierarchy, [Text.Encoding]::UTF8)
43 $app.SyncHierarchy($notebookId)
44 $app.CloseNotebook($notebookId, $false)
45 $notebookId = ''
46 Copy-Item -Recurse (Join-Path $Root 'notebook') (Join-Path $Root 'notebook-renamed')
47 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0)
48 $deadline = [DateTime]::UtcNow.AddSeconds(60)
49 do {
50 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
51 [xml]$tree = $hierarchy
52 if (@($tree.SelectNodes('//*[local-name()="Section"]')).Count -ge 4) { break }
53 Start-Sleep -Milliseconds 500
54 } while ([DateTime]::UtcNow -lt $deadline)
55 $manager = New-Object Xml.XmlNamespaceManager($tree.NameTable)
56 $manager.AddNamespace('one', $namespace)
57 [IO.File]::WriteAllText((Join-Path $Root 'structure-03b-reopened.xml'), $hierarchy, [Text.Encoding]::UTF8)
58 $doomed = @($tree.SelectNodes('//*[local-name()="Section"]') | Where-Object { $_.GetAttribute('name') -eq 'Third' })
59 if ($doomed.Count -ne 1) { throw "Expected one section named Third, found $($doomed.Count)." }
60 $app.DeleteHierarchy($doomed[0].GetAttribute('ID'), [DateTime]::MinValue, $false)
61 $app.GetHierarchy($notebookId, 4, [ref]$hierarchy, 1)
62 [IO.File]::WriteAllText((Join-Path $Root 'structure-04-deleted.xml'), $hierarchy, [Text.Encoding]::UTF8)
63 $app.SyncHierarchy($notebookId)
64 $app.CloseNotebook($notebookId, $false)
65 $notebookId = ''
66} finally {
67 if ($notebookId) { $app.CloseNotebook($notebookId, $false) }
68 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
69 [GC]::Collect()
70 [GC]::WaitForPendingFinalizers()
71}