Skip to content

Instantly share code, notes, and snippets.

@espio999
Last active May 27, 2023 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espio999/cd54884cf75c53b06f53b687b95925e4 to your computer and use it in GitHub Desktop.
Save espio999/cd54884cf75c53b06f53b687b95925e4 to your computer and use it in GitHub Desktop.
Print OneNote structure
function printAll($hierarchy, $indent_level){
foreach ($item in $hierarchy.ChildNodes){
printName $item $indent_level $False
}
}
function printName($items, [int]$indent_level, [bool]$printID){
$indent = "`t" * $indent_level
foreach($item in $items){
if ($printID){
$item_id = "`t" + $item.ID
}
else{
$item_id = ""
}
Write-Host ($indent + $item.name + $item_id)
if ($item.HasChildNodes){
$node_depth = countNodeDepth $indent_level
printAll $item $node_depth
}
}
}
function countNodeDepth([int]$n){
return $n + 1
}
$OneNote = New-Object -ComObject OneNote.Application
Add-Type -assembly Microsoft.Office.Interop.OneNote
$OneNoteID = ""
[xml]$Hierarchy = ""
$OneNote.GetHierarchy(
$OneNoteID,
[Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages,
[ref]$Hierarchy,
[Microsoft.Office.InterOp.OneNote.XMLSchema]::xsCurrent)
printAll $Hierarchy 0
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($OneNote) | Out-Null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment