Public/Get-OneNoteSection.ps1

#
# .SYNOPSIS
# Retrieves OneNote sections from a specified notebook using the OneNote COM
# API.
#
# .DESCRIPTION
# Accepts notebook objects from the pipeline or a notebook name via the
# notebook name parameter. Optionally filters for a section by name using
# wildcard patterns and case-insensitive matching. If neither notebook objects
# nor notebook name is provided, retrieves all sections across all notebooks.
# Can also retrieve a specific section by ID.
#
# .EXAMPLE
# # Get a specific section by ID.
# Get-OneNoteSection -Id "{12345678-1234-5678-...}{1}{...}"
#
function Get-OneNoteSection {
    [CmdletBinding()]
    param(
        # If specified, retrieves only the currently viewed section in OneNote.
        [Parameter(ParameterSetName = 'Current', Mandatory = $true)]
        [switch]$Current,

        # The ID of a specific section to retrieve.
        [Parameter(ParameterSetName = 'Id', Mandatory = $true)]
        [string]$Id,

        # Name of the notebook to search.
        [Parameter(ParameterSetName = 'ByNotebookName', Position = 0)]
        [string]$NotebookName,

        # Notebook XML element to retrieve sections from. Can be provided via
        # pipeline or directly.
        [Parameter(
            ValueFromPipeline = $true,
            ParameterSetName = 'FromPipeline',
            Mandatory = $true
        )]
        [System.Xml.XmlElement]$Notebook,

        # Name of the section to retrieve, supporting wildcards and prefix
        # matching.
        [Parameter(ParameterSetName = 'ByNotebookName', Position = 1)]
        [Parameter(ParameterSetName = 'FromPipeline')]
        [SupportsWildcards()]
        [string]$Name = "*",

        # The OneNote application object. If not provided, it will be created.
        [Alias('App')]
        [Parameter()]
        $OneNoteApplication = $null
    )

    begin {
        $comObjectCreated = $false
        if (-not $OneNoteApplication) {
            $comObjectCreated = $true
            $OneNoteApplication = New-Object -ComObject OneNote.Application
        }
    }

    process {
        $hsSections = 3 # HierarchyScope.hsSections
        $hsSelf = 0 # HierarchyScope.hsSelf
        $app = $OneNoteApplication
        $sections = @()

        if ($PSCmdlet.ParameterSetName -eq 'Current') {
            # First get the current notebook,
            # then search for current section within it.
            $currentNotebook = Get-OneNoteNotebook -Current -App $app
            $hierarchy = Get-OneNoteHierarchy `
                -Scope $hsSections `
                -StartNodeId $currentNotebook.ID `
                -OneNoteApplication $app

            $sections = @($hierarchy.Notebook.Section |
                Where-Object -Property isCurrentlyViewed -EQ true)

            if ($sections.Count -gt 1) {
                throw "There are currently $($sections.Count) sections that are viewed."
            }
        }
        elseif ($Id) {
            # Get section by ID using hsSelf scope
            $hierarchy = Get-OneNoteHierarchy `
                -Scope $hsSelf `
                -StartNodeId $Id `
                -OneNoteApplication $app

            $sections = @($hierarchy.Section)
        }
        else {
            # Determine which notebooks to retrieve sections from.
            if ($NotebookName) {
                $notebooks = Get-OneNoteNotebook -Name $NotebookName -App $app
            }
            elseif ($Notebook) {
                $notebooks = @($Notebook)
            }
            else {
                # Get all notebooks
                $notebooks = Get-OneNoteNotebook -App $app
            }

            # Fetch sections for each notebook.
            foreach ($nb in $notebooks) {
                $hierarchy = Get-OneNoteHierarchy `
                    -Scope $hsSections `
                    -StartNodeId $nb.ID `
                    -OneNoteApplication $app

                $sections += @($hierarchy.Notebook.Section)
            }

            # Apply name filter.
            $sections = @($sections | Where-Object -Property Name -Like -Value $Name)
        }

        # Tag each section with custom type name.
        $sections | ForEach-Object -Process {
            $_.PSTypeNames.Insert(0, 'OneNote.Section'); $_
        }
    }

    end {
        if ($comObjectCreated) {
            Remove-ComObject -ComObject $OneNoteApplication
        }
    }
}

Get-Command Get-OneNoteSection | Register-ArgumentCompleterMap -Map @{
    NotebookName = { Get-OneNoteNotebookNameCompletion @args }
    Name         = { Get-OneNoteSectionNameCompletion @args }
}