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 191 192 193 194 195 196 |
<# .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 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','UsePickListLabels')] [switch] $ConvertPicklistIdToLabel, [Parameter( ParameterSetName = 'Default' )] [Parameter( ParameterSetName = 'NoDiskCache' )] [ValidateScript( { # It can be empty, but if it isn't it should be max 8 characters and only letters and numbers if ($_.length -eq 0 -or ($_ -match '[a-zA-Z0-9]' -and $_.length -gt 0 -and $_.length -le 8)) { $true } else { $false } })] [string] $Prefix, [Parameter( ParameterSetName = 'Default' )] [switch] $RefreshCache, [Parameter( ParameterSetName = 'NoDiskCache' )] [switch] $NoDiskCache, [Parameter( Mandatory = $true, ParameterSetName = 'ConfigurationObject' )] [ValidateScript( { $requiredProperties = @('Username', 'Securepassword', 'SecureTrackingIdentifier', 'ConvertPicklistIdToLabel', 'Prefix', 'RefreshCache', 'NoDiskCache', 'DebugPref', 'VerbosePref') $members = Get-Member -InputObject $_ -MemberType NoteProperty $missingProperties = Compare-Object -ReferenceObject $requiredProperties -DifferenceObject $members.Name -PassThru -ErrorAction SilentlyContinue if (-not($missingProperties)) { $true } else { $missingProperties | ForEach-Object { Throw [System.Management.Automation.ValidationMetadataException] "Property: '$_' missing" } } })] [pscustomobject] $Configuration ) begin { # Enable modern -Debug behavior if ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) { $DebugPreference = 'Continue' } Write-Verbose ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) # the $My hashtable is created by autotask.psm1 $importParams = @{ Global = $true Version = $My.ModuleVersion Force = $true ErrorAction = 'Stop' } } process { # Make sure we have a valid configuration before we proceed try { # If we didn't get a prepared configuration object, create one from the parameters if ($PSCmdlet.ParameterSetName -ne 'ConfigurationObject') { $Parameters = @{ Credential = $Credential SecureTrackingIdentifier = ConvertTo-SecureString $ApiTrackingIdentifier -AsPlainText -Force ConvertPicklistIdToLabel = $ConvertPicklistIdToLabel.IsPresent Prefix = $Prefix RefreshCache = $RefreshCache.IsPresent NoDiskCache = $NoDiskCache.IsPresent DebugPref = $DebugPreference VerbosePref = $VerbosePreference } # We cannot reuse $configuration variable without triggering the validationscript # again $ConfigurationData = New-AtwsModuleConfiguration @Parameters } elseif (Test-AtwsModuleConfiguration -Configuration $Configuration) { # We got a configuration object and it passed validation $ConfigurationData = $Configuration } } catch { $message = "{0}`nStacktrace:`n{1}" -f $_, $_.ScriptStackTrace throw (New-Object System.Configuration.Provider.ProviderException $message) return } # Question 1: Is the current module in $env:PSModulePath $notInPath = $true $separator = if ($IsMacOS -or $IsLinux) { ':' } else { ';' } foreach ($dir in $env:PSModulePath -split $separator) { # Separator can be both ; and : depending on platform if ($My.ModuleBase -like "$dir*") { $notInPath = $false } } if ($notInPath) { # Import the module from its base directory $moduleName = $My.ModuleBase } else { # Import by module name $moduleName = $MyInvocation.MyCommand.ModuleName } # Reload the module with configuration Import-Module -Name $moduleName @importParams -ArgumentList $ConfigurationData } end { Write-Debug ('{0}: End of function' -F $MyInvocation.MyCommand.Name) } } |