< Summary - Kestrun — Combined Coverage

Information
Class: Public.Tasks.New-KrTask
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Tasks/New-KrTask.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 25
Coverable lines: 25
Total lines: 144
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 10/15/2025 - 01:01:18 Line coverage: 0% (0/25) Total lines: 144 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc5840

Metrics

Method
New-KrTask()

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Tasks/New-KrTask.ps1

#LineLine coverage
 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#>
 45function 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 {
 096        $Server = Resolve-KestrunServer -Server $Server
 97    }
 98    process {
 099        if ($PSCmdlet.ParameterSetName -ne 'Options') {
 0100            $Options = [Kestrun.Hosting.Options.LanguageOptions]::new()
 0101            $Options.ExtraImports = $ExtraImports
 0102            $Options.ExtraRefs = $ExtraRefs
 0103            if ($null -ne $Arguments) {
 0104                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0105                foreach ($key in $Arguments.Keys) {
 0106                    $dict[$key] = $Arguments[$key]
 107                }
 0108                $Options.Arguments = $dict
 109            }
 0110            switch ($PSCmdlet.ParameterSetName) {
 111                'ScriptBlock' {
 0112                    $Options.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 0113                    $Options.Code = $ScriptBlock.ToString()
 114                }
 115                'Code' {
 0116                    $Options.Language = $Language
 0117                    $Options.Code = $Code
 118                }
 119                'CodeFilePath' {
 0120                    if (-not (Test-Path -Path $CodeFilePath)) {
 0121                        throw "The specified code file path does not exist: $CodeFilePath"
 122                    }
 0123                    $extension = Split-Path -Path $CodeFilePath -Extension
 0124                    switch ($extension) {
 125                        '.ps1' {
 0126                            $Options.Language = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 127                        }
 128                        '.cs' {
 0129                            $Options.Language = [Kestrun.Scripting.ScriptLanguage]::CSharp
 130                        }
 131                        '.vb' {
 0132                            $Options.Language = [Kestrun.Scripting.ScriptLanguage]::VisualBasic
 133                        }
 134                        default {
 0135                            throw "Unsupported '$extension' code file extension."
 136                        }
 137                    }
 0138                    $Options.Code = Get-Content -Path $CodeFilePath -Raw
 139                }
 140            }
 141        }
 0142        return $Server.Tasks.Create($id, $Options, $AutoStart.IsPresent, $Name, $Description)
 143    }
 144}

Methods/Properties

New-KrTask()