PS:如果将现有参数值设置为特定值,则要使用的动态参数

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

我想仅当 runType 参数设置为 download 时才需要 token 参数。我相信 dynamicparam 可以完成类似的事情,但我正在努力了解它是如何工作的。如您所见,我有一个基本的 Write-Error 来捕获缺失值,但也许任何人都可以给出一个 example/how-to 来使用 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 = "git@ssh.dev.azure.com: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

发现 -> Microsoft Docs

对我有用。

[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
    }
}