Private/Get-AtwsPSParameter.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
Function Get-AtwsPSParameter {
    [CmdLetBinding()]
    [OutputType([String])]
    Param
    (
        [Parameter(Mandatory = $True)]
        [String]$Name,

        [String[]]$Alias,
    
        [Parameter(Mandatory = $True)]
        [String]$Type,
    
        [Switch]$Mandatory,

        [Alias('Remaining')]
        [Switch]$ValueFromRemainingArguments,

        [Alias('SetName')]
        [String[]]$ParameterSetName,

        [Alias('Pipeline')]
        [Switch]$ValueFromPipeline,

        [Alias('NotNull')]
        [Switch]$ValidateNotNullOrEmpty,

        [String[]]$ValidateSet,

        [Alias('Length')]
        [int]$ValidateLength,

        [String]$Comment,

        [Switch]$Array,
        
        [Switch]$Nullable


          
    )
  
    If ($Comment)
    { [String]$Text = "# {0}`n" -F $Comment }
    Else
    { [String]$Text = '' }
    
    Foreach ($SetName in $ParameterSetName) { 
        # Make an array of properties that goes inside the Parameter clause
        $ParamProperties = @()
        
        # Hardcoded filter against requiring parameters for 'Input_Object'
        If ($Mandatory.IsPresent -and $SetName -in 'By_parameters','Filter') {
            $ParamProperties += " Mandatory = `$true"  
        }
        If ($ValueFromRemainingArguments.IsPresent) {
            $ParamProperties += " ValueFromRemainingArguments = `$true" 
        } 
        If ($ParameterSetName) {
            $ParamProperties += " ParameterSetName = '$SetName'" 
        }
        If ($ValueFromPipeline.IsPresent) {
            $ParamProperties += " ValueFromPipeline = `$true" 
        }

        # Create the [Parameter()] clause
        If ($ParamProperties.Count -gt 0) {
            $Text += " [Parameter(`n"
            $Text += $ParamProperties -join ",`n"
            $Text += "`n )]`n"
        }
    }
    # Add any aliases
    If ($Alias.Count -gt 0) {
        $Text += " [Alias('{0}')]`n" -F $($Alias -join "','")
    }
    # Add validate not null if present
    If ($ValidateNotNullOrEmpty.IsPresent) {
        $Text += " [ValidateNotNullOrEmpty()]`n" 
    }

    # Add validate length if present
    If ($ValidateLength -gt 0) {
        $Text += " [ValidateLength(0,$ValidateLength)]`n" 
    }
        
    # Add Validateset if present
    If ($ValidateSet.Count -gt 0) { 
        # Fix quote characters for labels
        $Labels = Foreach ($Label in  $ValidateSet) {
            If ($Label -match ("['{0}]" -F [Char]8217)) {
                '"{0}"' -F $Label
            }
            Else {
                "'{0}'" -F $Label
            }
        }          
        $Text += " [ValidateSet($($Labels -join ', '))]`n" 
    }

    # Add the correct variable type for the parameter
    $Type = Switch ($Type) 
    {
      'Integer' 
      {
        'Int'
      }
      'Short'   
      {
        'Int16'
      }
      default   
      {
        $Type
      }
    }
    If ($Nullable.IsPresent) {
      $Type = "Nullable[$Type]"
    }
    $Text += " [$Type"
    If ($Array.IsPresent) {
        $Text += '[]'
    }
    $Text += "]`n` `$$Name"
        
    Return $Text
}