Powershell中的动态参数基于其他参数的值
Dynamic parameters in Powershell based on value of other parameter
[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 提示 您输入 -scope
参数,基于参数的 Mandatory
属性,您的代码无法运行,因为 dynamicparam
块在 之前 这样的自动提示运行。
由于您将 默认值 分配给 $scope
,'global'
(与 Mandatory=$true
结合使用没有意义), 一种选择是简单地删除 Mandatory
属性:
如果没有(可能是位置性的)-scope
参数,这将默认为 'global'
,在这种情况下,您知道不需要附加参数。
如果指定参数 ,那么您的 dynamicparam
块将按预期工作。
但是,这也要求您将强制性的$apiIdOrProductId
参数声明放在$scope
之前(交换Position
值4
和 5
)
[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 提示 您输入
-scope
参数,基于参数的Mandatory
属性,您的代码无法运行,因为dynamicparam
块在 之前 这样的自动提示运行。
由于您将 默认值 分配给 $scope
,'global'
(与 Mandatory=$true
结合使用没有意义), 一种选择是简单地删除 Mandatory
属性:
如果没有(可能是位置性的)
-scope
参数,这将默认为'global'
,在这种情况下,您知道不需要附加参数。如果指定参数 ,那么您的
dynamicparam
块将按预期工作。
但是,这也要求您将强制性的$apiIdOrProductId
参数声明放在$scope
之前(交换Position
值4
和 5
)