Public/New-AtwsAttachment.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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
#Requires -Version 4.0 <# .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 New-AtwsAttachment { <# .SYNOPSIS This function creates a new attachment through the Autotask Web Services API. .DESCRIPTION This function creates a new attachment connected to either an Account, an Opportunity, a Project or a Ticket. The attachment can be passed through the pipeline or provided as en URL or a file or folder path. .INPUTS Nothing .OUTPUTS Autotask attachments .EXAMPLE New-AtwsAttachment -TicketId 0 -Path C:\Document.docx Uploads C:\Document.docx as an attachment to the Ticket with id 0 and sets the attachment title to 'Document.docx'. .EXAMPLE New-AtwsAttachment -TicketId 0 -Path C:\Document.docx -Title 'A title' Uploads C:\Document.docx as an attachment to the Ticket with id 0 and sets the attachment title to 'A title'. .EXAMPLE New-AtwsAttachment -TicketId 0 -Path C:\Document.docx -FileLink Adds an file link attachment to the Ticket with id 0, title 'Document.docx' and C:\Document.docx as full path. .EXAMPLE $Attachment = Get-AtwsAttachment -TicketID 0 | Select-Object -First 1 New-AtwsAttachment -Data $Attachment.Data -TicketId 1 -Title $Attachment.Info.Title -Extension $([IO.Path]::GetExtension($Attachment.Info.FullPath)) Gets the first attachment from Ticket with id 0 and attaches it to Ticket with id 1 .NOTE Strongly related to Get-AtwsAttachmentInfo #> [CmdLetBinding(SupportsShouldProcess = $true, DefaultParameterSetName = 'Ticket', ConfirmImpact = 'None')] Param ( # An object as a byte array that will be attached to an Autotask object [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_byte' )] [ValidateNotNullOrEmpty()] [Byte[]] $Data, # An object as a byte array that will be attached to an Autotask object [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_byte' )] [ValidateNotNullOrEmpty()] [ValidatePattern('^\.?\w+$')] [string] $Extension, # A is required for Data [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_byte' )] [Parameter( ParameterSetName = 'Account' )] [Parameter( ParameterSetName = 'Opportunity' )] [Parameter( ParameterSetName = 'Project' )] [Parameter( ParameterSetName = 'Ticket' )] [Parameter( ParameterSetName = 'Account_as_url' )] [Parameter( ParameterSetName = 'Opportunity_as_url' )] [Parameter( ParameterSetName = 'Project_as_url' )] [Parameter( ParameterSetName = 'Ticket_as_url' )] [ValidateNotNullOrEmpty()] [string] $Title, # A file path that will be attached to an Autotask object [Parameter( Mandatory = $true, ParameterSetName = 'Account' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket' )] [ValidateNotNullOrEmpty()] [ValidateScript( { if ( -Not ($_ | Test-Path) ) { throw "File or folder does not exist" } return $true })] [IO.FileInfo] $Path, # URL to attach [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_url' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_url' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_url' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_url' )] [URI] $URI, # Attach as a file link, not an attachment [Parameter( ParameterSetName = 'Account_as_url' )] [Parameter( ParameterSetName = 'Opportunity_as_url' )] [Parameter( ParameterSetName = 'Project_as_url' )] [Parameter( ParameterSetName = 'Ticket_as_url' )] [Alias('Link')] [switch] $FileLink, # Attach as a folder link, not an attachment [Parameter( ParameterSetName = 'Account_as_url' )] [Parameter( ParameterSetName = 'Opportunity_as_url' )] [Parameter( ParameterSetName = 'Project_as_url' )] [Parameter( ParameterSetName = 'Ticket_as_url' )] [Alias('Folder')] [switch] $FolderLink, # Account ID [Parameter( Mandatory = $true, ParameterSetName = 'Account' )] [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Account_as_url' )] [ValidateScript( { if ( -Not (Get-AtwsAccount -id $_) ) { throw "Account does not exist" } return $true })] [long] $AccountID, # Opportunity ID [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Opportunity_as_url' )] [ValidateScript( { if ( -Not (Get-AtwsOpportunity -id $_) ) { throw "Opportunity does not exist" } return $true })] [long] $OpportunityID, # Project ID [Parameter( Mandatory = $true, ParameterSetName = 'Project' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Project_as_url' )] [ValidateScript( { if ( -Not (Get-AtwsProject -id $_) ) { throw "Project does not exist" } return $true })] [long] $ProjectID, # Ticket ID [Parameter( Mandatory = $true, ParameterSetName = 'Ticket' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_byte' )] [Parameter( Mandatory = $true, ParameterSetName = 'Ticket_as_url' )] [ValidateScript( { if ( -Not (Get-AtwsTicket -id $_) ) { throw "Ticket does not exist" } return $true })] [long] $TicketID, [ValidateSet('All Autotask Users', 'Internal Users Only')] [string] $Publish = 'All Autotask Users' ) begin { # Enable modern -Debug behavior if ($PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent) { $DebugPreference = 'Continue' } Write-Debug ('{0}: Begin of function' -F $MyInvocation.MyCommand.Name) # Dynamic field info $fields = Get-AtwsFieldInfo -Entity AttachmentInfo $Picklists = $fields.Where{ $_.IsPickList } # Publish dictionary $PublishToIndex = @{ 'All Autotask Users' = '1' 'Internal Users Only' = '2' } } process { # A new Attachment object $Attachment = New-Object "Autotask.Attachment" # A new AttachmentInfo object $AttachmentInfo = New-Object "Autotask.AttachmentInfo" # Attach info object to attachment object $Attachment.Info = $AttachmentInfo # Publishsettings $AttachmentInfo.Publish = $PublishToIndex[$Publish] # Attachment type if ($Data) { $Attachment.Data = $Data $AttachmentInfo.Type = 'FILE_ATTACHMENT' $AttachmentInfo.FullPath = '{0}.{1}' -F $Title, $Extension.TrimStart('.') } # Is it an URL? elseif ($URI) { if ($FolderLink.IsPresent) { $AttachmentInfo.Type = 'FOLDER_LINK' } elseif ($FileLink.IsPresent) { $AttachmentInfo.Type = 'FILE_LINK' } else { $AttachmentInfo.Type = 'URL' } $ATtachmentInfo.FullPath = $URI.AbsoluteUri $AttachmentInfo.Title = $AttachmentInfo.FullPath } # It is a file and it is going to be attached. else { [Byte[]]$Data = Get-Content -Path $Path.FullName -Encoding Byte -ReadCount 0 $Attachment.Data = $Data # Type is attachment $AttachmentInfo.Type = 'FILE_ATTACHMENT' $AttachmentInfo.Title = $Path.BaseName $AttachmentInfo.FullPath = $Path.FullName # Determine content type by file name Add-Type -AssemblyName "System.Web" $AttachmentInfo.ContentType = [System.Web.MimeMapping]::GetMimeMapping($Path.FullName) } # Overwrite title with $Title if it exists if ($Title) { $AttachmentInfo.Title = $Title } # What are we attaching to? $objectType = ($PSCmdlet.ParameterSetName -split '_')[0] $AttachmentInfo.ParentId = $TicketId + $AccountID + $ProjectID + $OpportunityId $AttachmentInfo.ParentType = $Picklists.Where{ $_.name -eq 'ParentType' }.PickListValues.Where{ $_.Label -eq $objectType }.Value # Prepare ShouldProcess $caption = $MyInvocation.MyCommand.Name $verboseDescription = '{0}: About to create an attachment of type {1} with title {2}.' -F $caption, $AttachmentInfo.Type, $AttachmentInfo.Title $verboseWarning = '{0}: About to create an attachment of type {1} with title {2}. Do you want to continue?' -F $caption, $AttachmentInfo.Type, $AttachmentInfo.Title # Do it, I dare you! if ($PSCmdlet.ShouldProcess($verboseDescription, $verboseWarning, $caption)) { $AttachmentId = $Script:Atws.CreateAttachment($Script:Atws.integrationsValue, $Attachment) $result = Get-AtwsAttachmentInfo -id $AttachmentId Write-Verbose ('{0}: Created attachment with id {1} and title {2}' -F $MyInvocation.MyCommand.Name, $AttachmentId, $result.Title) } } end { Write-Debug ('{0}: End of function' -F $MyInvocation.MyCommand.Name) if ($result) { Return $result } } } |