-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathone.ps1
101 lines (80 loc) · 3.74 KB
/
one.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
Import-Module -DisableNameChecking .\src\Import-All.psm1
Function one {
[CmdletBinding()]
param (
[Parameter()]
[string]
$ConversionConfigurationExportPath
)
try {
Set-StrictMode -Version Latest
# Fix encoding problems for languages other than English
$PSDefaultParameterValues['*:Encoding'] = 'UTF-8'
$totalerr = @()
# Validate dependencies
Validate-Dependencies
# Compile and validate configuration
$config = Compile-Configuration $PSScriptRoot | Validate-Configuration
"Configuration:" | Write-Host -ForegroundColor Cyan
$config | Print-Configuration
# Connect to OneNote
$OneNote = New-OneNoteConnection
# Get the hierarchy of OneNote objects as xml
$hierarchy = Get-OneNoteHierarchy -OneNoteConnection $OneNote
# Get and validate the notebook(s) to convert
$notebooks = @(
if ($config['targetNotebook']['value']) {
$hierarchy.Notebooks.Notebook | Where-Object { $_.Name -eq $config['targetNotebook']['value'] }
}else {
$hierarchy.Notebooks.Notebook
}
)
if ($notebooks.Count -eq 0) {
if ($config['targetNotebook']['value']) {
throw "Could not find notebook of name '$( $config['targetNotebook']['value'] )'"
}else {
throw "Could not find notebooks"
}
}
"`nNotebooks to convert:" | Write-Host -ForegroundColor Cyan
$notebooks.name | Write-Host -ForegroundColor Green
# Convert the notebook(s)
$pageConversionConfigsAll = @()
foreach ($notebook in $notebooks) {
"`nConverting notebook '$( $notebook.name )'... (Ignoring deleted notes)" | Write-Host -ForegroundColor Cyan
New-SectionGroupConversionConfig -OneNoteConnection $OneNote -NotesDestination $config['notesDestPath']['value'] -Config $config -SectionGroups $notebook -LevelsFromRoot 0 -ErrorVariable +totalerr | Tee-Object -Variable pageConversionConfigs | Convert-OneNotePage -OneNoteConnection $OneNote -Config $config -ErrorVariable +totalerr
"`nDone converting notebook '$( $notebook.name )' with $( ($pageConversionConfigs | Measure-object).Count ) notes." | Write-Host -ForegroundColor Cyan
$pageConversionConfigsAll += $pageConversionConfigs
}
# Export all Page Conversion Configuration objects as .json, which is useful for debugging
if ($ConversionConfigurationExportPath) {
"`nExporting Page Conversion Configuration as JSON file with $( $pageConversionConfigsAll.Count ) objects: $ConversionConfigurationExportPath" | Write-Host -ForegroundColor Cyan
$pageConversionConfigsAll | ConvertTo-Json -Depth 100 | Out-File $ConversionConfigurationExportPath -Encoding utf8 -Force
}
}catch {
if ($ErrorActionPreference -eq 'Stop') {
throw
}else {
Write-Error -Message $_.Exception.Message
Write-Error -Message $_.ScriptStackTrace
}
}finally {
'Cleaning up...' | Write-Host -ForegroundColor Cyan
# Disconnect OneNote connection
if (Get-Variable -Name OneNote -ErrorAction SilentlyContinue) {
Remove-OneNoteConnection -OneNoteConnection $OneNote
}
# Print any conversion errors
Print-ConversionErrors -ErrorCollection $totalerr
'Exiting.' | Write-Host -ForegroundColor Cyan
# Remove all imported modules
get-module | Remove-Module
}
}
if (!$Exit) {
# Entrypoint
$params = @{
ConversionConfigurationExportPath = $ConversionConfigurationExportPath
}
one @params
}