在PowerShell中使用动态参数值

时间:2017-02-18 17:33:35

标签: powershell

我正在尝试在PowerShell中使用动态参数,但在运行我的脚本后,参数的值似乎不存在。

[CmdletBinding()]
Param (
    [Parameter(
         Mandatory=$true,
         Position=0,
         HelpMessage = "The entity ID for the Version"
    )]
    [string]$TPVersionID,

    [Parameter(
         Mandatory=$true,
         Position=1,
         HelpMessage = ""
    )]
    [string]$VersionNumber,

    [Parameter(
         Mandatory=$true,
         Position=2,
         HelpMessage = "This is a boolean value; enter any value to make it True, leave it blank to make it False."
    )]
    [bool]$PullVersionDoc
)


function Get-VersionParam{
    [CmdletBinding()]
    Param ([string]$TPVersionID, [string]$VersionNumber, [bool]$PullVersionDoc?)
    DynamicParam {
        if ($PullVersionDoc) {
            write-host("HEY!")

            $attributes = new-object System.Management.Automation.ParameterAttribute
            $attributes.Position = 3
            $attributes.Mandatory = $true
            $attributeCollection = new-object `
                -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)

            $dynParam1 = new-object `
                -Type System.Management.Automation.RuntimeDefinedParameter('VersionDocumentID', [Int32], $attributeCollection)

            $paramDictionary = new-object `
                -Type System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add('VersionDocumentID', $dynParam1)
            return $paramDictionary
        }
    }
}


Get-VersionParam


#Write-Host "Dynamic Parameter PullVersionDoc? = " $PullVersionDoc
Write-Host $PSBoundParameters

如果[PullVersionDoc]的布尔值为TRUE,稍后在脚本中使用,我希望脚本要求[VersionDocumentID],但是当我写出[$ PSBoundParameters]时,参数不存在。我如何获得价值以便我可以使用它?

1 个答案:

答案 0 :(得分:4)

以下是我尝试使用动态参数获取Configuration Manager日志的方法。

积分转到blog post here

用法:Get-CCMLogs -ComputerNames -Remote -RemoteLogName <Tab to complete lognames>

本地使用:Get-CCMLogs -ComputerNames -LocalLogName <Tab to complete lognames>

如果输入了开关-Remote,则动态参数将返回远程日志名称;如果未输入开关-Remote,则返回本地日志名称。

Function Get-CCMLogs {

    [CmdletBinding()]

    Param(
        [Parameter(Mandatory=$false,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            HelpMessage="Give me a list of computer names!")]
        [Alias('Hostname','cn')]
        [string[]]$ComputerNames = $env:COMPUTERNAME,

        [switch]$Remote
    )

    DynamicParam{

        If ($Remote) {

            $ParameterName = 'RemoteLogName'

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

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

            $ParamAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParamAttribute.Mandatory = $true
            $ParamAttribute.Position = 1

            $AttributeCollection.Add($ParamAttribute)

            $ValidateItems = Get-ChildItem -Path "\\$ComputerNames\C$\Windows\CCM\Logs" | Where {$_ -notmatch '\d+'} | Select -ExpandProperty FullName
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ValidateItems)

            $AttributeCollection.Add($ValidateSetAttribute)

            $RunTimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

            $RunTimeDictionary.Add($ParameterName, $RunTimeParam)

            Return $RunTimeDictionary
        }
        else {
            $ParameterName = 'LocalLogName'

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

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

            $ParamAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParamAttribute.Mandatory = $true
            $ParamAttribute.Position = 1

            $AttributeCollection.Add($ParamAttribute)

            $ValidateItems = Get-ChildItem -Path C:\Windows\CCM\Logs | Select -ExpandProperty FullName | Where {$_ -notmatch '\d+'}
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ValidateItems)

            $AttributeCollection.Add($ValidateSetAttribute)

            $RunTimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

            $RunTimeDictionary.Add($ParameterName, $RunTimeParam)

            Return $RunTimeDictionary
        }
    }

    Begin{
        $LogName = $PSBoundParameters[$ParameterName]
    }

    Process{
        cmtrace.exe $LogName
    }
}