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 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 |
Function Import-AtwsCmdLet { [CmdLetBinding( SupportsShouldProcess = $True, ConfirmImpact = 'Medium' )] Param ( [Parameter(Mandatory = $True)] [String] $ModuleName, [Switch] $NoDiskCache, [Switch] $RefreshCache, [String] $Prefix = 'Atws' ) Begin { If (-not($global:AtwsConnection[$Prefix].Url)) { Throw [ApplicationException] 'Not connected to Autotask WebAPI. Run Connect-AutotaskWebAPI first.' } Else { $local:Atws = $global:AtwsConnection[$Prefix] } Write-Verbose -Message ('{0}: Calling API.EntityInfo() to get list over available entities.' -F $MyInvocation.MyCommand.Name) $Caption = $MyInvocation.MyCommand.Name $VerboseDescrition = '{0}: Calling API.EntityInfo()' -F $Caption $VerboseWarning = '{0}: About to call API.EntityInfo(). Do you want to continue?' -F $Caption If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { $Entities = $Atws.getEntityInfo() } Else { $Entities = @() } # A hashtable to keep track of fieldinfo pr entity. Keep it in the script and called functions $Script:FieldTable = @{} # An array to contain text that will be converted to a scriptblock $ModuleFunctions = @() $CacheInfo = Get-AtwsCacheInfo -Prefix $Prefix } Process { If ($CacheInfo.CacheDirty -or $NoDiskCache.IsPresent -or $RefreshCache.IsPresent) { Write-Verbose -Message ('{0}: Generating new functions (CacheDirty: {1}, NoDiskCache: {2}) ' -F $MyInvocation.MyCommand.Name,$CacheInfo.CacheDirty.ToString(), $NoDiskCache.IsPresent.ToString()) $Activity = 'Downloading detailed information about all Autotask entity types' $ParentId = 1 Foreach ($Entity in $Entities) { Write-Verbose -Message ('{0}: Importing detailed information about Entity {1}' -F $MyInvocation.MyCommand.Name, $Entity.Name) $FieldTable[$Entity.Name] = $atws.GetFieldInfo($Entity.Name) # Calculating progress percentage and displaying it $Index = $Entities.IndexOf($Entity) +1 $PercentComplete = $Index / $Entities.Count * 100 $Status = 'Entity {0}/{1} ({2:n0}%)' -F $Index, $Entities.Count, $PercentComplete $CurrentOperation = "GetFieldInfo('{0}')" -F $Entity.Name Write-Progress -ParentId $ParentId -Activity $Activity -Status $Status -PercentComplete $PercentComplete -CurrentOperation $CurrentOperation $VerboseDescrition = '{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 } $Activity = 'Creating and importing functions for all Autotask entities.' Foreach ($Entity in $Entities) { Write-Verbose -Message ('{0}: Creating functions for entity {1}' -F $MyInvocation.MyCommand.Name, $Entity.Name) # Calculating progress percentage and displaying it $Index = $Entities.IndexOf($Entity) +1 $PercentComplete = $Index / $Entities.Count * 100 $Status = 'Entity {0}/{1} ({2:n0}%)' -F $Index, $Entities.Count, $PercentComplete $CurrentOperation = 'Importing {0}' -F $Entity.Name Write-Progress -ParentId $ParentId -Activity $Activity -Status $Status -PercentComplete $PercentComplete -CurrentOperation $CurrentOperation $VerboseDescrition = '{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 -Prefix $Prefix -FieldInfo $FieldTable[$Entity.Name] If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { Foreach ($Function in $FunctionDefinition.GetEnumerator()) { If (-Not $NoDiskCache.IsPresent) { Write-Verbose -Message ('{0}: Writing file for function {1}' -F $MyInvocation.MyCommand.Name, $Function.Key) $FilePath = '{0}\{1}.ps1' -F $CacheInfo.CacheDir, $Function.Key $VerboseDescrition = '{0}: Overwriting {1}' -F $Caption, $FilePath $VerboseWarning = '{0}: About to overwrite {1}. Do you want to continue?' -F $Caption, $FilePath If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { Set-Content -Path $FilePath -Value $Function.Value -Force -Encoding UTF8 } } $ModuleFunctions += $Function.Value } } } Write-Verbose -Message ('{0}: Writing Moduleversion info to {1}' -F $MyInvocation.MyCommand.Name, $CacheInfo.CachePath) $ModuleVersionInfo = New-Object -TypeName PSObject -Property @{ APIversion = $CacheInfo.APIversion ModuleVersion = $CacheInfo.ModuleVersion CI = $CacheInfo.CI } Export-Clixml -InputObject $ModuleVersionInfo -Path $CacheInfo.CachePath -Encoding UTF8 Write-Progress -ParentId $ParentId -Activity $Activity -Completed } Else { Write-Verbose -Message ('{0}: Reading function definitions from {1}' -F $MyInvocation.MyCommand.Name, $CacheInfo.CachePath) $CacheFiles = '{0}\*.ps1' -F $CacheInfo.CacheDir Foreach ($File in Get-ChildItem -Path $CacheFiles) { $ModuleFunctions += Get-Content -Path $File.FullName -Encoding UTF8 -Raw } } Write-Verbose -Message ('{0}: Including private functions in dynamic mocule' -F $MyInvocation.MyCommand.Name) $PrivateFunctions = @( 'Get-AtwsFieldInfo', 'Get-CallerPreference' ) Foreach ($FunctionName in $PrivateFunctions) { $ModuleFunctions += (Get-Command $FunctionName).ScriptBlock.Ast.Extent.Text } } End { Write-Verbose -Message ('{0}: Importing Autotask Dynamic Module' -F $MyInvocation.MyCommand.Name) $FunctionScriptBlock = [ScriptBlock]::Create($($ModuleFunctions)) New-Module -Name $ModuleName -ScriptBlock $FunctionScriptBlock | Import-Module -Global } } |