ps/Modules/Cole.PowerShell.Developer/Public/Get-DynamicAwsProfilesParameter.ps1

56 lines
2.3 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-DynamicAwsProfilesParameter {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$GeneratedParameterName = "ProfileName",
[Parameter(Mandatory = $false)]
[string]$ParameterSetName = "__AllParameterSets",
[Parameter(Mandatory = $false)]
[switch]$IsMandatoryParameter
)
# Define the Paramater Attributes
$ParameterName = $GeneratedParameterName
$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$ParameterAttribute.Mandatory = $IsMandatoryParameter
$ParameterAttribute.ParameterSetName = $ParameterSetName
$ParameterAttribute.HelpMessage = "The Local AWS Credential Profile Name to Use for Requests"
$AttributeCollection.Add($ParameterAttribute)
$argumentCompleterScriptBlock = {
param ( $commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameters )
$awsEntries = Get-AWSConfigEntries
return $awsEntries.Name | Sort-Object | Get-Unique
}
$argumentCompleterAttribute = [System.Management.Automation.ArgumentCompleterAttribute]::new($argumentCompleterScriptBlock) # constructor requires a ScriptBlock or Type parameter
$AttributeCollection.Add($argumentCompleterAttribute)
# Generate and add the ValidateSet
# Do not print warnings because this may be loaded/evaluated on servers without any profiles
$arrSet = (Get-AWSConfigEntries -WarningAction SilentlyContinue).Name
# If no profiles are found, set the only available value to NoLocalProfilesFound
if (Test-IsCollectionNullOrEmpty $arrSet) {
$arrSet = @( "NoLocalProfilesFound" )
}
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
$AttributeCollection.Add($ValidateSetAttribute)
# Create the dynamic parameter
$RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string[]], $AttributeCollection)
$RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
return $RuntimeParameterDictionary
}