通过反射获取cmdlet的动态参数

时间:2011-03-14 21:32:37

标签: reflection powershell system.reflection cmdlets

Powershell根据上下文公开了一些参数“dynamic parameters”。 MSDN页面很好地解释了这个机制,但是为了找出这些机制,必须调用GetDynamicParameters(),它返回一个包含附加参数的类。我需要通过反射来获取这些参数,并且(这里是它的关键),在ReflectionOnly上下文中(也就是说,类型是用ReflectionOnlyLoadFrom加载的)。所以,没有Assembly.InvokeMember(“GetDynamicParameters”)。

可以这样做吗?

2 个答案:

答案 0 :(得分:2)

没有。反射适用于静态程序集元数据。 powershell中的动态参数由命令或函数本身添加到 runtime

答案 1 :(得分:1)

也许这会有所帮助:

1:定义动态参数

#===================================================================================
# DEFINITION OF FREE FIELDS USED BY THE CUSTOMER
#-----------------------------------------------------------------------------------
# SYNTAX: @{ <FF-Name>=@(<FF-Number>,<isMandatory_CREATE>,<isMandatory_UPDATE>); }
$usedFFs = @{               
                      "defaultSMTP"=@(2,1,0); `
                      "allowedSMTP"=@(3,1,0); `
                       "secondName"=@(100,1,0); `
                            "orgID"=@(30001,1,0); `
            "allowedSubjectTypeIDs"=@(30002,1,0); `
            }

# FF-HelpMessage for input          
$usedFFs_HelpMSG = @{ 2="the default smtp domain used by the organizaiton. Sampel:'algacom.ch'"; `
                      3="comma seperated list of allowed smtp domains. Sampel:'algacom.ch,basel.algacom.ch'"; `
                      100="an additional organization name. Sampel:'algaCom AG')"; `
                      30001="an unique ID (integer) identifying the organization entry"; `
                      30002="comma seperated list of allowed subject types. Sampel:'1,2,1003,10040'"; `
            }

2:构建动态参数的函数定义

#-------------------------------------------------------------------------------------------------------        
# Build-DynParams : Used to build the dynamic input parameters based on $usedFFs / $usedFFs_HelpMSG
#-------------------------------------------------------------------------------------------------------
function Build-DynParams($type) {
    $paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary

    foreach($ffName in $usedFFs.Keys) {
        $ffID = $usedFFs.Item($ffName)[0]
        $dynAttribCol = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
        $dynAttrib = New-Object System.Management.Automation.ParameterAttribute
        $dynAttrib.ParameterSetName = "__AllParameterSets"
        $dynAttrib.HelpMessage = $usedFFs_HelpMSG.Item($ffID)
        switch($type) {
            "CREATE" { $dynAttrib.Mandatory = [bool]($usedFFs.Item($ffName)[1]) }
            "UPDATE" { $dynAttrib.Mandatory = [bool]($usedFFs.Item($ffName)[2]) }
        }   
        $dynAttribCol.Add($dynAttrib)   
        $dynParam = New-Object -Type System.Management.Automation.RuntimeDefinedParameter($ffName, [string], $dynAttribCol)
        $paramDictionary.Add($ffName, $dynParam)        
    }
    return $paramDictionary         
}

3。利用动态参数的函数

#-------------------------------------------------------------------------------------------------------
# aAPS-OrganizationAdd : This will add a new organization entry
#-------------------------------------------------------------------------------------------------------
Function aAPS-OrganizationAdd {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,HelpMessage="The name of the new organization")] 
        [String]$Descr,
        [Parameter(Mandatory=$false,HelpMessage="The name of the parent organization")] 
        [String]$ParentDescr=$null,
        [Parameter(Mandatory=$false,HelpMessage="The status of the new organization [1=Active|2=Inactive]")] 
        [int]$Status = 1,
        [Parameter(Mandatory=$false,HelpMessage="If you want to see the data of the deactivated object")] 
        [switch]$ShowResult
    )
    DynamicParam { Build-DynParams "CREATE" }

    Begin {}

    Process {
        # do what oyu want here
    }

    End {}
}