Public/Get-AtwsConnectionObject.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
#Requires -Version 4.0
<#
 
    .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 Get-AtwsConnectionObject {
  <#
      .SYNOPSIS
      This function returns a Web Proxy Object with the active connection to Autotask Web Api from the current namespace.
      .DESCRIPTION
      This function returns a Web Proxy Object with the active connection to Autotask Web API from the current namespace.
      Advanced users may use this object for direct access to API methods or hardcoded queries. It may also be useful for
      debugging.
      .INPUTS
      Nothing.
      .OUTPUTS
      [Web.Services.Protocols.SoapHttpClientProtocol]
      .EXAMPLE
      $Atws = Get-AtwsConnectionObject
      Gets a Web Proxy Object with the active connection to Autotask Web Api from the current namespace.
      .NOTES
      NAME: Get-AtwsConnectionObject
       
  #>

    
  [cmdletbinding(SupportsShouldProcess = $True, ConfirmImpact = 'High')]
  Param()
  
  Begin {
    
    # Enable modern -Debug behavior before the first Write-Debug
    If ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) { $DebugPreference = 'Continue' }
    
    Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name)
    
    If (-not($Script:Atws.Url)) {
      Throw [ApplicationException] 'Not connected to Autotask WebAPI. Re-import module with valid credentials.'
    }    
    
  }

  Process {
    $Caption = $MyInvocation.MyCommand.Name
    $VerboseDescrition = '{0}: About to return the Web Proxy Object for the current connection Autotask Web API. ' -F $Caption
    $VerboseWarning = '{0}: About to return the Web Proxy Object for the current connection Autotask Web API. Do you want to continue?' -F $Caption
    
    If ($PSCmdlet.ShouldProcess($VerboseDescrition, $VerboseWarning, $Caption)) { 
      Return $Script:Atws
    }
  }

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