| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Removes a Kestrun server instance. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function stops and destroys a Kestrun server instance with the specified name. |
| | 6 | | .PARAMETER Name |
| | 7 | | The name of the Kestrun server instance to remove. |
| | 8 | | .PARAMETER Force |
| | 9 | | If specified, the server will be stopped and destroyed without confirmation. |
| | 10 | | .EXAMPLE |
| | 11 | | Remove-KrServer -Name "MyKestrunServer" |
| | 12 | | Removes the specified Kestrun server instance. |
| | 13 | | .EXAMPLE |
| | 14 | | Remove-KrServer -Name "MyKestrunServer" -Force |
| | 15 | | Removes the specified Kestrun server instance without confirmation. |
| | 16 | | .NOTES |
| | 17 | | This function is designed to be used in the context of a Kestrun server management. |
| | 18 | | #> |
| | 19 | | function Remove-KrServer { |
| | 20 | | [KestrunRuntimeApi('Definition')] |
| | 21 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | 22 | | [CmdletBinding()] |
| | 23 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | 24 | | param( |
| | 25 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | 26 | | [string]$Name, |
| | 27 | | [Parameter()] |
| | 28 | | [switch]$Force |
| | 29 | | ) |
| | 30 | | process { |
| 0 | 31 | | if ( [Kestrun.KestrunHostManager]::Contains($Name) ) { |
| 0 | 32 | | if ($Force) { |
| 0 | 33 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| 0 | 34 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | 35 | | } |
| 0 | 36 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | 37 | | } else { |
| 0 | 38 | | $confirm = Read-Host "Server '$Name' is running. Do you want to stop and destroy the previous instance? |
| 0 | 39 | | if ($confirm -notin @('Y', 'y')) { |
| 0 | 40 | | Write-Warning 'Operation cancelled by user.' |
| 0 | 41 | | exit 1 |
| | 42 | | } |
| 0 | 43 | | if ([Kestrun.KestrunHostManager]::IsRunning($Name)) { |
| 0 | 44 | | [Kestrun.KestrunHostManager]::Stop($Name) |
| | 45 | | } |
| 0 | 46 | | [Kestrun.KestrunHostManager]::Destroy($Name) |
| | 47 | | } |
| | 48 | | } |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|