Can values that are stored in a PowerShell session variable be used to populate a parameter's default value?
In this example, the session variables are populate the first time the script is run, but aren't used in subsequent executions:
function Get-Authetication
{
[cmdletbinding()]
param(
[parameter(Mandatory=$true)]
[string]$Server = { if ($PSCmdlet.SessionState.PSVariable.Get('Server').Value) { $PSCmdlet.SessionState.PSVariable.Get('Server').Value } },
[parameter(Mandatory=$true)]
[pscredential]$Credential = (Get-Credential)
)
# store
$PSCmdlet.SessionState.PSVariable.Set('Server',$Server)
$PSCmdlet.SessionState.PSVariable.Set('Credential',$Credential)
# return for testing
[PsCustomObject]@{
Server=$PSCmdlet.SessionState.PSVariable.Get('Server').Value;
Username=($PSCmdlet.SessionState.PSVariable.Get('Credential').Value).Username
}
}
Get-Authetication