functions/public/Restart-KlippyService.ps1
|
function Restart-KlippyService { <# .SYNOPSIS Restarts a service on a Klipper printer's host machine. .DESCRIPTION Restarts a system service on the printer's host machine. Requires the -Force parameter to confirm the potentially disruptive action. Common services include: klipper, moonraker, crowsnest, etc. .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 ServiceName The name of the service to restart. .PARAMETER Force Required parameter to confirm the restart action. .EXAMPLE Restart-KlippyService -ServiceName "klipper" -Force Restarts the Klipper service on the default printer. .EXAMPLE Restart-KlippyService -PrinterName "voronv2" -ServiceName "moonraker" -Force Restarts Moonraker on the specified printer. .EXAMPLE Get-KlippyPrinter | Restart-KlippyService -ServiceName "klipper" -Force Restarts Klipper on all registered printers. .OUTPUTS PSCustomObject with restart result. #> [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'Default')] [OutputType([PSCustomObject])] param( [Parameter(Mandatory = $true, ParameterSetName = 'ById')] [ValidateNotNullOrEmpty()] [string]$Id, [Parameter(Mandatory = $true, ParameterSetName = 'ByName')] [ValidateNotNullOrEmpty()] [string]$PrinterName, [Parameter(Mandatory = $true, ParameterSetName = 'ByObject', ValueFromPipeline = $true)] [PSCustomObject]$InputObject, [Parameter(Mandatory = $true, Position = 0)] [ValidateNotNullOrEmpty()] [string]$ServiceName, [Parameter(Mandatory = $true)] [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 (-not $Force) { Write-Error "[$($printer.PrinterName)] The -Force parameter is required to restart services." return } if ($PSCmdlet.ShouldProcess("$($printer.PrinterName):$ServiceName", "Restart service")) { try { Write-Verbose "[$($printer.PrinterName)] Restarting service: $ServiceName" $null = Invoke-KlippyJsonRpc -Printer $printer -Method "machine/services/restart" -Params @{ service = $ServiceName } Write-Verbose "[$($printer.PrinterName)] Service restart initiated: $ServiceName" # Brief wait to allow service to start restarting Start-Sleep -Milliseconds 500 # Try to get updated status $newStatus = $null try { $statusResult = Get-KlippyServiceStatus @resolveParams -ServiceName $ServiceName $newStatus = $statusResult.ActiveState } catch { Write-Verbose "Could not retrieve updated status: $_" } return [PSCustomObject]@{ PSTypeName = 'KlippyCLI.ServiceRestart' PrinterId = $printer.Id PrinterName = $printer.PrinterName ServiceName = $ServiceName Action = 'Restart' Success = $true NewState = $newStatus } } catch { Write-Error "[$($printer.PrinterName)] Failed to restart service '$ServiceName': $_" return [PSCustomObject]@{ PSTypeName = 'KlippyCLI.ServiceRestart' PrinterId = $printer.Id PrinterName = $printer.PrinterName ServiceName = $ServiceName Action = 'Restart' Success = $false Error = $_.Exception.Message } } } } } |