Beefcake
Beefcake

Reputation: 801

PS: Dynamic parameter to be used if existing parameter value is set to a particular value

I would like to make the token parameter required ONLY when the runType parameter is set to download. I believe something like this can be done with dynamicparam, but I'm stuggling to find out how it works. As you can see, I have a basic Write-Error to catch the missing value, but perhaps can anyone give an example/how-to for use of something smart with dynamicparam?

[CmdletBinding()]
param (
    [Parameter(Position=1, Mandatory=$true)]
    [ValidateSet('download', 'update')]
    [string]$runType,

    [Parameter(Position=2)]
    [ValidateNotNullOrEmpty()]
    [string]$token
)


if (($runType -eq "download") -and (!$token)){
    Write-Error "Personal Access Token parameter required: '-token'"
    exit 1
}


$mainDir = (Get-Location).Path
$subDir  = (Get-ChildItem -Path $mainDir | Where-Object { $_.PSIsContainer }).Name


foreach ($dir in $subDir) {
    Write-Host "`n`n **** Directory $dir"

    if ($runType -eq "download") {
        $base64token  = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes(":$token"))
        $headers      = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
        $headers.Add("Authorization", "Basic $base64token")
        $url          = "https://dev.azure.com/ASDF/$dir/_apis/git/repositories?api-version=6.1-preview.1"
        $response     = Invoke-RestMethod $url -Method 'GET' -Headers $headers
        $sortResponse = $response.value | Sort-Object -Property "name"

        foreach ($item in $sortResponse) {
            $name = $item.name
            if (Test-Path        -Path "$mainDir\$dir\$name" -PathType "Container") {
                if (Test-Path    -Path "$mainDir\$dir\$name\.git" -PathType "Container") {
                    Set-Location -Path "$mainDir\$dir\$name"
                    Write-Host "`n`t **** Updating Git $name"
                    Write-Host "$mainDir\$dir\$name"
                    # git pull
                }
            } else {
                Set-Location -Path "$mainDir\$dir"
                Write-Host "`n`t **** Cloning $name"
                Write-Host "$mainDir\$dir\$name"
                $clone = "[email protected]:v3/ASDF/$dir/$name"
                # git clone $clone
            }
        }
    }

    if ($runType -eq "update") {
        $repos = (Get-ChildItem -Path "$mainDir\$dir" | Where-Object { $_.PSIsContainer }).FullName

        foreach ($item in $repos) {
            if (Test-Path -Path "$item\.git" -PathType "Container") {
                Write-Host "`n`t **** Updating Git $($item.split("\")[-1])"
                Set-Location -Path "$item"
                # git pull
            }
        }
    }
}


Set-Location -Path $mainDir

Upvotes: 0

Views: 37

Answers (1)

newone
newone

Reputation: 111

Found that -> Microsoft Docs

Worked for me.

[CmdletBinding()]
Param(
    [Parameter(Position=1, Mandatory=$true)]
    [ValidateSet('download', 'update')]
    [string]$runType
)

DynamicParam
{
    if ($runType -eq 'download')
    {
    $attributes = New-Object -Type System.Management.Automation.ParameterAttribute
    $attributes.ParameterSetName = "token"
    $attributes.Mandatory = $true
    $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
    $attributeCollection.Add($attributes)

    $dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("token", [string],
        $attributeCollection)

    $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
    $paramDictionary.Add("token", $dynParam1)
    return $paramDictionary
    }
}

Upvotes: 1

Related Questions