Private/Get-AtwsParameterDefinition.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
Function Get-AtwsParameterDefinition
{
  [CmdLetBinding()]
  Param
  (   
    [Parameter(Mandatory)]
    [Autotask.EntityInfo]
    $Entity,
        
    [Parameter(Mandatory)]
    [ValidateSet('Get', 'Set', 'New', 'Remove')]
    [String]
    $Verb,
        
    [Parameter(Mandatory)]
    [Autotask.Field[]]
    $FieldInfo
  )
  Begin
  {
      
  }

  Process
  { 
    $TypeName = 'Autotask.{0}' -F $Entity.Name
      
    If ($Verb -eq 'Get')
    {
      Get-AtwsPSParameter -Name 'Filter' -SetName 'Filter' -Type 'String' -Mandatory -Remaining -NotNull  -Array 
      $ReferenceFields = $FieldInfo.Where({$_.IsReference}).Name
      Get-AtwsPSParameter -Name 'GetReferenceEntityById' -Alias 'GetRef' -SetName 'Filter','By_parameters' -Type 'String' -NotNull -ValidateSet $ReferenceFields
    }    
    ElseIf ($Verb -eq 'Set')
    {
      Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull -Array 
      Get-AtwsPSParameter -Name 'PassThru' -SetName 'Input_Object' -Type 'Switch'
    }
    ElseIf ($Verb -in 'New')
    {
      Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull
    }
    ElseIf ($Verb -eq 'Remove')
    {
      Get-AtwsPSParameter -Name 'InputObject' -SetName 'Input_Object' -Type $TypeName -Mandatory -Pipeline -NotNull -Array
      Get-AtwsPSParameter -Name 'Id' -SetName 'By_parameters' -Type $TypeName -Mandatory  -NotNull -Array
    }
    

    Switch ($Verb)
    {
      'Get' 
      { 
        $Fields = $FieldInfo.Where({
            $_.IsQueryable
        }) | ForEach-Object -Process {
          $_.Mandatory = $False
          $_
        }
      }
      'Set' 
      { 
        $Fields = $FieldInfo.Where({
            -Not $_.IsReadOnly
        }) | ForEach-Object -Process {
          $_.ParameterSet = 'Input_Object'
          $_
        }
      }
      'New' 
      {
        $Fields = $FieldInfo.Where({
            $_.Name -ne 'Id'
        })
      }
      default 
      {
        Return
      }

    }
    
    # Add Name alias for EntityName parameters
    $EntityNameParameter = '{0}Name' -f $Entity.Name
    Foreach ($Field in $Fields )
    {
      $Type = Switch ($Field.Type) 
      {
        'Integer' 
        {
          'Int'
        }
        'Short'   
        {
          'Int16'
        }
        default   
        {
          $Field.Type
        }
      }

      # Fieldtype for picklists
      If ($Field.IsPickList -and $Field.PicklistValues.Count -gt 0)
      {
        $Type = 'String'
      }
      
      $Alias = @() 
      If ($Field.Name -eq $EntityNameParameter)
      {
        $Alias += 'Name'
      }

      $ParameterOptions = @{
        Mandatory              = $Field.Mandatory
        ParameterSetName       = $Field.ParameterSet
        ValidateNotNullOrEmpty = $True
        ValidateLength         = $Field.Length
        ValidateSet            = $Field.PickListValues.Label
        Array                  = $(($Verb -eq 'Get'))
        Name                   = $Field.Name
        Alias                  = $Alias
        Type                   = $Type
      }

      Get-AtwsPSParameter @ParameterOptions
    }
    
    
    # Make modifying operators possible
    If ($Verb -eq 'Get')
    {
      # These operators work for all fields (add quote characters here)
      $Labels = $Fields | ForEach-Object -Process {
        $_ = "'{0}'" -F $_
      }
      Foreach ($Operator in 'NotEquals', 'GreaterThan', 'GreaterThanOrEqual', 'LessThan', 'LessThanOrEquals')
      {
        Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'String' -Array -ValidateSet $Labels.Name
      }

      # These operators only work for strings (add quote characters here)
      $Labels = $Fields |
      Where-Object -FilterScript {
        $_.Type -eq 'string'
      } |
      ForEach-Object -Process {
        $_ = "'{0}'" -F $_
      }
      Foreach ($Operator in 'Like', 'NotLike', 'BeginsWith', 'EndsWith', 'Contains')
      {
        Get-AtwsPSParameter -Name $Operator -SetName 'By_parameters' -Type 'String' -Array -ValidateSet $Labels.Name
      }
    }
  }
}