Public/Add-JiraIssueAttachment.ps1
function Add-JiraIssueAttachment { [CmdletBinding( SupportsShouldProcess )] param( [Parameter( Mandatory )] [ValidateNotNullOrEmpty()] [ValidateScript( { if (("JiraPS.Issue" -notin $_.PSObject.TypeNames) -and (($_ -isnot [String]))) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"Invalid Type for Parameter"), 'ParameterType.NotJiraIssue', [System.Management.Automation.ErrorCategory]::InvalidArgument, $_ ) $errorItem.ErrorDetails = "Wrong object type provided for Issue. Expected [JiraPS.Issue] or [String], but was $($_.GetType().Name)" $PSCmdlet.ThrowTerminatingError($errorItem) <# #ToDo:CustomClass Once we have custom classes, this check can be done with Type declaration #> } else { return $true } } )] [Alias('Key')] [Object] $Issue, <# #ToDo:CustomClass Once we have custom classes, this can also accept ValueFromPipeline #> [Parameter( Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [ValidateScript( { if (-not (Test-Path $_ -PathType Leaf)) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"File not found"), 'ParameterValue.FileNotFound', [System.Management.Automation.ErrorCategory]::ObjectNotFound, $_ ) $errorItem.ErrorDetails = "No file could be found with the provided path '$_'." $PSCmdlet.ThrowTerminatingError($errorItem) } else { return $true } } )] [Alias('InFile', 'FullName', 'Path')] [String[]] $FilePath, [PSCredential] $Credential, [Switch] $PassThru ) begin { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started" $resourceURi = "{0}/attachments" } process { Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] ParameterSetName: $($PsCmdlet.ParameterSetName)" Write-DebugMessage "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)" if (@($Issue).Count -ne 1) { $errorItem = [System.Management.Automation.ErrorRecord]::new( ([System.ArgumentException]"invalid Issue provided"), 'ParameterValue.JiraIssue', [System.Management.Automation.ErrorCategory]::InvalidArgument, $_ ) $errorItem.ErrorDetails = "Only one Issue can be provided at a time." $PSCmdlet.ThrowTerminatingError($errorItem) } # Find the proper object for the Issue $issueObj = Resolve-JiraIssueObject -InputObject $Issue -Credential $Credential $parameter = @{ URI = $resourceURi -f $issueObj.RestURL Method = "POST" Credential = $Credential } foreach ($file in $FilePath) { $parameter["InFile"] = $file Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter" if ($PSCmdlet.ShouldProcess($IssueObj.Key, "Adding attachment '$($fileName)'.")) { $rawResult = Invoke-JiraMethod @parameter if ($PassThru) { Write-Output (ConvertTo-JiraAttachment -InputObject $rawResult) } } } } end { Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete" } } |