Powershell中的动态参数基于其他参数的值


[CmdletBinding()]
param (
[Parameter(Mandatory=$true,Position=0)]
[string]
$subscription,
[Parameter(Mandatory=$false)]
[string]
$applicationId,
[Parameter(Mandatory=$false)]
[string]
$clientSecret,
[Parameter(Mandatory=$true,Position=1)]
[string]
$resourceGroupName,
[Parameter(Mandatory=$true,Position=2)]
[string]
$apimServiceName,
[Parameter(Mandatory=$true,Position=3)]
[string]
$policyfilePath,
[Parameter(Mandatory=$true,Position=4)]
[ValidateSet('global','product','api','operation', IgnoreCase = $true)]
[string]
$scope='global',

[Parameter(Mandatory=$true,Position=5)]
[string]
$apiIdOrProductId
#THIS IS THE PARAMETER WHICH I WANT TO MAKE MANDATORY IF VALUE OF PREVIOUS PARAMETER $scope = "OPEARTION"
#[Parameter(Mandatory=$false)]
#[string]
#$operationname
)
DynamicParam {
if ($scope -eq "OPERATION" -or $scope -eq "operation") {
#create a new ParameterAttribute Object
Write-Host "Called test"
Write-Host $scope
$operationAttribute = New-Object System.Management.Automation.ParameterAttribute
$operationAttribute.Mandatory = $true
$operationAttribute.Position = 6

#create an attributecollection object for the attribute we just created.
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attribute
$attributeCollection.Add($operationAttribute)
#add our paramater specifying the attribute collection
$operationName = New-Object System.Management.Automation.RuntimeDefinedParameter('operationname', [String], $attributeCollection)
#expose the name of our parameter
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('operationname', $operationName)
return $paramDictionary
}
}

我无法使依赖于先前参数"的动态参数工作;"范围";。它只是在我运行PS脚本时跳过。请告诉我我做错了什么?

  • 只有在命令行上直接向-scope参数传递参数时,代码才有效

  • 如果让PowerShell根据参数的Mandatory属性提示您输入-scope参数,则代码无法工作,因为dynamicparam块在此类自动提示之前运行

由于要将默认值分配给$scope'global'(与Mandatory=$true结合使用没有意义(,因此一个选项是简单地删除Mandatory属性:

  • 在没有(可能是位置(-scope参数的情况下,这将默认为'global',在这种情况下,您知道不需要额外的参数。

  • 如果指定了参数,则dynamicparam块将按预期工作。

但是,这也要求您在$scope之前放置强制性的$apiIdOrProductId参数声明(交换Position45(

根据标题,至少有一种方法可以让动态参数根据之前输入的常规参数的值为该动态参数提供ValidateSet。

在这种情况下,常规参数可以是文件夹。但你必须在没有引号的情况下输入。

例如:

Function Get-SubFolderDynamically {
[CmdletBinding()]
param($path)

dynamicparam
{
Write-Verbose "DynamicParam triggered. PSBoundParameters `$path is currently: $($PSBoundParameters['path'])" -Verbose

#Parameter Definition
$attributes = New-Object -Type System.Management.Automation.ParameterAttribute
$attributes.Mandatory = $true
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
#AcceptedValues for Parameter Definition
#Note: Only Works if path is entered in the command line without quotes
$subFolders = (Get-ChildItem $path).Name
$attributeCollection.add((New-Object System.Management.Automation.ValidateSetAttribute($subFolders)))
#ParameterDictionary
$dynParam = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("subFolder", [string], $attributeCollection)
$paramDictionary = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("subFolder", $dynParam)

return $paramDictionary
}
}

#RUNIT-创建一个包含要测试的子文件夹的文件夹。

Get-SubFolderDynamically -path C:PathWithoutQuotes -subFolder [TAB|TAB|...]