在 PowerShell 中使用动态参数值 [英] Using dynamic parameters value in PowerShell

查看:115
本文介绍了在 PowerShell 中使用动态参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use a dynamic parameter in PowerShell, but the value for the parameter doesn't seem to exist after I have run through my script.

[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] 时,该参数不存在.我是如何获得价值以便我可以使用它的?

I want the script to ask for a [VersionDocumentID] if the boolean value for [PullVersionDoc] is TRUE to use later on in the script, but when I write out the [$PSBoundParameters] the parameter doesn't exist. How did I get the value so that I can use it?

推荐答案

这是我尝试使用动态参数获取 Configuration Manager 日志的方法.

Here's how I tried using dynamic parameters to get Configuration Manager logs.

归功于 此处为博文.

用法:Get-CCMLogs -ComputerNames -Remote -RemoteLogName

本地使用:Get-CCMLogs -ComputerNames -LocalLogName

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

The dynamic parameter will return a remote log name if the switch -Remote is entered or return a local log name if the switch -Remote is not entered.

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
    }
}

这篇关于在 PowerShell 中使用动态参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆