From 62d515fcb3611c1a6191e4183f73e56f418e5b8a Mon Sep 17 00:00:00 2001 From: pi-ux-ce Date: Thu, 22 Jan 2026 10:31:15 +0000 Subject: [PATCH] Update OneNoteXmlExport.ps1 --- OneNoteXmlExport.ps1 | 155 +++++++++---------------------------------- 1 file changed, 32 insertions(+), 123 deletions(-) diff --git a/OneNoteXmlExport.ps1 b/OneNoteXmlExport.ps1 index b9dd270..5e7d502 100644 --- a/OneNoteXmlExport.ps1 +++ b/OneNoteXmlExport.ps1 @@ -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 $folder = "C:\Users\pgeeraert\Downloads\export-onenote" @@ -77,63 +9,40 @@ $OneNote = New-Object -ComObject OneNote.Application [xml]$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 - Write-Host "Notebook: $($nf)" - #> - - Write-Host $notebook.InnerXml - - - $destinationFile = Join-Path -Path $folder -ChildPath $name".xml" - - write-output $destinationFile - - $notebook.InnerXml |Out-File $destinationFile - }else{} - +Function ReplaceIllegal { + param ( $text ) + $illegal = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\' + $replaced = $text -replace "[$illegal]",'_' + return $replaced } -# Loop over each notebook -#foreach ($notebook in $Hierarchy.Notebooks.Notebook ) { -# $name = ReplaceIllegal -text $notebook.name -# $nf = Join-Path -Path $folder -ChildPath $name -# Write-Host "Notebook: $($nf)" -# New-Item -Path $nf -ItemType directory | Out-Null -# Spider-OneNote-Notebook -onenote $OneNote -node $notebook -path $nf -#} +Write-Output $Hierarchy.Notebooks.Notebook.Roser.name + +function LoopAndExport { + # 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.ID + Write-Host $notebook.InnerXml + + + $destinationFile = Join-Path -Path $folder -ChildPath $name".test.xml" + + write-output $destinationFile + + $notebookInnerXml = $notebook.InnerXml + + $exportedXML = "$notebookInnerXml" + + $exportedXML |Out-File $destinationFile + }else{} + + } +} +LoopAndExport \ No newline at end of file