functions/public/Stop-KlippyHost.ps1
|
function Stop-KlippyHost { <# .SYNOPSIS Shuts down the Klipper host machine. .DESCRIPTION Performs a system shutdown on the host machine running Klipper/Moonraker. The printer will need to be physically powered back on. .PARAMETER Id The unique identifier of the printer. .PARAMETER PrinterName The friendly name of the printer. .PARAMETER InputObject A printer object from pipeline input. .PARAMETER Force Skip confirmation prompt. .EXAMPLE Stop-KlippyHost Shuts down the default printer's host (with confirmation). .EXAMPLE Stop-KlippyHost -PrinterName "voronv2" -Force Shuts down without confirmation. .NOTES The host machine will need to be physically powered on to recover. .OUTPUTS None. #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] param( [Parameter(ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(ParameterSetName = 'ByName', Position = 0)] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject, [Parameter()] [switch]$Force ) process { # Resolve printer $resolveParams = @{} switch ($PSCmdlet.ParameterSetName) { 'ById' { $resolveParams['Id'] = $Id } 'ByName' { $resolveParams['PrinterName'] = $PrinterName } 'ByObject' { $resolveParams['InputObject'] = $InputObject } } $printer = Resolve-KlippyPrinterTarget @resolveParams if ($Force -or $PSCmdlet.ShouldProcess($printer.PrinterName, "Shutdown Host Machine")) { if (-not $Force) { $confirm = Read-Host "Are you sure you want to shutdown '$($printer.PrinterName)'? (yes/no)" if ($confirm -notin @('yes', 'y')) { Write-Warning "Shutdown cancelled." return } } try { Write-Warning "Shutting down host machine for '$($printer.PrinterName)'..." $null = Invoke-KlippyJsonRpc -Printer $printer -Method "machine/shutdown" Write-Host "Shutdown command sent. The printer will power off." -ForegroundColor Yellow } catch { Write-Error "Failed to shutdown host for '$($printer.PrinterName)': $_" } } } } |