1param([Parameter(Mandatory=$true)][string]$Root, [string]$CloneHost = '', [int[]]$Lengths = @(0, 1024))
2Set-StrictMode -Version Latest
3$ErrorActionPreference = 'Stop'
4& "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost
5$app = New-Object -ComObject OneNote.Application
6$notebook = ''
7try {
8 $inputs = Join-Path $Root 'inputs'
9 New-Item -ItemType Directory -Path $inputs | Out-Null
10 $app.OpenHierarchy((Join-Path $Root 'notebook'), '', [ref]$notebook, 0)
11 $section = ''
12 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
13 foreach ($length in $Lengths) {
14 $path = Join-Path $inputs ($length.ToString() + '.bin')
15 $stream = [IO.File]::Create($path)
16 try {
17 $block = New-Object byte[] 1024
18 for ($i = 0; $i -lt $block.Length; $i++) { $block[$i] = [byte]($i % 251) }
19 for ($written = 0; $written -lt $length; $written += $block.Length) { $stream.Write($block, 0, [Math]::Min($block.Length, $length - $written)) }
20 } finally { $stream.Dispose() }
21 $page = ''
22 $app.CreateNewPage($section, [ref]$page, 0)
23 $xml = '<one:Page xmlns:one="http://schemas.microsoft.com/office/onenote/2010/onenote" ID="' + $page + '"><one:Title><one:OE><one:T>Attachment ' + $length + '</one:T></one:OE></one:Title><one:Outline><one:OEChildren><one:OE><one:InsertedFile pathSource="' + $path + '" preferredName="' + $length + '.bin"/></one:OE></one:OEChildren></one:Outline></one:Page>'
24 $app.UpdatePageContent($xml, [DateTime]::MinValue, 1, $false)
25 }
26 $app.SyncHierarchy($notebook)
27
28} finally {
29 if ($notebook) { $app.CloseNotebook($notebook, $false) }
30 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
31 $app = $null
32 [GC]::Collect()
33 [GC]::WaitForPendingFinalizers()
34}