Powershell 提示输入时DynamicRAM不工作
我试图让我的powershell脚本提示输入,但仍然使用DynamicRAM。如果我通过命令行传递参数(例如:Powershell 提示输入时DynamicRAM不工作,powershell,powershell-3.0,Powershell,Powershell 3.0,我试图让我的powershell脚本提示输入,但仍然使用DynamicRAM。如果我通过命令行传递参数(例如:DeployBuild“DEV02”“ClientPortal”“a”“b”),那么下面的代码工作正常,并且会提示我输入动态参数。如果我选择不通过命令行传递参数(例如:DeployBuild),而是让脚本提示输入,那么动态参数将停止工作。有人知道为什么这不起作用吗 Function DeployBuild { [CmdletBinding()] Param (
DeployBuild“DEV02”“ClientPortal”“a”“b”
),那么下面的代码工作正常,并且会提示我输入动态参数。如果我选择不通过命令行传递参数(例如:DeployBuild
),而是让脚本提示输入,那么动态参数将停止工作。有人知道为什么这不起作用吗
Function DeployBuild {
[CmdletBinding()]
Param
(
[ValidateSet("DEV01","DEV02","PEDEV01","QA01","QA02","UAT","PERF01","PROD")]
[Parameter(Mandatory=$true, Position=1)]
[String]$environment,
[Parameter(Mandatory=$true, Position=2)]
[ValidateSet("AuthenticationService","ClientPortal")]
[String]$application,
[Parameter(Mandatory=$true, Position=3)]
[String]$buildName,
[Parameter(Mandatory=$true, Position=4)]
[String]$buildNumber
)
DynamicParam{
if ($environment -eq "DEV02"){
#create a new ParameterAttribute Object
$buildVersionAttribute = New-Object System.Management.Automation.ParameterAttribute
$buildVersionAttribute.Position = 5
$buildVersionAttribute.Mandatory = $true
#create an attributecollection object for the attribute just created.
$attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
#add our custom attribute
$attributeCollection.Add($buildVersionAttribute)
#add our paramater specifying the attribute collection
$buildVersionParam = New-Object System.Management.Automation.RuntimeDefinedParameter('buildVersion', [double], $attributeCollection)
#expose the name of our parameter
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add('buildVersion', $buildVersionParam)
return $paramDictionary
}
}
End{
Write-Host $environment
Write-Host $application
Write-Host $buildName
Write-Host $buildNumber
Write-Host $PSBoundParameters.buildVersion
}
}
这已经很旧了,所以你可能不再需要它了,但我很确定发生这种情况的原因是因为
DynamicParam
块中的条件,它查看$environment
的值是什么
当计算dynamicRAM
块时,$environment
为空,因此永远不会添加参数
您可以这样为自己演示:
DynamicParam {
Write-Verbose "DynamicParam here, environment is: '$environment'" -Verbose
# ... rest of code here
}
现在不带任何参数运行DeployBuild
,您将准确地看到何时对块进行求值