| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds ad-hoc Tasks support to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Registers the Kestrun Task service on the server, enabling one-off script execution (PowerShell, C#, VB.NET) |
| | | 6 | | with status/result and cancellation support. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance. |
| | | 9 | | .PARAMETER MaxRunspaces |
| | | 10 | | Optional maximum PowerShell runspaces for task execution; falls back to scheduler sizing when omitted. |
| | | 11 | | .PARAMETER PassThru |
| | | 12 | | Returns the server when specified. |
| | | 13 | | .EXAMPLE |
| | | 14 | | Add-KrTasksService |
| | | 15 | | Adds the Tasks service to the default server. |
| | | 16 | | .EXAMPLE |
| | | 17 | | $server = Add-KrTasksService -PassThru |
| | | 18 | | Adds the Tasks service and returns the server instance. |
| | | 19 | | .NOTES |
| | | 20 | | Requires the Kestrun.Hosting.KestrunHost.AddTasks() method. |
| | | 21 | | If the Tasks service is already registered, this cmdlet has no effect. |
| | | 22 | | The Tasks service enables ad-hoc script execution via New-KrTask, Start-KrTask, Stop-KrTask, Remove-KrTask. |
| | | 23 | | The Tasks service uses PowerShell runspaces for PowerShell tasks; the MaxRunspaces parameter controls the maximum nu |
| | | 24 | | #> |
| | | 25 | | function Add-KrTasksService { |
| | | 26 | | [KestrunRuntimeApi('Definition')] |
| | | 27 | | [CmdletBinding()] |
| | | 28 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 29 | | param( |
| | | 30 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 31 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 32 | | |
| | | 33 | | [Parameter()] |
| | | 34 | | [int]$MaxRunspaces, |
| | | 35 | | |
| | | 36 | | [Parameter()] |
| | | 37 | | [switch]$PassThru |
| | | 38 | | ) |
| | | 39 | | begin { |
| | 0 | 40 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 41 | | } |
| | | 42 | | process { |
| | 0 | 43 | | if ($PSBoundParameters.ContainsKey('MaxRunspaces')) { |
| | 0 | 44 | | $Server.AddTasks($MaxRunspaces) | Out-Null |
| | | 45 | | } else { |
| | 0 | 46 | | $Server.AddTasks() | Out-Null |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | if ($PassThru) { return $Server } |
| | | 50 | | } |
| | | 51 | | } |