functions/public/Get-KlippyNetworkInfo.ps1

function Get-KlippyNetworkInfo {
    <#
    .SYNOPSIS
        Gets network information from a Klipper printer host.

    .DESCRIPTION
        Retrieves network interface details from the host machine running
        Moonraker, including addresses and link state information.

    .PARAMETER Id
        The unique identifier of the printer.

    .PARAMETER PrinterName
        The friendly name of the printer.

    .PARAMETER InputObject
        A printer object from pipeline input.

    .EXAMPLE
        Get-KlippyNetworkInfo
        Gets network info from the default printer.

    .EXAMPLE
        Get-KlippyNetworkInfo -PrinterName "voronv2"
        Gets network info from the specified printer.

    .EXAMPLE
        Get-KlippyPrinter | Get-KlippyNetworkInfo
        Gets network info from all registered printers.

    .OUTPUTS
        PSCustomObject with network information.
    #>

    [CmdletBinding(DefaultParameterSetName = 'Default')]
    [OutputType([PSCustomObject])]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = 'ById')]
        [ValidateNotNullOrEmpty()]
        [string]$Id,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByName', Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]$PrinterName,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', ValueFromPipeline = $true)]
        [PSCustomObject]$InputObject
    )

    process {
        # Resolve printer
        $resolveParams = @{}
        switch ($PSCmdlet.ParameterSetName) {
            'ById' { $resolveParams['Id'] = $Id }
            'ByName' { $resolveParams['PrinterName'] = $PrinterName }
            'ByObject' { $resolveParams['InputObject'] = $InputObject }
        }

        $printer = Resolve-KlippyPrinterTarget @resolveParams

        try {
            Write-Verbose "[$($printer.PrinterName)] Fetching network info..."

            $sysInfo = Invoke-KlippyJsonRpc -Printer $printer -Method "machine/system_info"
            $network = $sysInfo.SystemInfo.Network

            if (-not $network) {
                Write-Warning "[$($printer.PrinterName)] No network information available"
                return
            }

            # Network is a hashtable/dictionary with interface names as keys
            $interfaceNames = if ($network -is [System.Collections.IDictionary]) {
                $network.Keys
            }
            elseif ($network.PSObject.Properties) {
                $network.PSObject.Properties.Name
            }
            else {
                @()
            }

            foreach ($ifName in $interfaceNames) {
                $iface = if ($network -is [System.Collections.IDictionary]) {
                    $network[$ifName]
                }
                else {
                    $network.$ifName
                }

                [PSCustomObject]@{
                    PSTypeName    = 'KlippyCLI.NetworkInterface'
                    PrinterId     = $printer.Id
                    PrinterName   = $printer.PrinterName
                    InterfaceName = $ifName
                    MacAddress    = $iface.MacAddress ?? $iface.mac_address ?? $iface.Mac ?? $iface.mac
                    IpAddresses   = $iface.IpAddresses ?? $iface.ip_addresses ?? $iface.Addresses ?? $iface.addresses
                }
            }
        }
        catch {
            Write-Error "[$($printer.PrinterName)] Failed to get network info: $_"
        }
    }
}