diff --git a/functions/Merge-Ticket.ps1 b/functions/Merge-Ticket.ps1 new file mode 100644 index 0000000..6960fdc --- /dev/null +++ b/functions/Merge-Ticket.ps1 @@ -0,0 +1,62 @@ + +function Merge-Ticket { + + [OutputType([PSCustomObject])] + [CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + Param ( + + # Unique Id of the ticket or tickets to merge into target ticket + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $SourceTicketId, + + # Unique Id of the ticket to merge source tickets into. + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $TargetTicketId, + + # Private comment to leave on the source tickets. + [Parameter(Mandatory = $false)] + [ValidateNotNullOrEmpty()] + [String] + $SourceTicketComment, + + # Private comment to leave on the target ticket. + [Parameter(Mandatory = $false)] + [ValidateNotNullOrEmpty()] + [String] + $TargetTicketComment, + + # Zendesk Connection Context from `Get-ZendeskConnection` + [Parameter(Mandatory = $false)] + [PSTypeName('ZendeskContext')] + [PSCustomObject] + $Context = $null + ) + + if (-not $PSBoundParameters.ContainsKey('SourceTicketComment')) { + $SourceTicketComment = "This request was closed and merged into request #$TargetTicketId" + } + + if (-not $PSBoundParameters.ContainsKey('TargetTicketComment')) { + if ($SourceTicketId.Count -gt 1) { + $ids = $SourceTicketId -join ' #' + $TargetTicketComment = "Requests #$ids were closed and merged into this request." + } else { + $TargetTicketComment = "Request #$SourceTicketId was closed and merged into this request." + } + } + + $path = "/api/v2/tickets/$TargetTicketId/merge.json" + $body = @{ + ids = $SourceTicketId + target_comment = $TargetTicketComment + source_comment = $SourceTicketComment + } + + $result = Invoke-Method -Context $Context -Method 'Post' -Path $path -Body $body -Verbose:$VerbosePreference + $result + +} diff --git a/functions/New-Ticket.ps1 b/functions/New-Ticket.ps1 new file mode 100644 index 0000000..6a2cfa9 --- /dev/null +++ b/functions/New-Ticket.ps1 @@ -0,0 +1,217 @@ + +function New-Ticket { + + [OutputType([PSCustomObject])] + [CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + Param ( + + # The subject of the ticket + [Parameter(Mandatory = $false)] + [String] + $Subject, + + # The dynamic content placeholder + [Parameter(Mandatory = $false)] + [String] + $RawSubject, + + # An object that adds a comment to the ticket. + [Parameter(Mandatory = $true)] + [PSCustomObject] + $Comment, + + # The numeric ID of the user asking for support through the ticket + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $RequesterId, + + # The user who submitted the ticket. + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $SubmitterId, + + # The numeric ID of the agent to assign the ticket to + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $AssigneeId, + + # The email address of the agent to assign the ticket to + [Parameter(Mandatory = $false)] + [ValidatePattern('@')] + [String] + $AssigneeEmail, + + # The original recipient e-mail address of the ticket + [Parameter(Mandatory = $false)] + [ValidatePattern('@')] + [String] + $RecipientEmail, + + # The numeric ID of the group to assign the ticket to + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $GroupId, + + # The numeric ID of the organization to assign the ticket to. + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $OrganizationId, + + # An array of the numeric IDs of agents or end-users to CC. + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $CollaboratorId, + + # An array of numeric IDs, emails, or objects containing name and email properties. + [Parameter(Mandatory = $false)] + [Object[]] + $Collaborator, + + # An array of objects that represent agent followers to add or delete from the ticket. + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $FollowerId, + + # An array of objects that represent agent or end users email CCs to add or delete from the ticket. + [Parameter(Mandatory = $false)] + [Object[]] + $EmailCc, + + # Allowed values are problem, incident, question, or task + [Parameter(Mandatory = $false)] + [ValidateSet('problem', 'incident', 'question', 'task')] + [String] + $Type, + + # Allowed values are urgent, high, normal, or low + [Parameter(Mandatory = $false)] + [ValidateSet('urgent', 'high', 'normal', 'low')] + [String] + $Priority, + + # Allowed values are open, pending, hold, solved or closed + [Parameter(Mandatory = $false)] + [ValidateSet('new', 'open', 'pending', 'hold', 'solved', 'closed')] + $Status, + + # An array of tags to add to the ticket. + [Parameter(Mandatory = $false)] + [ValidateNotNullOrEmpty()] + [String[]] + $Tags, + + # An ID to link tickets to local records + [Parameter(Mandatory = $false)] + [String] + $ExternalId, + + # The topic this ticket originated from, if any + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $ForumTopicId, + + # The id of a closed ticket when creating a follow-up ticket. + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $FollowupId, + + # The id of the ticket form to render for the ticket + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $FormId, + + # The id of the brand this ticket is associated with + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $BrandId, + + # For tickets of type "incident", the numeric ID of the problem the incident is linked to, if any + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $ProblemId, + + # For tickets of type "task", the due date of the task. Accepts the ISO 8601 date format (yyyy-mm-dd) + [Parameter(Mandatory = $false)] + [DateTime] + $DueAt, + + # An array of the custom field objects consisting of ids and values. + [Parameter(Mandatory = $false)] + [PSCustomObject[]] + $CustomFields, + + # An array of macro IDs to be recorded in the ticket audit + [Parameter(Mandatory = $false)] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $MacroId, + + # Zendesk Connection Context from `Get-ZendeskConnection` + [Parameter(Mandatory = $false)] + [PSTypeName('ZendeskContext')] + [PSCustomObject] + $Context = $null + ) + + $path = "/api/v2/tickets.json" + + $body = @{ + ticket = @{} + } + + $map = @{ + external_id = 'ExternalId' + type = 'Type' + subject = 'Subject' + raw_subject = 'RawSubject' + comment = 'Comment' + priority = 'Priority' + status = 'Status' + recipient = 'RecipientEmail' + requester_id = 'RequesterId' + submitter_id = 'SubmitterId' + assignee_id = 'AssigneeId' + assignee = 'AssigneeEmail' + organization_id = 'OrganizationId' + collaborator_ids = 'CollaboratorId' + collaborators = 'Collaborator' + email_cc_ids = 'EmailCc' + follower_ids = 'FollowerId' + forum_topic_id = 'ForumTopicId' + problem_id = 'ProblemId' + due_at = 'DueAt' + tags = 'Tags' + custom_fields = 'CustomFields' + via_followup_source_id = 'FollowupId' + macro_ids = 'MacroId' + ticket_form_id = 'FormId' + brand_id = 'BrandId' + group_id = 'GroupId' + } + + foreach ($item in $map.GetEnumerator()) { + $property = $item.key + $parameter = $item.value + if ($PSBoundParameters.ContainsKey($parameter)) { + $body.ticket[$property] = $PSBoundParameters.$parameter + } + } + + if ($PSCmdlet.ShouldProcess($Subject, "Create Zendesk Ticket")) { + $result = Invoke-Method -Context $Context -Method 'Post' -Path $path -Body $body -Verbose:$VerbosePreference + $result + } + +} diff --git a/functions/Set-GroupMembershipAsDefault.ps1 b/functions/Set-GroupMembershipAsDefault.ps1 new file mode 100644 index 0000000..11db4fb --- /dev/null +++ b/functions/Set-GroupMembershipAsDefault.ps1 @@ -0,0 +1,36 @@ + +function Set-GroupMembershipAsDefault { + + [OutputType([PSCustomObject])] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + Param ( + + # Unique Id of User to set default group membership for + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [ValidateNotNullOrEmpty()] + [Int64[]] + $UserId, + + # Unique Id of group membership to make default + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [ValidateNotNullOrEmpty()] + [Int64[]] + $MembershipId, + + # Zendesk Connection Context from `Get-ZendeskConnection` + [Parameter(Mandatory = $false)] + [PSTypeName('ZendeskContext')] + [PSCustomObject] + $Context = $null + ) + + $path = "/api/v2/users/$UserId/group_memberships/$MembershipId/make_default.json" + + if ($PSCmdlet.ShouldProcess($UserId, "Set default group: $MembershipId")) { + $result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference + $result + } + +} diff --git a/functions/Set-OrganizationMembershipAsDefault.ps1 b/functions/Set-OrganizationMembershipAsDefault.ps1 new file mode 100644 index 0000000..d1573c9 --- /dev/null +++ b/functions/Set-OrganizationMembershipAsDefault.ps1 @@ -0,0 +1,36 @@ + +function Set-OrganizationMembershipAsDefault { + + [OutputType([PSCustomObject])] + [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + Param ( + + # Unique Id of user to set default organization for + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [ValidateNotNullOrEmpty()] + [Int64[]] + $UserId, + + # Unique Id of organization membership to set as default + [Parameter(Mandatory = $true)] + [ValidateRange(1, [Int64]::MaxValue)] + [ValidateNotNullOrEmpty()] + [Int64[]] + $MembershipId, + + # Zendesk Connection Context from `Get-ZendeskConnection` + [Parameter(Mandatory = $false)] + [PSTypeName('ZendeskContext')] + [PSCustomObject] + $Context = $null + ) + + $path = "/api/v2/users/$UserId/organization_memberships/$MembershipId/make_default.json" + + if ($PSCmdlet.ShouldProcess($UserId, "Set default Organization Membership: $MembershipId")) { + $result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Verbose:$VerbosePreference + $result + } + +} diff --git a/functions/Update-Ticket.ps1 b/functions/Update-Ticket.ps1 new file mode 100644 index 0000000..40fb1fa --- /dev/null +++ b/functions/Update-Ticket.ps1 @@ -0,0 +1,262 @@ + +function Update-Ticket { + + [OutputType([PSCustomObject])] + [CMDletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] + Param ( + + # Tickets to update + [Parameter(Mandatory = $true, + ParameterSetName = 'Object')] + [PSCustomObject[]] + $Ticket, + + # Ids of tickets to update + [Parameter(Mandatory = $true, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $Id, + + # The subject of the ticket + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [String] + $Subject, + + # An object that adds a comment to the ticket. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [PSCustomObject] + $Comment, + + # The numeric ID of the user asking for support through the ticket + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $RequesterId, + + # The numeric ID of the agent to assign the ticket to + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $AssigneeId, + + # The email address of the agent to assign the ticket to + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidatePattern('@')] + [String] + $AssigneeEmail, + + # The numeric ID of the group to assign the ticket to + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $GroupId, + + # The numeric ID of the organization to assign the ticket to. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $OrganizationId, + + # An array of the numeric IDs of agents or end-users to CC. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $CollaboratorId, + + # An array of numeric IDs, emails, or objects containing name and email properties. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [Object[]] + $Collaborator, + + # An array of numeric IDs, emails, or objects containing name and email properties. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [Object[]] + $AdditionalCollaborators, + + # An array of objects that represent agent followers to add or delete from the ticket. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [Object[]] + $Followers, + + # An array of objects that represent agent or end users email CCs to add or delete from the ticket. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [Object[]] + $EmailCc, + + # Allowed values are problem, incident, question, or task + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateSet('problem', 'incident', 'question', 'task')] + [String] + $Type, + + # Allowed values are urgent, high, normal, or low + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateSet('urgent', 'high', 'normal', 'low')] + [String] + $Priority, + + # Allowed values are open, pending, hold, solved or closed + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateSet('open', 'pending', 'hold', 'solved', 'closed')] + $Status, + + # An array of tags to add to the ticket. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateNotNullOrEmpty()] + [String[]] + $Tags, + + # An ID to link tickets to local records + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [String] + $ExternalId, + + # For tickets of type "incident", the numeric ID of the problem the incident is linked to, if any + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64] + $ProblemId, + + # For tickets of type "task", the due date of the task. Accepts the ISO 8601 date format (yyyy-mm-dd) + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [DateTime] + $DueAt, + + # An array of the custom field objects consisting of ids and values. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [PSCustomObject[]] + $CustomFields, + + # Datetime of last update received from API. See safe_update param + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [String] + $UpdatedStamp, + + # Optional boolean. Prevents updates with outdated ticket data (updated_stamp param required when true) + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [Switch] + $SafeUpdate, + + # An array of the numeric IDs of sharing agreements. + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $SharingAgreementId, + + # An array of macro IDs to be recorded in the ticket audit + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $MacroId, + + # An array of the IDs of attribute values to be associated with the ticket + [Parameter(Mandatory = $false, + ParameterSetName = 'Property')] + [ValidateRange(1, [Int64]::MaxValue)] + [Int64[]] + $AttributeValueId, + + # Zendesk Connection Context from `Get-ZendeskConnection` + [Parameter(Mandatory = $false)] + [PSTypeName('ZendeskContext')] + [PSCustomObject] + $Context = $null + ) + + if ($PSCmdlet.ParameterSetName -eq 'Property') { + + if ($Id.Count -gt 1) { + $ids = $Id -join ',' + $path = "/api/v2/tickets/update_many.json?ids=$ids" + } else { + $path = "/api/v2/tickets/$id.json" + } + + $body = @{ + ticket = @{} + } + + $map = @{ + subject = 'Subject' + comment = 'Comment' + requester_id = 'RequesterId' + assignee_id = 'AssigneeId' + assignee_email = 'AssigneeEmail' + group_id = 'GroupId' + organization_id = 'OrganizationId' + collaborator_ids = 'CollaboratorId' + collaborators = 'Collaborator' + additional_collaborators = 'AdditionalCollaborators' + followers = 'Followers' + email_ccs = 'EmailCc' + type = 'Type' + priority = 'Priority' + status = 'Status' + tags = 'Tags' + external_id = 'ExternalId' + problem_id = 'ProblemId' + due_at = 'DueAt' + custom_fields = 'CustomFields' + updated_stamp = 'UpdatedStamp' + safe_update = 'SafeUpdate' + sharing_agreement_ids = 'SharingAgreementId' + macro_ids = 'MacroId' + attribute_value_ids = 'AttributeValueId' + } + + foreach ($item in $map.GetEnumerator()) { + $property = $item.key + $parameter = $item.value + if ($PSBoundParameters.ContainsKey($parameter)) { + $body.ticket[$property] = $PSBoundParameters.$parameter + } + } + + } else { + + if ($Ticket.count -gt 1) { + $path = '/api/v2/tickets/update_many.json' + $body = @{ + tickets = $Ticket + } + } else { + $id = $Ticket.id + $path = "/api/v2/tickets/$id.json" + $body = @{ + ticket = $Ticket[0] + } + } + + } + + if ($PSCmdlet.ShouldProcess('Update Ticket')) { + $result = Invoke-Method -Context $Context -Method 'Put' -Path $path -Body $body -Verbose:$VerbosePreference + $result + } + +}