PowerShell script/command 将 OneNote 页面名称 + URL 导出到 JSON

PowerShell script/command to export OneNote page names + URLs to JSON

对于所有同步到我的 Win10 PC 的 OneNote 笔记本,我如何使用 PowerShell 生成一个包含对象数组的 .json 文件,其中每个对象包含:

...对于我当前 Windows 用户登录中的每个 OneNote notebook/section/page?

输出如下:

    [
      {
        "notebook_name": "My Notebook name",
        "section_name": "The name of a section",
        "page_name": "The name of a page",
        "page_url": "onenote:https://d.docs.live.net/...etc..."
      },
      { /* ...etc...*/ }
    ]

我对 OP 的等待有点无聊,所以这里是:

$oneNote = New-Object -ComObject OneNote.Application
[xml]$hierarchy = ""
$oneNote.GetHierarchy("", [Microsoft.Office.InterOp.OneNote.HierarchyScope]::hsPages, [ref]$hierarchy)

$result = foreach ($notebook in $hierarchy.Notebooks.Notebook ) {
    foreach ($section in $notebook.Section) {
        $sectionUrl = 'onenote:{0}' -f $section.path
        $section.ChildNodes | ForEach-Object { 
            [PsCustomObject]@{
                'notebook_name' = $notebook.name
                'section_name'  = $section.name
                'page_name'     = $_.name
                'page_url'      = '{0}/pages/{1}' -f $sectionUrl, $_.ID
            }
        }
    }
}

# clean up the COM object
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($oneNote) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

$result | ConvertTo-Json