Dynamicparam.psm1

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.145
     Generated on: 12/8/2017 11:26 AM
     Generated by: olgab
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2017
#>



<#
    ===========================================================================
     Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.145
     Created on: 12/8/2017 10:48 AM
     Created by: olgab
     Organization:
     Filename: Dynamicparam.psm1
    -------------------------------------------------------------------------
     Module Name: Dynamicparam
    ===========================================================================
#>




function Write-HelloWorld
{
    Write-Host "Hello World"
}

Function Get-Order2 {
[CmdletBinding()]
    Param(
      [Parameter(
         Mandatory=$true,
         Position=1,
         HelpMessage="How many cups would you like to purchase?"
      )]
      [int]$cups,

     [Parameter(
         Mandatory=$false,
         Position=2,
         HelpMessage="What would you like to purchase?"
      )]
      [ValidateSet("Lemonade","Water","Tea","Coffee")]
      [string]$product="Lemonade"
   )

  Process {
      $order = @()
      for ($cup = 1; $cup -le $cups; $cup++) {
          $order += "$($cup): A cup of $($product)"
      }
      $order
   }
}
function Get-Order
{
    [CmdletBinding()]
    Param (
        [Parameter(
                   Mandatory = $true,
                   Position = 1,
                   HelpMessage = "How many cups would you like to purchase?"
                   )]
        [int]$cups,
        [Parameter(
                   Mandatory = $false,
                   Position = 2,
                   HelpMessage = "What would you like to purchase?"
                   )]
        [ValidateSet("Lemonade", "Water", "Tea", "Coffee", "Hard Lemonade")]
        [string]$product = "Lemonade"
    )
    
    DynamicParam
    {
        if ($product -eq "Hard Lemonade")
        {
            #create a new ParameterAttribute Object
            $ageAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ageAttribute.Position = 3
            $ageAttribute.Mandatory = $true
            $ageAttribute.HelpMessage = "This product is only available for customers 21 years of age and older. Please enter your age:"
            
            #create an attributecollection object for the attribute we just created.
            $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]
            
            #add our custom attribute
            $attributeCollection.Add($ageAttribute)
            
            #add our paramater specifying the attribute collection
            $ageParam = New-Object System.Management.Automation.RuntimeDefinedParameter('age', [Int16], $attributeCollection)
            
            #expose the name of our parameter
            $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add('age', $ageParam)
            return $paramDictionary
        }
    }
    
    Process
    {
        $order = @()
        for ($cup = 1; $cup -le $cups; $cup++)
        {
            $order += "$($cup): A cup of $($product)"
        }
        $order
    }
}
Export-ModuleMember -Function Write-HelloWorld, Get-Order