techthoughts2
3/20/2019 - 7:06 PM

PowerShell Parameter Sets

#---------------------------------------------------------------
# Evaluating if a Parameter Set has been used
if ($PSCmdlet.ParameterSetName -eq 'InstanceId') {
    $target = @{Key = 'instanceids'; Values = $managedInstanceId}
}
else {
    $target = @{Key = "tag:$tagName"; Values = $TagValue}
}
#---------------------------------------------------------------
#controlling multiple dependencies of parameter choices example
[Parameter(ParameterSetName = 'ScriptblockTag', Mandatory = $true)]
[Parameter(ParameterSetName = 'ScriptPathTag', Mandatory = $true)]
[ValidateSet('prod', 'prod-braveheart', 'gamma', 'alpha')]
[string]$TagValue,

[Parameter(ParameterSetName = 'ScriptblockInstanceId', Mandatory = $true)]
[Parameter(ParameterSetName = 'ScriptPathInstanceId', Mandatory = $true)]
[string[]]$ManagedInstanceId,

[Parameter(ParameterSetName = 'ScriptblockInstanceId', Mandatory = $true)]
[Parameter(ParameterSetName = 'ScriptPathInstanceId', Mandatory = $true)]
[string]$AWSAccountID,

[Parameter(ParameterSetName = 'ScriptblockTag', Mandatory = $true)]
[Parameter(ParameterSetName = 'ScriptblockInstanceId', Mandatory = $true)]
[string]$Scriptblock,

[Parameter(ParameterSetName = 'ScriptPathTag', Mandatory = $true)]
[Parameter(ParameterSetName = 'ScriptPathInstanceId', Mandatory = $true)]
#---------------------------------------------------------------
#validating a parameter input for a file via a validation script
[ValidateScript( {
        if (-Not ($_ | Test-Path) ) {
            throw "File or folder does not exist"
        }
        if (-Not ($_ | Test-Path -PathType Leaf) ) {
            throw "The Path argument must be a file. Folder paths are not allowed."
        }
        if ($_ -notmatch "(\.ps1)") {
            throw "The file specified in the path argument must be either of type ps1"
        }
        return $true
    })]
[System.IO.FileInfo]$ScriptPath,
#---------------------------------------------------------------