| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves the value of a previously defined global variable. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Looks up a variable in the Kestrun global variable table and returns its |
| | | 6 | | value. If the variable does not exist, `$null` is returned. |
| | | 7 | | .PARAMETER Server |
| | | 8 | | The Kestrun server instance. |
| | | 9 | | .PARAMETER Global |
| | | 10 | | If specified, the variable is retrieved from the global shared state. |
| | | 11 | | .PARAMETER Name |
| | | 12 | | Name of the variable to retrieve. |
| | | 13 | | This should be the fully qualified name of the variable, including any |
| | | 14 | | namespaces. |
| | | 15 | | .EXAMPLE |
| | | 16 | | Get-KrSharedState -Name "MyVariable" |
| | | 17 | | This retrieves the value of the global variable "MyVariable". |
| | | 18 | | .NOTES |
| | | 19 | | This function is part of the Kestrun.SharedState module and is used to retrieve the value of global variables. |
| | | 20 | | #> |
| | | 21 | | function Get-KrSharedState { |
| | | 22 | | [KestrunRuntimeApi('Everywhere')] |
| | | 23 | | [CmdletBinding(defaultParameterSetName = 'Server')] |
| | | 24 | | param( |
| | | 25 | | [Parameter(ValueFromPipeline = $true, ParameterSetName = 'Server')] |
| | | 26 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 27 | | |
| | | 28 | | [Parameter(Mandatory = $true, ParameterSetName = 'Global')] |
| | | 29 | | [switch]$Global, |
| | | 30 | | |
| | | 31 | | [Parameter(Mandatory)] |
| | | 32 | | [string]$Name |
| | | 33 | | ) |
| | | 34 | | begin { |
| | 1 | 35 | | if (-not $Global.IsPresent) { |
| | | 36 | | # Ensure the server instance is resolved |
| | 0 | 37 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | process { |
| | 1 | 41 | | if ($Global.IsPresent) { |
| | | 42 | | # Retrieve from server instance |
| | 1 | 43 | | return [Kestrun.SharedState.GlobalStore]::Get($Name) |
| | | 44 | | } |
| | | 45 | | # Retrieve (or $null if not defined) |
| | 0 | 46 | | return $Server.SharedState.Get($Name) |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |