Private/Get-AtwsFunctionDefinition.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
<#
 
.COPYRIGHT
Copyright (c) ECIT Solutions AS. All rights reserved. Licensed under the MIT license.
See https://github.com/ecitsolutions/Autotask/blob/master/LICENSE.md for license information.
 
#>


Function Get-AtwsfunctionDefinition {
     <#
      .SYNOPSIS
 
      .DESCRIPTION
 
      .INPUTS
 
      .OUTPUTS
 
      .EXAMPLE
 
      .NOTES
      NAME:
      .LINK
 
  #>

    [CmdLetBinding()]
    [OutputType([PSObject[]])]
    Param
    (
        [Parameter(Mandatory = $true)]
        [PSObject]
        $Entity,
    
        [Parameter(Mandatory = $true)]
        [Autotask.Field[]]
        $fieldInfo
    )
   
    begin {

        # Hashtable for storing all function properties
        $functionDefinition = @{ }
        
        # A container for all valid verbs for a function
        $verbs = @()

        Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name)

    }

    process { 
    
        # Collect valid verbs based on Autotask.EntityInfo for the current entity
        if ($Entity.CanCreate) {
            $verbs += 'New'
        }
        if ($Entity.CanDelete) {
            $verbs += 'Remove'
        }
        if ($Entity.CanQuery) {
            $verbs += 'Get'
        }
        if ($Entity.CanUpdate) {
            $verbs += 'Set'
        }

    
        # Loop through the valid verbs and generate functions for each of them
        foreach ($verb in $verbs) {
            $functionName = '{0}-Atws{1}' -F $verb, $Entity.Name

            Write-Verbose ('{0}: Creating Function {1}' -F $MyInvocation.MyCommand.Name, $functionName)
    
            $confirmImpact = switch ($verb) {
                'New' { 'Low' }
                'Remove' { 'Low' }
                'Get' { 'None' }
                'Set' { 'Low' }
            }
      
            $defaultParameterSetName = switch ($verb) {
                'New' { 'By_parameters' }
                'Remove' { 'Input_Object' }
                'Get' { 'Filter' }
                'Set' { 'InputObject' }
            }
     
            $atwsFunction = New-Object -TypeName PSObject -Property @{
                FunctionName            = $functionName
                Copyright               = Get-Copyright
                HelpText                = Get-AtwsHelpText -Entity $Entity -verb $verb -FieldInfo $fieldInfo -functionName $functionName
                DefaultParameterSetName = $defaultParameterSetName 
                ConfirmImpact           = $confirmImpact
                Parameters              = Get-AtwsParameterDefinition -Entity $Entity -verb $verb -FieldInfo $fieldInfo
                Definition              = (Get-Command ('{0}-AtwsDefinition' -F $verb)).Definition -replace '#EntityName', $($Entity.Name)
            }
    
            $functionDefinition[$functionName] = Convert-AtwsFunctionToText -AtwsFunction $atwsFunction
   
        }
    }
  
    end { 
        Return $functionDefinition
    }
}