Neptune443

Dynamic Parameters with [ValidateSet()]

Feb 5th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-Info {
  2.     [cmdletbinding()]
  3.     param(
  4.         [parameter(ParameterSetName="System")]
  5.         [ValidateSet("Server","Desktop","Mainframe")]
  6.         [string]$System
  7.     )
  8.     DynamicParam {
  9.         if($PSBoundParameters["System"] -ne $null) {
  10.             switch($System){
  11.                 "Server" {
  12.                     $OSList = "WS2008R2","WS2012","WS2012R2","RHEL60" #...
  13.                 }
  14.                 "Desktop" {
  15.                     $OSList = "WinXP","Win7","Win8","Win81","Win10"
  16.                 }
  17.                 "Mainframe" {
  18.                     $OSList = "zOS","zVM","zVSE"
  19.                 }
  20.             }
  21.             $validateSetAttribute = new-object ValidateSet -ArgumentList $OSList
  22.             $parameterAttribute = [parameter]::new()
  23.             $parameterAttribute.ParameterSetName = "System"
  24.             $attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
  25.             $attributeCollection.Add($validateSetAttribute)
  26.             $attributeCollection.Add($parameterAttribute)
  27.             $operatingSystemParam = [System.Management.Automation.RuntimeDefinedParameter]::new('OS',[string],$attributeCollection)
  28.             $paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
  29.             $paramDictionary.Add('OS', $operatingSystemParam)
  30.             $paramDictionary
  31.         }
  32.     }
  33.     process {
  34.         "System: $System, OS: $($PSBoundParameters['OS'])"
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment