functions/public/Get-KlippyMachineInfo.ps1

function Get-KlippyMachineInfo {
    <#
    .SYNOPSIS
        Gets host machine information from a Klipper printer.

    .DESCRIPTION
        Retrieves detailed information about the host machine running
        Klipper/Moonraker including system info, CPU, memory, and network.

    .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-KlippyMachineInfo
        Gets machine info from the default printer.

    .EXAMPLE
        Get-KlippyMachineInfo -PrinterName "voronv2"
        Gets machine info from the specified printer.

    .EXAMPLE
        Get-KlippyPrinter | Get-KlippyMachineInfo | Select-Object PrinterName, Hostname, CpuCount, MemoryTotal
        Gets machine info summary from all printers.

    .OUTPUTS
        PSCustomObject with host machine 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 machine info..."

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

            # Calculate memory in GB
            $memTotalGB = if ($info.CpuInfo.MemoryUnits -eq 'kB' -and $info.CpuInfo.TotalMemory) {
                [Math]::Round($info.CpuInfo.TotalMemory / 1MB, 2)
            } else { $null }

            [PSCustomObject]@{
                PSTypeName       = 'KlippyCLI.MachineInfo'
                PrinterId        = $printer.Id
                PrinterName      = $printer.PrinterName
                Hostname         = $info.Hostname
                OperatingSystem  = $info.Distribution.Name
                OSVersion        = $info.Distribution.Version
                OSId             = $info.Distribution.Id
                KernelVersion    = $info.Distribution.KernelVersion
                CpuModel         = $info.CpuInfo.Model
                CpuCount         = $info.CpuInfo.CpuCount
                CpuBits          = $info.CpuInfo.Bits
                CpuDescription   = $info.CpuInfo.CpuDesc
                MemoryTotal      = $info.CpuInfo.TotalMemory
                MemoryTotalGB    = $memTotalGB
                MemoryUnits      = $info.CpuInfo.MemoryUnits
                PythonVersion    = $info.PythonInfo.Version
                Virtualization   = $info.Virtualization.VirtType
                Network          = $info.Network
                CanBus           = $info.CanBus
                SdInfo           = $info.SdInfo
            }
        }
        catch {
            Write-Error "[$($printer.PrinterName)] Failed to get machine info: $_"
        }
    }
}