Skip to main content

Get-VarOrSave

SYNOPSIS​

Get the value of an environment variable or save it if it does not exist.

SYNTAX​

Get-VarOrSave [-VariableName] <String> [-LazyValue] <ScriptBlock> [[-Validate] <ScriptBlock>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION​

This function will get the value of an environment variable or save it if it does not exist. It will also validate the value if a test script block is provided. If the value does not exist, it will prompt the user for the value and save it as an environment variable, The value will be saved as a process environment variable.

EXAMPLES​

EXAMPLE 1​

Get-VarOrSave `
-VariableName 'HUDU_KEY' `
-LazyValue { Get-UserInput -Title 'Hudu API Key' -Question 'Please enter your Hudu API Key' };

PARAMETERS​

-VariableName​

The name of the environment variable to get or save.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-LazyValue​

The script block to execute if the environment variable does not exist.

Type: ScriptBlock
Parameter Sets: (All)
Aliases:

Required: True
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Validate​

The script block to test the value of the environment variable or the lazy value.

Type: ScriptBlock
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction​

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters​

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS​

OUTPUTS​

System.String if the environment variable exists or the lazy value if it does not.​

null if the value didn't pass the validation.​

NOTES​