public/Restart-UnraidApi.ps1
|
function Restart-UnraidApi { <# .SYNOPSIS Restarts the Unraid API service via SSH. .DESCRIPTION Uses the native Windows 'ssh.exe' to execute the 'unraid-api restart' command. It seems to get out of sync sometimes, and a svc restart fixes this. Runs synchronously to ensure password prompts appear correctly if keys aren't set up. .PARAMETER ComputerName Unraid IP or Hostname. .PARAMETER UserName SSH User (default: root). .EXAMPLE Restart-UnraidApi -ComputerName "192.168.1.10" #> [CmdletBinding()] param( [Parameter(Mandatory, Position = 0)] [string]$ComputerName, [Parameter()] [string]$UserName = "root" ) process { if (!(Get-Command ssh.exe -ErrorAction SilentlyContinue)) { Write-Error "SSH client (ssh.exe) missing. This requires Windows 10/11 or OpenSSH." return } # ISE handles console streams differently- SSH.exe password prompt does not appear. if ($psISE -or $Host.Name -match "ISE") { Write-Error "SSH.exe fails in PowerShell ISE. Please try again using a non-ISE terminal (PowerShell.exe, Windows Terminal)." return } Write-Information "Connecting to $ComputerName..." $remoteCommand = "unraid-api restart" try { & ssh.exe -l $UserName $ComputerName $remoteCommand if ($LASTEXITCODE -eq 0) { Write-Information "Service restart complete." } else { Write-Warning "SSH exited with error code $LASTEXITCODE." } } catch { Write-Error "SSH execution failed: $_" } } } |