Private/Import-AtwsCmdLet.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

Function Import-AtwsCmdLet
{
  [CmdLetBinding(
      SupportsShouldProcess = $True,
      ConfirmImpact = 'Medium'
  )]
  Param(
    [PSObject[]]
    $Entities = $(Get-AtwsFieldInfo -Dynamic)
  )
  
  Begin
  { 
    # Prepare parameters for @splatting
    $ProgressId = 2
    $ProgressParameters = @{
      Activity = 'Creating and importing functions for all Autotask entities with picklists.'
      Id = $ProgressId
    }
    
    # Enable modern -Debug behavior
    If ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) {$DebugPreference = 'Continue'}  
              
    Write-Debug -Message ('{0}: Start of functions.' -F $MyInvocation.MyCommand.Name)
    
    $RootPath = '{0}\WindowsPowershell\Cache\{1}' -f $([environment]::GetFolderPath('MyDocuments')), $Script:Atws.CI

    # Separate cache for beta module
    If ($Script:IsBeta) { 
      $RootPath += '\Beta'
    }
    Else {
      $RootPath += '\Dynamic'
    }

    # Make sure directory exists
    If (-not (Test-Path "$RootPath")) {
      $Null = New-Item -Path "$RootPath" -ItemType Directory -Force
    }
    
  } 
  
  Process
  {
            
    # 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.GetEnumerator()) {
      # 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-Progress @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
        
      If ($PSCmdlet.ShouldProcess($VerboseDescription, $VerboseWarning, $Caption)) { 
        
        Foreach ($Function in $FunctionDefinition.GetEnumerator()) {
          # Set path to powershell script file in user cache
          $FilePath = '{0}\{1}.ps1' -F $RootPath, $Function.Key
          
          # IMport the updated function
          . ([ScriptBlock]::Create($Function.Value))
          
          # Export the module member
          Export-ModuleMember -Function $Function.Key
          
          # Write the function to disk for faster load later
          Set-Content -Path $FilePath -Value $Function.Value -Force -Encoding UTF8           
        }
      }
    }        

  }
  End
  {
    Write-Debug -Message ('{0}: Imported {1} dynamic functions' -F $MyInvocation.MyCommand.Name, $Index)
  }
}