2012-06-13 125 views

回答

1

System.Management.Automation.RuntimeDefinedParameter有“价值”的财产,所以我想我会用它来设置默认值。它有些作品(当我调试脚本时,我可以使用$ PSCmdlet.GetDynamicParameters()看到这个'默认'值),但是我没有在实际函数中访问它(它在$ pscmdlet.GetDynamicParameters()调用中执行'死亡'), 。

无论如何:当我指定值时,我的函数显示的是绑定值而不是默认值。

不知道它是否有帮助,并且TBH当参数为动态时,几乎看不到任何用例的默认值。很想看看你为什么需要它。 :)

+0

我需要的默认值,使参数是必需的。像'函数f($ p = @(抛出p是必需的)){}' – Jack128

+0

'强制'不是'比'更好的选择吗? – BartekB

+0

编号强制显示对话框(或命令行提示符),但我的脚本必须在没有人为操作的情况下工作 – Jack128

0

这不正是你所需要的,但至少一个很好的解决方法(需要的PowerShell 3.0):

您可以使用$PSDefaultParameterValues的所有功能,所有参数设置的默认值。请参阅https://technet.microsoft.com/en-us/library/hh847819.aspx或在about_Parameters_Default_Values上使用PowerShell帮助。

0

作为鲍尔泰克建议,可以使用Value属性如下

DynamicParam { 
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    $colorlist = [System.Enum]::GetNames([System.Drawing.KnownColor]) 

    $attributes = New-Object System.Management.Automation.ParameterAttribute 
    $attributes.ParameterSetName = "__AllParameterSets" 
    $attributes.Mandatory = $false 

    # Background color 
    $validationset = New-Object -Type System.Management.Automation.ValidateSetAttribute -ArgumentList $colorlist 
    $collection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] 
    $collection.Add($attributes) 
    $collection.Add($validationset) 
    $background = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Background", [String], $collection) 
    $background.Value = "Transparent" 

    $newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary 
    $newparams.Add("Background", $background) 

return $newparams 
} 

这里重要的线在代码看到的是$ background.Value =“透明”,其中$背景是RunTimeDefinedParameter。

对于那些好奇。我最初尝试将其用作属性,但ParameterAttributes中没有.Value可用。

1

Chrissy的例子可能是正确的做法,但我无法检索到默认值。当指定默认值时,该参数不存在于$ PSBoundParameters中。

我们应用的“解决方法”是将$ PSBoundParameter [“Background”]绑定到我们想要的默认值。 $PSBoundParameters["Background"] = "Transparent"

扩展克里希的例子:

DynamicParam { 
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    $colorlist = [System.Enum]::GetNames([System.Drawing.KnownColor]) 

    $attributes = New-Object System.Management.Automation.ParameterAttribute 
    $attributes.ParameterSetName = "__AllParameterSets" 
    $attributes.Mandatory = $false 

    # Background color 
    $validationset = New-Object -Type System.Management.Automation.ValidateSetAttribute -ArgumentList $colorlist 
    $collection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] 
    $collection.Add($attributes) 
    $collection.Add($validationset) 
    $background = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Background", [String], $collection) 
    $PSBoundParameters["Background"] = "Transparent" 

    $newparams = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary 
    $newparams.Add("Background", $background) 

    return $newparams 
} 
+0

这是我能够自己拥有默认值的动态参数的唯一方法。谢谢,托马斯! –

相关问题