Using dynamic parameters value in PowerShell

后端 未结 1 384
感情败类
感情败类 2020-12-20 00:50

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.

[CmdletBindi         


        
相关标签:
1条回答
  • 2020-12-20 01:31

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

    Credit goes to the blog post here.

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

    Local usage: Get-CCMLogs -ComputerNames -LocalLogName <Tab to complete lognames>

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题