| 1 | param([Parameter(Mandatory=$true)][string]$Root, [Parameter(Mandatory=$true)][string]$CloneHost) |
| 2 | Set-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 | function Save-Page([string]$pageId, [string]$name) { |
| 9 | $content = '' |
| 10 | $app.GetPageContent($pageId, [ref]$content, 0, 1) |
| 11 | [IO.File]::WriteAllText((Join-Path $Root $name), $content, [Text.Encoding]::UTF8) |
| 12 | } |
| 13 | try { |
| 14 | $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebookId, 0) |
| 15 | $sectionId = '' |
| 16 | $app.OpenHierarchy('pictures.one', $notebookId, [ref]$sectionId, 0) |
| 17 | # Two new pages with OneNote's own title, date and time: one keeps its date, the |
| 18 | # other has its date and time changed after it was saved once. |
| 19 | $ids = @() |
| 20 | foreach ($title in @('Kept date', 'Changed date')) { |
| 21 | $pageId = '' |
| 22 | $app.CreateNewPage($sectionId, [ref]$pageId, 0) |
| 23 | $escaped = [Security.SecurityElement]::Escape($pageId) |
| 24 | $xml = "<one:Page xmlns:one='$namespace' ID='$escaped'><one:Title><one:OE><one:T>$title</one:T></one:OE></one:Title><one:Outline><one:Position x='72' y='144'/><one:OEChildren><one:OE><one:T>Body of $title</one:T></one:OE></one:OEChildren></one:Outline></one:Page>" |
| 25 | $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $false) |
| 26 | $ids += $pageId |
| 27 | } |
| 28 | $app.SyncHierarchy($notebookId) |
| 29 | Start-Sleep -Seconds 3 |
| 30 | Save-Page $ids[1] 'before.xml' |
| 31 | $escaped = [Security.SecurityElement]::Escape($ids[1]) |
| 32 | $update = "<one:Page xmlns:one='$namespace' ID='$escaped' dateTime='2024-03-05T14:30:00.000Z'/>" |
| 33 | [IO.File]::WriteAllText((Join-Path $Root 'update.xml'), $update, [Text.Encoding]::UTF8) |
| 34 | $app.UpdatePageContent($update, [DateTime]::MinValue, 1, $false) |
| 35 | $app.SyncHierarchy($notebookId) |
| 36 | Start-Sleep -Seconds 3 |
| 37 | Save-Page $ids[1] 'after.xml' |
| 38 | Save-Page $ids[0] 'kept.xml' |
| 39 | $app.CloseNotebook($notebookId, $false) |
| 40 | $notebookId = '' |
| 41 | } finally { |
| 42 | if ($notebookId) { $app.CloseNotebook($notebookId, $false) } |
| 43 | [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app) |
| 44 | [GC]::Collect() |
| 45 | [GC]::WaitForPendingFinalizers() |
| 46 | } |