Public/Update-AtwsFunctions.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 |
<# .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 Update-AtwsFunctions { <# .SYNOPSIS This function recreates the autotask powershell functions. .DESCRIPTION Entities with picklists need customized parameter definitions to get the right validateset attributes. This function regenerates either static functions (those for entities that do not have any picklists or user defined fields) or dynamic functions (any function for an entity with either picklists or user defined fields). .INPUTS A string representing the function set to regenerate, either Static or Dynamic. .OUTPUTS Script files in module directory and the current user's dynamic cache. .EXAMPLE Update-AtwsFunctions -FunctionSet Static Regenerates all static functions in $Module.Modulebase/Static directory .EXAMPLE Update-AtwsFunctions -FunctionSet Dynamic Regenerates all static functions in $Module.Modulebase/Dynamic directory and the current user's dynamic cache directory. .NOTES NAME: Update-AtwsFunctions #> [CmdLetBinding( SupportsShouldProcess = $true, ConfirmImpact = 'High' )] # The function set to generate, either 'Dynamic' or 'Static' Param( [ValidateSet('Dynamic', 'Static')] [string] $FunctionSet = 'Static' ) begin { # Enable modern -Debug behavior if ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) { $DebugPreference = 'Continue' } Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) if (-not($Script:Atws.integrationsValue)) { Throw [ApplicationException] 'Not connected to Autotask WebAPI. Re-import module with valid credentials.' } # Prepare parameters for @splatting $ProgressId = 6 $ProgressParameters = @{ Activity = 'Creating and importing functions for all static Autotask entities (no picklists).' Id = $ProgressId } Write-Verbose -Message ('{0}: Making sure cache is loaded.' -F $MyInvocation.MyCommand.Name) if (-not ($Script:Atws.Cache.Count -gt 0)) { Import-AtwsDiskCache } } process { # Prepare parameters for @splatting $ParentProgressParameters = @{ Activity = 'Going through cache entries.' Id = 6 } # Prepare Index for progressbar $ParentIndex = 0 foreach ($Tenant in $Script:Atws.Cache.GetEnumerator()) { # Calculating progress percentage and displaying it $ParentIndex++ $PercentComplete = $ParentIndex / $Script:Atws.Cache.Count * 100 # Add parameters for @splatting $ParentProgressParameters['PercentComplete'] = $PercentComplete $ParentProgressParameters['Status'] = 'Entity {0}/{1} ({2:n0}%)' -F $ParentIndex, $Script:Atws.Cache.Count, $PercentComplete $ParentProgressParameters['CurrentOperation'] = 'Importing {0}' -F $Entity.Key Write-AtwsProgress @ParentProgressParameters if ($Tenant.Key -eq '00') { $RootPath = $MyInvocation.MyCommand.Module.ModuleBase } elseif ($FunctionSet -eq 'Dynamic' -and $My.ContainsKey('DynamicCache') -and -not $My['IsBeta']) { $RootPath = $Script:Atws.DynamicCache # Create Rootpath directory if it doesn't exist if (-not (Test-Path $RootPath)) { $null = New-Item -Path "$RootPath" -ItemType Directory -Force } } else { # Do not create static functions pr tenant Continue } $Entities = switch ($FunctionSet) { 'Static' { $Tenant.Value.FieldInfoCache.GetEnumerator() | Where-Object { -not $_.Value.HasPickList } } 'Dynamic' { $Tenant.Value.FieldInfoCache.GetEnumerator() | Where-Object { $_.Value.HasPickList } } } # Prepare parameters for @splatting $ProgressParameters = @{ Activity = 'Creating and importing functions for all {0} Autotask entities.' -F $FunctionSet Id = 10 ParentId = 6 } $caption = $MyInvocation.MyCommand.Name $verboseDescription = '{0}: Creating and overwriting {1} functions for {2} entities' -F $caption, $FunctionSet, $Entities.count $verboseWarning = '{0}: About to create and overwrite {1} functions for {2} entities. Do you want to continue?' -F $caption, $FunctionSet, $Entities.count if ($PSCmdlet.ShouldProcess($verboseDescription, $verboseWarning, $caption)) { # Prepare Index for progressbar $Index = 0 Write-Verbose -Message ('{0}: Creating functions for {1} entities.' -F $MyInvocation.MyCommand.Name, $Entities.count) foreach ($CacheEntry in $Entities) { # EntityInfo() $Entity = $CacheEntry.Value.EntityInfo Write-Debug -Message ('{0}: Creating functions for entity {1}' -F $MyInvocation.MyCommand.Name, $Entity.Name) # Calculating progress percentage and displaying it $Index++ $PercentComplete = $Index / $Entities.Count * 100 # Add parameters for @splatting $ProgressParameters['PercentComplete'] = $PercentComplete $ProgressParameters['Status'] = 'Entity {0}/{1} ({2:n0}%)' -F $Index, $Entities.Count, $PercentComplete $ProgressParameters['CurrentOperation'] = 'Importing {0}' -F $Entity.Name Write-AtwsProgress @ProgressParameters $caption = $MyInvocation.MyCommand.Name $verboseDescription = '{0}: Creating and Invoking functions for entity {1}' -F $caption, $Entity.Name $verboseWarning = '{0}: About to create and Invoke functions for entity {1}. Do you want to continue?' -F $caption, $Entity.Name $FunctionDefinition = Get-AtwsFunctionDefinition -Entity $Entity -FieldInfo $CacheEntry.Value.FieldInfo foreach ($Function in $FunctionDefinition.GetEnumerator()) { Write-Debug -Message ('{0}: Writing file for function {1}' -F $MyInvocation.MyCommand.Name, $Function.Key) $FilePath = '{0}\{1}\{2}.ps1' -F $RootPath, $FunctionSet, $Function.Key $verboseDescription = '{0}: Overwriting {1}' -F $caption, $FilePath $verboseWarning = '{0}: About to overwrite {1}. Do you want to continue?' -F $caption, $FilePath if ($PSCmdlet.ShouldProcess($verboseDescription, $verboseWarning, $caption)) { Set-Content -Path $FilePath -Value $Function.Value -Force -Encoding UTF8 } } # foreach $Function } # foreach $Cacheentry } # Shouldprocess } # foreach $TenantS if ($progressParameters['CurrentOperation']) { Write-AtwsProgress @progressParameters -Completed } } # Process end { $caption = $MyInvocation.MyCommand.Name $verboseDescription = '{0}: Overwriting existing module info cache with updated data.' -F $caption $verboseWarning = '{0}: About to overwrite existing module info cache with updated data. This cannot be undone. Do you want to continue?' -F $caption if ($PSCmdlet.ShouldProcess($verboseDescription, $verboseWarning, $caption)) { # Save updated base info for connection to new tenants. $BaseEntityInfo = @{ } $BaseEntityInfo['00'] = $Script:Atws.Cache['00'] $BaseEntityInfoPath = '{0}\Private\AutotaskFieldInfoCache.xml' -F $MyInvocation.MyCommand.Module.ModuleBase $BaseEntityInfo | Export-Clixml -Path $BaseEntityInfoPath -Force Write-Verbose -Message ('{0}: Updated central module fieldinfocache.' -F $MyInvocation.MyCommand.Name) } Write-Debug ('{0}: End of function' -F $MyInvocation.MyCommand.Name) } } |