< Summary - Kestrun — Combined Coverage

Information
Class: Public.Health.Add-KrHealthProbe
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Health/Add-KrHealthProbe.ps1
Tag: Kestrun/Kestrun@2d87023b37eb91155071c91dd3d6a2eeb3004705
Line coverage
0%
Covered lines: 0
Uncovered lines: 44
Coverable lines: 44
Total lines: 161
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/13/2025 - 16:52:37 Line coverage: 0% (0/44) Total lines: 161 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Health/Add-KrHealthProbe.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Registers a script-based health probe with the active Kestrun server.
 4    .DESCRIPTION
 5        Wraps the Kestrun host AddProbe overload that accepts script content. You can provide inline
 6        PowerShell via the -ScriptBlock parameter set, inline code tied to an explicit language, or
 7        the path to a script file. Optional arguments, imports, references, and tag metadata can be
 8        supplied to influence probe execution and filtering.
 9    .PARAMETER Server
 10        The Kestrun host instance to configure. If omitted, the current server context is resolved automatically.
 11    .PARAMETER Name
 12        Unique name for the probe.
 13    .PARAMETER Tags
 14        Optional set of tags used to include or exclude the probe when requests filter by tag.
 15    .PARAMETER ScriptBlock
 16        Inline PowerShell that returns a ProbeResult (or equivalent contract). This is the default parameter set.
 17    .PARAMETER Code
 18        Inline code interpreted in the language supplied via -Language.
 19    .PARAMETER Language
 20        Script language for inline code or script files. When registering a script block the language defaults to PowerS
 21    .PARAMETER CodePath
 22        Path to a script file. The file is read once at registration time.
 23    .PARAMETER Arguments
 24        Hashtable of values exposed to the probe at execution time.
 25    .PARAMETER ExtraImports
 26        Additional language-specific imports (namespaces) supplied to Roslyn-based probes.
 27    .PARAMETER ExtraRefs
 28        Additional assemblies referenced by Roslyn-based probes.
 29    .PARAMETER PassThru
 30        Emits the configured server instance so the call can be chained.
 31    .EXAMPLE
 32        Add-KrHealthProbe -Name SelfCheck -Tags 'core' -ScriptBlock {
 33            return [Kestrun.Health.ProbeResult]::new([Kestrun.Health.ProbeStatus]::Healthy, 'Service ready')
 34        }
 35        Registers a PowerShell health probe named SelfCheck tagged with 'core'.
 36    .EXAMPLE
 37        Add-KrHealthProbe -Name Database -Language CSharp -Code @"
 38            return await ProbeAsync();
 39        "@
 40        Registers an inline C# health probe.
 41    .EXAMPLE
 42        Add-KrHealthProbe -Name Cache -CodePath './Scripts/CacheProbe.cs' -Language CSharp -ExtraImports 'System.Net'
 43        Registers a C# health probe from a script file and adds an extra namespace import.
 44#>
 45function Add-KrHealthProbe {
 46    [KestrunRuntimeApi('Definition')]
 47    [CmdletBinding(DefaultParameterSetName = 'ScriptBlock')]
 48    [OutputType([Kestrun.Hosting.KestrunHost])]
 49    param(
 50        [Parameter(ValueFromPipeline = $true)]
 51        [Kestrun.Hosting.KestrunHost]$Server,
 52
 53        [Parameter(Mandatory = $true)]
 54        [string]$Name,
 55
 56        [string[]]$Tags,
 57
 58        [Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'ScriptBlock')]
 59        [scriptblock]$ScriptBlock,
 60
 61        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 62        [string]$Code,
 63
 64        [Parameter(Mandatory = $true, ParameterSetName = 'Code')]
 65        [Parameter(ParameterSetName = 'File')]
 66        [Kestrun.Scripting.ScriptLanguage]$Language,
 67
 68        [Parameter(Mandatory = $true, ParameterSetName = 'File')]
 69        [string]$CodePath,
 70
 71        [hashtable]$Arguments,
 72
 73        [string[]]$ExtraImports,
 74
 75        [System.Reflection.Assembly[]]$ExtraRefs,
 76
 77        [switch]$PassThru
 78    )
 79    begin {
 080        $Server = Resolve-KestrunServer -Server $Server
 081        if ($null -eq $Server) {
 082            throw 'Server is not initialized. Call New-KrServer first or pipe an existing host instance.'
 83        }
 84    }
 85    process {
 086        $codeToRegister = $null
 087        [Kestrun.Scripting.ScriptLanguage]$effectiveLanguage = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 88
 089        switch ($PSCmdlet.ParameterSetName) {
 90            'ScriptBlock' {
 091                $codeToRegister = $ScriptBlock.ToString()
 092                $effectiveLanguage = [Kestrun.Scripting.ScriptLanguage]::PowerShell
 93            }
 94            'Code' {
 095                $codeToRegister = $Code
 096                $effectiveLanguage = $Language
 97            }
 98            'File' {
 099                if (-not (Test-Path -LiteralPath $CodePath)) {
 0100                    throw "The specified code path '$CodePath' does not exist. Ensure the file exists and the path is co
 101                }
 0102                $codeToRegister = Get-Content -LiteralPath $CodePath -Raw
 0103                if ($PSBoundParameters.ContainsKey('Language')) {
 0104                    $effectiveLanguage = $Language
 105                } else {
 0106                    $extension = [System.IO.Path]::GetExtension($CodePath).ToLowerInvariant()
 0107                    $effectiveLanguage = switch ($extension) {
 0108                        '.ps1' { [Kestrun.Scripting.ScriptLanguage]::PowerShell }
 0109                        '.cs' { [Kestrun.Scripting.ScriptLanguage]::CSharp }
 0110                        '.vb' { [Kestrun.Scripting.ScriptLanguage]::VBNet }
 111                        default {
 0112                            throw "Cannot infer script language from extension '$extension'. Specify -Language explicitl
 113                        }
 114                    }
 115                }
 116            }
 117            default {
 0118                throw "Unsupported parameter set '$($PSCmdlet.ParameterSetName)'."
 119            }
 120        }
 121
 0122        if ([string]::IsNullOrWhiteSpace($codeToRegister)) {
 0123            throw 'Probe code cannot be empty.'
 124        }
 125
 0126        $normalizedTags = $null
 0127        if ($PSBoundParameters.ContainsKey('Tags')) {
 0128            $normalizedTags = @($Tags | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Tri
 0129            if ($normalizedTags.Count -eq 0) {
 0130                $normalizedTags = $null
 131            }
 132        }
 133
 0134        $argDictionary = $null
 0135        if ($PSBoundParameters.ContainsKey('Arguments') -and $Arguments) {
 0136            $argDictionary = [System.Collections.Generic.Dictionary[string, object]]::new()
 0137            foreach ($key in $Arguments.Keys) {
 0138                $argDictionary[$key] = $Arguments[$key]
 139            }
 140        }
 141
 0142        $imports = $null
 0143        if ($PSBoundParameters.ContainsKey('ExtraImports')) {
 0144            $imports = @($ExtraImports | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Tr
 0145            if ($imports.Count -eq 0) {
 0146                $imports = $null
 147            }
 148        }
 149
 150        try {
 0151            $hostResult = $Server.AddProbe($Name, $normalizedTags, $codeToRegister, $effectiveLanguage, $argDictionary, 
 0152            Write-KrLog -Level Information -Message "Health probe '{0}' registered." -Values $Name
 0153            if ($PassThru.IsPresent) {
 0154                return $hostResult
 155            }
 156        } catch {
 0157            Write-KrLog -Level Error -Message "Failed to register health probe '{0}'." -Values $Name -ErrorRecord $_
 158            throw
 159        }
 160    }
 161}

Methods/Properties

Add-KrHealthProbe()