Private/New-AtwsData.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
<#
 
    .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 New-AtwsData 
{
  [cmdletbinding()]
  [OutputType([Object[]])]
  param
  (
    [Parameter(
        Mandatory = $True,
        Position = 0
    )]
    [PSObject[]]
    $Entity
  )
   
  Begin 
  { 
    # Enable modern -Debug behavior
    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.'
    }

    $EndResult = @()
  }
    
  Process { 
    Write-Verbose -Message ('{0}: Creating a new object of type Autotask.{1}' -F $MyInvocation.MyCommand.Name, $Entity.Name) 
    
    # create() function can take up to 200 objects at a time
    For ($i = 0; $i -lt $Entity.count; $i += 200) 
    {
      $j = $i + 199
      If ($j -gt ($Entity.count - 1)) 
      {
        $j = $Entity.count - 1
      } 
      Write-Debug -Message ('{0}: Creating chunk from index {1} to index {2}' -F $MyInvocation.MyCommand.Name, $i, $j)        
        
      # the .Create() method takes datetime in local time and correctly
      # translates to CEST without our assistance. .Update() is not as nice...
        
      $Result = $Atws.Create($Entity[$i .. $j])
        
        
      If ($Result.Errors.Count -eq 0) 
      {
        # Check for duplicates
        $Duplicates = $Result.EntityReturnInfoResults | Where-Object {$_.DuplicateStatus.Found -and -not $_.DuplicateStauts.Ignored}
           
        Foreach ($Duplicate in $Duplicates)
        {
          Write-Warning ('{0}: Duplicate found for Object Id {1} on {2}' -F $MyInvocation.MyCommand.Name, $Duplicate.EntityId, $Duplicate.DuplicateStatus.MatchInfo)
        }
          
        # The API documentation explicitly states that you can only use the objects returned
        # by the .create() function to get the new objects ID.
        # so to return objects with accurately represents what has been created we have to
        # get them again by id
        # But not all objects support queries, for instance service adjustments
          
        $EntityInfo = Get-AtwsFieldInfo -Entity $Result.EntityResultType -EntityInfo
          
        If ($Result.EntityResults.Count -gt 0 -and $EntityInfo.CanQuery)
        {
          $NewObjectFilter = 'id -eq {0}' -F ($Result.EntityResults.Id -join ' -or id -eq ')
                        
          $EndResult += Get-AtwsData -Entity $Result.Entityresulttype -Filter $NewObjectFilter
        }
      }
      Else 
      {
        Foreach ($AtwsError in $Result.Errors) 
        {
          Write-Error -Message $AtwsError.Message
        }
      }
    }
  }
  End {
   
    Write-Debug -Message ('{0}: End of function' -F $MyInvocation.MyCommand.Name) 
    Return $EndResult  
  }
}