Public/Connect-AtwsWebAPI.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
<#
 
    .COPYRIGHT
    Copyright (c) Office Center Hønefoss AS. All rights reserved. Licensed under the MIT license.
    See https://github.com/officecenter/Autotask/blob/master/LICENSE.md for license information.
 
#>


Function Connect-AtwsWebAPI {
  <#
      .SYNOPSIS
      This function re-loads the module with the correct parameters for full functionality
      .DESCRIPTION
      This function is a wrapper that is included for backwards compatibility with previous module behavior.
      These parameters should be passed to Import-Module -Variable directly, but previously the module
      consisted of two, nested modules. Now there is a single module with all functionality.
      .INPUTS
      A PSCredential object. Required.
      A string used as ApiTrackingIdentifier. Required.
      .OUTPUTS
      Nothing.
      .EXAMPLE
      Connect-AtwsWebAPI -Credential $Credential -ApiTrackingIdentifier $String
      .NOTES
      NAME: Connect-AtwsWebAPI
  #>

    
  [cmdletbinding(
      SupportsShouldProcess = $True,
      ConfirmImpact = 'Low',
      DefaultParameterSetName = 'Default'
  )]
  Param
  (
    [Parameter(
        Mandatory = $true,
        ParameterSetName = 'Default'
    )]
    [Parameter(
        Mandatory = $true,
        ParameterSetName = 'NoDiskCache'
    )]
    [ValidateNotNullOrEmpty()]    
    [pscredential]
    $Credential,
    
    [Parameter(
        Mandatory = $true,
        ParameterSetName = 'Default'
    )]
    [Parameter(
        Mandatory = $true,
        ParameterSetName = 'NoDiskCache'
    )]
    [String]
    $ApiTrackingIdentifier,
    
    [Parameter(
        ParameterSetName = 'Default'
    )]
    [Parameter(
        ParameterSetName = 'NoDiskCache'
    )]
    [Alias('Picklist')]
    [Switch]
    $UsePicklistLabels,
    
    [Parameter(
        ParameterSetName = 'Default'
    )]
    [Parameter(
        ParameterSetName = 'NoDiskCache'
    )]
    [ValidatePattern('[a-zA-Z0-9]')]
    [ValidateLength(1, 8)]
    [String]
    $Prefix,

    [Parameter(
        ParameterSetName = 'Default'
    )]
    [Switch]
    $RefreshCache,

    
    [Parameter(
        ParameterSetName = 'NoDiskCache'
    )]
    [Switch]
    $NoDiskCache
  )
    
  Begin { 
    
    # Enable modern -Debug behavior
    If ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) {$DebugPreference = 'Continue'}
    
    Write-Verbose ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name)
    
    # The module is already loaded. It has to be, or this function would not be in
    # the users scope.
    $ModuleName = $MyInvocation.MyCommand.Module.Name
    
    # Store parameters to global variables. Force-reloading the module destroys this scope.
    $Global:AtwsCredential = $Credential
    $Global:AtwsApiTrackingIdentifier = $ApiTrackingIdentifier
    
    # Set Refresh pattern
    If ($RefreshCache.IsPresent) {
      $Global:AtwsRefreshCachePattern = '*'
    }
    
    # Set No disk cache preference
    If ($NoDiskCache.IsPresent) {
      $Global:AtwsNoDiskCache = $True
    }
    
    # Set Picklist preference
    If ($UsePicklistLabels.IsPresent) {
      $Global:AtwsUsePicklistLabels = $True
    }
    
    
    $ImportParams = @{
      Global = $True
      Version = $MyInvocation.MyCommand.Module.Version
      Force = $True
      ErrorAction = 'Stop'
    }
    
    If ($Prefix) {
      $ImportParams['Prefix'] = $Prefix    
    }



  }
  
  Process {
  
    Try 
    { 
      # First try to re-import the module by name
      
      # Unfortunately -Debug and -Verbose is not inherited into the module load, so we have to do a bit of awkward checking
      If ($DebugPreference -eq 'Continue' -and $VerbosePreference -eq 'Continue') {
        Import-Module -Name $ModuleName @ImportParams -Debug -Verbose
      }
      ElseIf ($DebugPreference -eq 'Continue' -and $VerbosePreference -ne 'Continue') {
        $ImportParams['Verbose'] = $False 
        Import-Module -Name $ModuleName @ImportParams -Debug 
      }
      ElseIf ($DebugPreference -ne 'Continue' -and $VerbosePreference -eq 'Continue') {
        Import-Module -Name $ModuleName @ImportParams -Verbose 
      }
      Else {
        Import-Module -Name $ModuleName @ImportParams 
      }
    }
    Catch 
    {
      # If import by name fails the module has most likely been loaded directly from disk (path)
      # Retry loading the module from its base directory
      $ModulePath = $MyInvocation.MyCommand.Module.ModuleBase
      $ImportParams['ErrorAction'] = 'Continue'
      
      # Unfortunately -Debug and -Verbose is not inherited into the module load, so we have to do a bit of awkward checking
      If ($DebugPreference -eq 'Continue' -and $VerbosePreference -eq 'Continue') {
        Import-Module -Name $ModulePath @ImportParams -Debug -Verbose
      }
      ElseIf ($DebugPreference -eq 'Continue' -and $VerbosePreference -ne 'Continue') {
        $ImportParams['Verbose'] = $False 
        Import-Module -Name $ModulePath @ImportParams -Debug 
      }
      ElseIf ($DebugPreference -ne 'Continue' -and $VerbosePreference -eq 'Continue') {
        Import-Module -Name $ModulePath @ImportParams -Verbose 
      }
      Else {
        Import-Module -Name $ModulePath @ImportParams 
      }

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