PoshCode Archive  Artifact [71f0f6dc45]

Artifact 71f0f6dc45a47421fd0a4a43e622832e569e65a50be996756c73be28fa64c2b0:

  • File Dynamic-Parameters-Huh.ps1 — part of check-in [358ded077f] at 2018-06-10 14:15:30 on branch trunk — I can’t get dynamic parameters working when there are multiple parameter sets on the other parameters. as soon as i limit the param sets down to 1, it works. if there are 2, it doesn’t. (user: BattleChicken size: 3175)

# encoding: ascii
# api: powershell
# title: Dynamic Parameters Huh?
# description: I can’t get dynamic parameters working when there are multiple parameter sets on the other parameters. as soon as i limit the param sets down to 1, it works. if there are 2, it doesn’t.
# version: 0.1
# type: function
# author: BattleChicken
# license: CC0
# function: Dynamic-ParamTest
# x-poshcode-id: 6426
# x-archived: 2016-07-01T08:16:21
# x-published: 2016-06-28T17:08:00
#
#
function Dynamic-ParamTest {
    param(
        [parameter(Mandatory=$true)]
        [string]$Hostname,
        
        [parameter()]
        [validateset('Ftp','Scp','Sftp')] # Webdav was added as an option, but not adding it yet since I haven't looked into the requirements and have no way to test it currently.
        [string]$Protocol='Sftp',

        [parameter()]
        [string]$Port = $null, # based on protocol, the session options automatically use the right default port so it is not required.

        [parameter(Mandatory=$true)]
        [string]$Username,
    
        [parameter(Mandatory=$true,ParameterSetName="SecurePass")]
        [string]$PasswordFile,

        [parameter()] #Mandatory=$true -- need to figure out param set, re-add for appropriate conditions later.
        [string]$SshHostKeyFingerprint,

        [parameter()]
        [string]$TlsHostCertificateFingerprint,

        [parameter(ParameterSetName="SecurePass")]
        [switch]$SetSecurePassword,

        [parameter()]
        [string]$SshPrivateKeyPath,

        [parameter()]
        [string]$SshPrivateKeyPassphrase,

        [parameter()]
        [ValidateSet('None','Implicit','ExplicitSsl','Explicit','ExplicitTls')]
        [string]$FtpSecureMode = $null,

        [parameter(Mandatory=$true)]# ,ParameterSetName="PlainTextPass"
        [ValidateNotNullOrEmpty()]
        [string]$PlainTextPassword,
        
        [parameter()]
        [bool]$WebdavSecure,

        [parameter()]
        [string]$WebdavRoot,

        [parameter()]
        [string]$DLLFolder=$Script:WinSCPDLLFolder,

        [parameter()]
        [switch]$GiveUpSecurityAndAcceptAnySshHostKey,

        [parameter()]
        [switch]$GiveUpSecurityAndAcceptAnyTlsHostCertificate
    )
    
    DynamicParam{
        if ($Protocol -match "^Sftp$")
        {
            $attributes = new-object System.Management.Automation.ParameterAttribute
            $attributes.ParameterSetName = "__AllParameterSets"
            $attributes.Mandatory = $false
            $attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)

            $dynParam1 = new-object -Type System.Management.Automation.RuntimeDefinedParameter("IAMDYNAMIC", [Int32], $attributeCollection)
            
            $paramDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add("IAMDYNAMIC", $dynParam1)
            return $paramDictionary
        }
    }

    process{
        write-host "I Ran"
    }



}