| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a task without starting it. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Returns a new task id after registering the code/file with the Task service. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance. |
| | | 8 | | .PARAMETER Id |
| | | 9 | | Optional task id; if omitted, a new GUID is generated. |
| | | 10 | | .PARAMETER AutoStart |
| | | 11 | | If specified, the task will be started immediately after creation. |
| | | 12 | | .PARAMETER Name |
| | | 13 | | Optional human-friendly name of the task. |
| | | 14 | | .PARAMETER Description |
| | | 15 | | Optional description of the task. |
| | | 16 | | .PARAMETER Options |
| | | 17 | | Language options object; mutually exclusive with other code parameters. |
| | | 18 | | .PARAMETER ScriptBlock |
| | | 19 | | PowerShell script block to run; mutually exclusive with other code parameters. |
| | | 20 | | .PARAMETER Code |
| | | 21 | | Code string to run; mutually exclusive with other code parameters. |
| | | 22 | | .PARAMETER Language |
| | | 23 | | Language of the code string; required when using -Code. |
| | | 24 | | .PARAMETER CodeFilePath |
| | | 25 | | Path to a code file to run; mutually exclusive with other code parameters. |
| | | 26 | | .PARAMETER ExtraImports |
| | | 27 | | Additional namespaces to import; applies to -Code and -CodeFilePath. |
| | | 28 | | .PARAMETER ExtraRefs |
| | | 29 | | Additional assemblies to reference; applies to -Code and -CodeFilePath. |
| | | 30 | | .PARAMETER Arguments |
| | | 31 | | Hashtable of named arguments to pass to the script; applies to -ScriptBlock, -Code, and -CodeFilePath. |
| | | 32 | | .EXAMPLE |
| | | 33 | | New-KrTask -ScriptBlock { param($name) "Hello, $name!" } -Arguments @{ name = 'World' } |
| | | 34 | | |
| | | 35 | | Creates a new PowerShell task that greets the specified name. |
| | | 36 | | .EXAMPLE |
| | | 37 | | New-KrTask -Code 'return 2 + 2' -Language CSharp |
| | | 38 | | Creates a new C# task that returns the result of 2 + 2. |
| | | 39 | | .EXAMPLE |
| | | 40 | | New-KrTask -CodeFilePath 'C:\Scripts\MyScript.ps1' |
| | | 41 | | Creates a new PowerShell task from the specified script file. |
| | | 42 | | .OUTPUTS |
| | | 43 | | Returns the id of the created task. The task is not started; use Start-KrTask to run it. |
| | | 44 | | #> |
| | | 45 | | function New-KrTask { |
| | | 46 | | [KestrunRuntimeApi('Everywhere')] |
| | | 47 | | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')] |
| | | 48 | | [CmdletBinding(DefaultParameterSetName = 'FromCode')] |
| | | 49 | | [OutputType([string])] |
| | | 50 | | param( |
| | | 51 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 52 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 53 | | |
| | | 54 | | [Parameter()] |
| | | 55 | | [string]$Id, |
| | | 56 | | |
| | | 57 | | [Parameter()] |
| | | 58 | | [switch]$AutoStart, |
| | | 59 | | |
| | | 60 | | [Parameter(Mandatory = $false)] |
| | | 61 | | [string]$Name, |
| | | 62 | | |
| | | 63 | | [parameter(Mandatory = $false)] |
| | | 64 | | [string]$Description, |
| | | 65 | | |
| | | 66 | | [Parameter(Mandatory = $true, ParameterSetName = 'Options')] |
| | | 67 | | [Kestrun.Hosting.Options.LanguageOptions]$Options, |
| | | 68 | | |
| | | 69 | | [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')] |
| | | 70 | | [scriptblock]$ScriptBlock, |
| | | 71 | | |
| | | 72 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 73 | | [Alias('CodeBlock')] |
| | | 74 | | [string]$Code, |
| | | 75 | | |
| | | 76 | | [Parameter(Mandatory = $true, ParameterSetName = 'Code')] |
| | | 77 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 78 | | |
| | | 79 | | [Parameter(Mandatory = $true, ParameterSetName = 'CodeFilePath')] |
| | | 80 | | [string]$CodeFilePath, |
| | | 81 | | |
| | | 82 | | [Parameter(ParameterSetName = 'Code')] |
| | | 83 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 84 | | [string[]]$ExtraImports = $null, |
| | | 85 | | |
| | | 86 | | [Parameter(ParameterSetName = 'Code')] |
| | | 87 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 88 | | [System.Reflection.Assembly[]]$ExtraRefs = $null, |
| | | 89 | | |
| | | 90 | | [Parameter(ParameterSetName = 'ScriptBlock')] |
| | | 91 | | [Parameter(ParameterSetName = 'Code')] |
| | | 92 | | [Parameter(ParameterSetName = 'CodeFilePath')] |
| | | 93 | | [hashtable]$Arguments |
| | | 94 | | ) |
| | | 95 | | begin { |
| | 0 | 96 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 97 | | } |
| | | 98 | | process { |
| | 0 | 99 | | if ($PSCmdlet.ParameterSetName -ne 'Options') { |
| | 0 | 100 | | $Options = [Kestrun.Hosting.Options.LanguageOptions]::new() |
| | 0 | 101 | | $Options.ExtraImports = $ExtraImports |
| | 0 | 102 | | $Options.ExtraRefs = $ExtraRefs |
| | 0 | 103 | | if ($null -ne $Arguments) { |
| | 0 | 104 | | $dict = [System.Collections.Generic.Dictionary[string, object]]::new() |
| | 0 | 105 | | foreach ($key in $Arguments.Keys) { |
| | 0 | 106 | | $dict[$key] = $Arguments[$key] |
| | | 107 | | } |
| | 0 | 108 | | $Options.Arguments = $dict |
| | | 109 | | } |
| | 0 | 110 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 111 | | 'ScriptBlock' { |
| | 0 | 112 | | $Options.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | 0 | 113 | | $Options.Code = $ScriptBlock.ToString() |
| | | 114 | | } |
| | | 115 | | 'Code' { |
| | 0 | 116 | | $Options.Language = $Language |
| | 0 | 117 | | $Options.Code = $Code |
| | | 118 | | } |
| | | 119 | | 'CodeFilePath' { |
| | 0 | 120 | | if (-not (Test-Path -Path $CodeFilePath)) { |
| | 0 | 121 | | throw "The specified code file path does not exist: $CodeFilePath" |
| | | 122 | | } |
| | 0 | 123 | | $extension = Split-Path -Path $CodeFilePath -Extension |
| | 0 | 124 | | switch ($extension) { |
| | | 125 | | '.ps1' { |
| | 0 | 126 | | $Options.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell |
| | | 127 | | } |
| | | 128 | | '.cs' { |
| | 0 | 129 | | $Options.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp |
| | | 130 | | } |
| | | 131 | | '.vb' { |
| | 0 | 132 | | $Options.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic |
| | | 133 | | } |
| | | 134 | | default { |
| | 0 | 135 | | throw "Unsupported '$extension' code file extension." |
| | | 136 | | } |
| | | 137 | | } |
| | 0 | 138 | | $Options.Code = Get-Content -Path $CodeFilePath -Raw |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |
| | 0 | 142 | | return $Server.Tasks.Create($id, $Options, $AutoStart.IsPresent, $Name, $Description) |
| | | 143 | | } |
| | | 144 | | } |