Private/DynamicParameters/Get-JCCommonParameters.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
Function Get-JCCommonParameters
{
    Param(
        [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The RuntimeDefinedParameterDictionary to store variables.')][ValidateNotNullOrEmpty()][System.Management.Automation.RuntimeDefinedParameterDictionary]$RuntimeParameterDictionary = (New-Object -TypeName:([System.Management.Automation.RuntimeDefinedParameterDictionary]))
        , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The type of the object.')][ValidateNotNullOrEmpty()][ValidateSet('command', 'ldap_server', 'policy', 'application', 'radius_server', 'system_group', 'system', 'user_group', 'user', 'g_suite', 'office_365', 'organization')][Alias('TypeNameSingular')][System.String]$Type
        , [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, HelpMessage = 'The verb of the command calling it. Different verbs will make different parameters required.')][ValidateSet('add', 'copy', 'get', 'new', 'remove', 'set')][System.String]$Action
        , [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Bypass user prompts and dynamic ValidateSet.')][ValidateNotNullOrEmpty()][Switch]$Force
    )
    Begin
    {
    }
    Process
    {
        # Get type list
        $JCType = If ($Type)
        {
            Get-JCType -Type:($Type);
        }
        Else
        {
            Get-JCType;
        }
        # Define the new parameters
        $Param_Id = @{
            'Name'                            = 'Id';
            'Type'                            = [System.String[]];
            'Mandatory'                       = $true;
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ById');
            'HelpMessage'                     = 'The unique id of the object.';
            'Alias'                           = $JCType.ById | Where-Object { $_ -ne 'Id' } | Select-Object -Unique;
            'Position'                        = 1;
        }
        $Param_Name = @{
            'Name'                            = 'Name';
            'Type'                            = [System.String[]];
            'Mandatory'                       = $true;
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ByName');
            'HelpMessage'                     = 'The name of the object.';
            'Alias'                           = $JCType.ByName | Where-Object { $_ -ne 'Name' } | Select-Object -Unique;
            'Position'                        = 1;
        }
        $Param_SearchBy = @{
            'Name'                            = 'SearchBy';
            'Type'                            = [System.String];
            'Mandatory'                       = $true;
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ByValue');
            'ValidateSet'                     = @('ById', 'ByName');
            'HelpMessage'                     = 'Specify how you want to search.';
            'DontShow'                        = $true;
            'Position'                        = 1;
        }
        $Param_SearchByValue = @{
            'Name'                            = 'SearchByValue';
            'Type'                            = [System.String[]];
            'Mandatory'                       = $true;
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ByValue');
            'HelpMessage'                     = 'Specify the item which you want to search for. Supports wildcard searches using: *';
            'DontShow'                        = $true;
            'Position'                        = 2;
        }
        $Param_Fields = @{
            'Name'                            = 'Fields';
            'Type'                            = [System.Array];
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ById', 'ByName', 'ByValue', '__AllParameterSets');
            'HelpMessage'                     = 'An array of the fields/properties/columns you want to return from the search.';
            'Position'                        = 95;
        }
        $Param_Filter = @{
            'Name'                            = 'Filter';
            'Type'                            = [System.String];
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateNotNullOrEmpty'          = $true;
            'ParameterSets'                   = @('ById', 'ByName', 'ByValue', '__AllParameterSets');
            'HelpMessage'                     = 'Filters to narrow down search.';
            'ValidateScript'                  = {
                $FilterPattern = [regex]'.*?:.*?:.*?'
                If ($_ -notmatch $FilterPattern)
                {
                    Throw ('Invalid filter "' + $_ + '". Filter must match pattern: {PropertyName}:{Operator}:{Value} (' + $FilterPattern + ')')
                }
                Else
                {
                    $FilterParts = $_ -split ':'
                    $FilterProperties = ((($JCType.ByName, $JCType.ById) + $JCType.SystemInsights.ByName + $JCType.SystemInsights.ById) | Select-Object -Unique)
                    $FilterOperators = $JCType.FilterOperators + $JCType.SystemInsights.FilterOperators | Select-Object -Unique
                    If ($FilterParts[0] -notin $FilterProperties)
                    {
                        Throw ('Invalid filter property provided "' + $FilterParts[0] + '". Accepted filter properties: "' + ($FilterProperties -join ', ') + '"')
                    }
                    If ($FilterParts[1] -notin $FilterOperators)
                    {
                        Throw ('Invalid filter operator provided "' + $FilterParts[1] + '". Accepted filter operators: "' + ($FilterOperators -join ', ') + '"')
                    }
                    $true
                }
            };
            'Position'                        = 96;
        }
        $Param_Limit = @{
            'Name'                            = 'Limit';
            'Type'                            = [System.Int32];
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateRange'                   = (1, [int]::MaxValue);
            'DefaultValue'                    = $JCType.Limit | Select-Object -Unique;
            'HelpMessage'                     = 'The number of items you want to return per API call.';
            'ParameterSets'                   = @('ById', 'ByName', 'ByValue', '__AllParameterSets');
            'Position'                        = 97;
        }
        $Param_Skip = @{
            'Name'                            = 'Skip';
            'Type'                            = [System.Int32];
            'ValueFromPipelineByPropertyName' = $true;
            'ValidateRange'                   = (0, [int]::MaxValue);
            'DefaultValue'                    = $JCType.Skip | Select-Object -Unique;
            'HelpMessage'                     = 'The number of items you want to skip over per API call.';
            'ParameterSets'                   = @('ById', 'ByName', 'ByValue', '__AllParameterSets');
            'Position'                        = 98;
        }
        $Param_Paginate = @{
            'Name'                            = 'Paginate';
            'Type'                            = [System.Boolean];
            'ValueFromPipelineByPropertyName' = $true;
            'DefaultValue'                    = $JCType.Paginate | Select-Object -Unique;
            'ValidateSet'                     = @($true, $false);
            'HelpMessage'                     = 'Whether or not you want to paginate through the results.';
            'ParameterSets'                   = @('ById', 'ByName', 'ByValue', '__AllParameterSets');
            'Position'                        = 99;
        }
        # # Add conditional parameter settings
        # If ($Type -and -not $Force)
        # {
        # # Determine if help files are being built
        # If ((Get-PSCallStack).Command -like '*MarkdownHelp')
        # {
        # $JCObjectCount = 999999
        # }
        # Else
        # {
        # # Get count of JCObject to determine if script should load dynamic parameters
        # $JCObjectCount = (Get-JCObject -Type:($Type) -ReturnCount).totalCount
        # }
        # }
        # If ($Type -and -not $Force -and $JCObjectCount -le 300)
        # {
        # # Populate DefaultValue and ValidateSets
        # $JCObject = Get-JCObject -Type:($Type);
        # $Param_Id.Add('DefaultValue', $JCObject.($JCObject.ById | Select-Object -Unique));
        # $Param_Name.Add('DefaultValue', $JCObject.($JCObject.ByName | Select-Object -Unique));
        # $Param_Id.Add('ValidateSet', @($JCObject.($JCObject.ById | Select-Object -Unique)));
        # $Param_Name.Add('ValidateSet', @($JCObject.($JCObject.ByName | Select-Object -Unique)));
        # If ($JCObjectCount -eq 1)
        # {
        # # Allow Id and Name to use the default value
        # $Param_Id.Add('Mandatory', $false);
        # $Param_Name.Add('Mandatory', $false);
        # }
        # ElseIf ($JCObjectCount -ge 1)
        # {
        # # Don't allow Id and Name to use the default value
        # $Param_Id.Add('Mandatory', $true);
        # $Param_Name.Add('Mandatory', $true);
        # }
        # }
        # Else
        # {
        # $Param_Id.Add('Mandatory', $true);
        # $Param_Name.Add('Mandatory', $true);
        # }
        # Create another parameter set if an id or name is not required
        If ($Action -eq 'get')
        {
            $Param_Fields.ParameterSets += 'Default'
            $Param_Filter.ParameterSets += 'Default'
            $Param_Limit.ParameterSets += 'Default'
            $Param_Skip.ParameterSets += 'Default'
            $Param_Paginate.ParameterSets += 'Default'
        }
        # Build output
        $ParamVarPrefix = 'Param_'
        Get-Variable -Scope:('Local') | Where-Object { $_.Name -like '*' + $ParamVarPrefix + '*' } | Sort-Object { [int]$_.Value.Position } | ForEach-Object {
            # Add RuntimeDictionary to each parameter
            $_.Value.Add('RuntimeParameterDictionary', $RuntimeParameterDictionary)
            # Creating each parameter
            $VarName = $_.Name
            $VarValue = $_.Value
            Try
            {
                If ($Action -in ('add', 'new') -and $_.Name -in ('Param_Name')) # Can only add new objects by name
                {
                    New-DynamicParameter @VarValue | Out-Null
                }
                ElseIf ($Action -in ('remove') -and $_.Name -in ('Param_Id')) # Can only remove objects by id
                {
                    New-DynamicParameter @VarValue | Out-Null
                }
                ElseIf ($Action -in ('set') -and $_.Name -in ('Param_Id', 'Param_Name', 'Param_SearchBy', 'Param_SearchByValue' )) # Can set or update objects by id or name
                {
                    New-DynamicParameter @VarValue | Out-Null
                }
                ElseIf ($Action -eq 'get' -and $_.Name -in ('Param_Id', 'Param_Name', 'Param_SearchBy', 'Param_SearchByValue', 'Param_Fields', 'Param_Filter', 'Param_Limit', 'Param_Skip', 'Param_Paginate'))
                {
                    New-DynamicParameter @VarValue | Out-Null
                }
                ElseIf ($Action -in ('copy') -and $_.Name -in ('Param_Id', 'Param_Name'))
                {
                    New-DynamicParameter @VarValue | Out-Null
                }
            }
            Catch
            {
                Write-Error -Message:('Unable to create dynamic parameter:"' + $VarName.Replace($ParamVarPrefix, '') + '"; Error:' + $Error)
            }
        }
    }
    End
    {
        Return $RuntimeParameterDictionary
    }
}