Source/Get-DotEnvValue.ps1
|
<# .SYNOPSIS Reads a single value from a .env file. .DESCRIPTION Reads the specified .env file via Get-DotEnv and returns the value of the given key, without setting any environment variables. Throws if the file, the key, or the file's format is not valid. .PARAMETER Name The key to read. .PARAMETER Path The path to the .env file. If not specified, reads .env in the current directory. .EXAMPLE Get-DotEnvValue -Name "Foo" -Path ".env" .EXAMPLE "Foo", "Bar" | Get-DotEnvValue -Path ".env" .INPUTS [System.String] The key to read. Accepts pipeline input by value or by property name. .OUTPUTS [System.String] The value of the requested key. #> function Get-DotEnvValue { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Name, [string] $Path = ".env" ) begin { $values = Get-DotEnv -Path $Path } process { if (-not $values.ContainsKey($Name)) { throw "Key '$Name' not found in '$Path'." } return $values[$Name] } } |