Update OneNoteXmlExport.ps1

This commit is contained in:
2026-01-22 10:31:15 +00:00
parent f8480a1612
commit 62d515fcb3
+32 -123
View File
@@ -1,74 +1,6 @@
# https://stackoverflow.com/questions/53689087/powershell-and-onenote
# http://thebackend.info/powershell/2017/12/onenote-read-and-write-content-with-powershell/
# https://stackoverflow.com/questions/53639041/how-to-access-contents-of-onenote-page
# Get export folder
Function Get-Folder($initialDirectory) {
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select an export folder"
$foldername.rootfolder = "MyComputer"
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}
# Spider and find each page, create directory for each group
Function Spider-OneNote-Notebook {
param( $onenote, $node, $path )
foreach($child in $node.ChildNodes) {
if ($child.HasChildNodes) {
if ($child.isRecycleBin -ne 'true') {
$folder = Join-Path -Path $path -ChildPath $child.name
New-Item -Path $folder -ItemType directory | Out-Null
Write-Host "Section: $($folder)"
Spider-OneNote-Notebook -onenote $onenote -node $child -path $folder
}
} else {
Export-OneNote-Page -onenote $onenote -node $child -path $path
}
}
}
# Export page
Function Export-OneNote-Page {
param( $onenote, $node, $path )
# Replace invalid file characters
$name = ReplaceIllegal -text $node.name
$file = $(Join-Path -Path $path -ChildPath "$($name).htm")
Write-Host "Page: $($file)"
# Export
$onenote.Publish($node.ID, $file, 7, "")
Export-OneNote-Attachments -onenote $onenote -node $node -path $path
}
# Copy embedded attachments
Function Export-OneNote-Attachments {
param ( $onenote, $node, $path )
$xml = ''
$schema = @{one=”http://schemas.microsoft.com/office/onenote/2013/onenote”}
$onenote.GetPageContent($node.ID, [ref]$xml)
$xml | Select-Xml -XPath "//one:Page/one:Outline/one:OEChildren/one:OE/one:InsertedFile" -Namespace $schema | foreach {
$file = Join-Path -Path $path -ChildPath $_.Node.preferredName
Write-Host "Attachment: $($file)"
Copy-Item $_.Node.pathCache -Destination $file
}
}
Function ReplaceIllegal {
param ( $text )
$illegal = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\'
$replaced = $text -replace "[$illegal]",'_'
return $replaced
}
# Get export folder # Get export folder
$folder = "C:\Users\pgeeraert\Downloads\export-onenote" $folder = "C:\Users\pgeeraert\Downloads\export-onenote"
@@ -77,63 +9,40 @@ $OneNote = New-Object -ComObject OneNote.Application
[xml]$Hierarchy = "" [xml]$Hierarchy = ""
$OneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy) $OneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$Hierarchy)
Write-Output $Hierarchy.Notebooks.Notebook.Roser.name
# Loop over each notebook
foreach ($notebook in $Hierarchy.Notebooks.Notebook ) {
$name = ReplaceIllegal -text $notebook.name
if($name -ccontains "Deployments"){
Write-Output $name
<#
Write-Host $notebook.ChildNodes
Write-Host $notebook.Attributes
Write-Host $notebook.BaseURI
Write-Host $notebook.color
Write-Host $notebook.FirstChild
Write-Host $notebook.HasAttributes
Write-Host $notebook.HasChildNodes
Write-Host $notebook.ID
Write-Host $notebook.InnerText
Write-Host $notebook.InnerXml
Write-Host $notebook.IsEmpty
Write-Host $notebook.IsReadOnly
Write-Host $notebook.LastChild
Write-Host $notebook.lastModifiedTime
Write-Host $notebook.LocalName
Write-Host $notebook.name
Write-Host $notebook.NamespaceURI
Write-Host $notebook.NextSibling
Write-Host $notebook.nickname
Write-Host $notebook.NodeType
Write-Host $notebook.OwnerDocument
Write-Host $notebook.OuterXml
Write-Host $notebook.ParentNode
Write-Host $notebook.path
Write-Host $notebook
$nf = Join-Path -Path $folder -ChildPath $name Function ReplaceIllegal {
Write-Host "Notebook: $($nf)" param ( $text )
#> $illegal = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\'
$replaced = $text -replace "[$illegal]",'_'
Write-Host $notebook.InnerXml return $replaced
$destinationFile = Join-Path -Path $folder -ChildPath $name".xml"
write-output $destinationFile
$notebook.InnerXml |Out-File $destinationFile
}else{}
} }
# Loop over each notebook Write-Output $Hierarchy.Notebooks.Notebook.Roser.name
#foreach ($notebook in $Hierarchy.Notebooks.Notebook ) {
# $name = ReplaceIllegal -text $notebook.name function LoopAndExport {
# $nf = Join-Path -Path $folder -ChildPath $name # Loop over each notebook
# Write-Host "Notebook: $($nf)" foreach ($notebook in $Hierarchy.Notebooks.Notebook ) {
# New-Item -Path $nf -ItemType directory | Out-Null $name = ReplaceIllegal -text $notebook.name
# Spider-OneNote-Notebook -onenote $OneNote -node $notebook -path $nf
#} if($name -ccontains "Deployments"){
Write-Output $name
Write-Host $notebook.ID
Write-Host $notebook.InnerXml
$destinationFile = Join-Path -Path $folder -ChildPath $name".test.xml"
write-output $destinationFile
$notebookInnerXml = $notebook.InnerXml
$exportedXML = "<xml>$notebookInnerXml</xml>"
$exportedXML |Out-File $destinationFile
}else{}
}
}
LoopAndExport