gpt4 book ai didi

powershell - 没有 CmdletBinding() 动态参数不起作用

转载 作者:行者123 更新时间:2023-12-04 12:45:52 25 4
gpt4 key购买 nike

该参数似乎根本没有被“设置”为参数。验证集不起作用。自动完成也不行。键入参数名称也不起作用。

我知道我以前做过动态参数。但这一次,我错过了一些东西。只是不知道它是什么。

Function Add-Control() {
DynamicParam {
$ParamAttribute = New-Object Parameter
$ParamAttribute.Mandatory = $true
$ParamAttribute.ParameterSetName = '__AllParameterSets'

$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

$AttributeCollection.Add($ParamAttribute)

$controlTypes = @("TextBox", "Label", "DataGrid")

$AttributeCollection.Add((New-Object ValidateSet($controlTypes)))

$RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Type', [string], $AttributeCollection)

$RuntimeParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

$RuntimeParamDictionary.Add('Type', $RuntimeParam)

return $RuntimeParamDictionary
}

Process {
Write-Host ($PSBoundParameters['Type'])
}
}

Add-Control -Type "Test"
# $null

最佳答案

不确定这是否是一个愚蠢的错误,但我确实有这种感觉。我失踪了

[CmdletBinding()]
Param()

验证集和自动完成现在都可以工作。

希望这对其他人有帮助。

关于powershell - 没有 CmdletBinding() 动态参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49139688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com