80 lines
2.9 KiB
PowerShell
80 lines
2.9 KiB
PowerShell
function Get-DynamicAwsProfilesParameter {
|
|
|
|
<#
|
|
.SYNOPSIS
|
|
Returns a RuntimeDefinedParameterDictionary with all locally configured AWS Profiles
|
|
|
|
.DESCRIPTION
|
|
Returns a RuntimeDefinedParameterDictionary with all locally configured AWS Profiles for use as a DynamicParameter in functions. Does
|
|
not retrieve profiles for any AWSSDK configurations.
|
|
|
|
.PARAMETER DynamicParameterName
|
|
The parameter name to set on the return value. Defaults to ProfileName
|
|
|
|
.PARAMETER ParameterSetName
|
|
The parameter set to associate the dynamic parameter with. Defaults to all parameter sets.
|
|
|
|
.PARAMETER IsMandatoryParameter
|
|
Wheter or not the parameter is mandatory. Defaults to false.
|
|
|
|
.EXAMPLE
|
|
Get-DynamicAwsProfilesParameter
|
|
|
|
Key Value
|
|
--- -----
|
|
ProfileName System.Management.Automation.RuntimeDefinedParameter
|
|
|
|
.EXAMPLE
|
|
|
|
Get-DynamicAwsProfilesParameter -DynamicParameterName "HooDoggie"
|
|
|
|
Key Value
|
|
--- -----
|
|
HooDoggie System.Management.Automation.RuntimeDefinedParameter
|
|
|
|
.LINK
|
|
Get-AwsCredentialConfiguration
|
|
#>
|
|
|
|
[CmdletBinding()]
|
|
[OutputType([System.Management.Automation.RuntimeDefinedParameterDictionary])]
|
|
param(
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$DynamicParameterName = "ProfileName",
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[string]$ParameterSetName = "__AllParameterSets",
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
[switch]$IsMandatoryParameter
|
|
)
|
|
|
|
# Define the Paramater Attributes
|
|
$runtimeParameterDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
|
|
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
|
|
|
|
$parameterAttribute = New-Object -Type 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)
|
|
|
|
# Generate and add the ValidateSet
|
|
# Do not print warnings because this may be loaded/evaluated on servers without any profiles
|
|
$profileNames = Get-AwsCredentialConfiguration -WarningAction SilentlyContinue | Select-Object -ExpandProperty Name
|
|
|
|
# If no profiles are found, set the only available value to NoLocalProfilesFound
|
|
if (Test-IsCollectionNullOrEmpty -Collection $profileNames) {
|
|
|
|
$profileNames = @( "NoLocalProfilesFound" )
|
|
}
|
|
|
|
$validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($profileNames)
|
|
$attributeCollection.Add($validateSetAttribute)
|
|
|
|
# Create the dynamic parameter
|
|
$runtimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($DynamicParameterName, [string], $attributeCollection)
|
|
$runtimeParameterDictionary.Add($DynamicParameterName, $RuntimeParameter)
|
|
return $runtimeParameterDictionary
|
|
} |