NN.Pureservice.psm1

#Region './Private/Get-PuAccessToken.ps1' 0
function Get-PuAccessToken {
    param (
        [string]$accessTokenPath = "$env:USERPROFILE\.creds\Pureservice\PureserviceAccessToken.xml"
    )

    if (!(Test-Path $accessTokenPath)) {
        New-PuAccessToken
    }

    Import-Clixml $accessTokenPath | ConvertFrom-SecureString -AsPlainText
}
#EndRegion './Private/Get-PuAccessToken.ps1' 12
#Region './Private/Get-PuEndpoint.ps1' 0
function Get-PuEndpoint {
    param (
        [string]$endpointPath = "$env:USERPROFILE\.creds\Pureservice\PureserviceEndpoint.xml"
    )

    if (!(Test-Path $endpointPath)) {
        New-PuEndpoint
    }

    Import-Clixml $endpointPath
}
#EndRegion './Private/Get-PuEndpoint.ps1' 12
#Region './Public/Get-PuCommunicationDirectionList.ps1' 0
function Get-PuCommunicationDirectionList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                id = 1
                name = "Inbound"
            },
            [PSCustomObject]@{
                id = 2
                name = "Outbound"
            },
            [PSCustomObject]@{
                id = 3
                name = "None"
            }
        )
    }
}
#EndRegion './Public/Get-PuCommunicationDirectionList.ps1' 24
#Region './Public/Get-PuCommunications.ps1' 0
function Get-PuCommunications {
    [CmdletBinding()]
    param (
        [string]$filter,
        [string]$sort,
        [int]$limit,
        [array]$include
    )

    process {
        $Uri = "$(Get-PuEndpoint)/communication"
        #Parameters to exclude in Uri build
        $ParameterExclusion = @()
        #Build request Uri
        $PSBoundParameters.Keys.ForEach({
            [string]$Key = $_
            $Value = $PSBoundParameters.$key
        
            #Check if parameter is excluded
            if ($ParameterExclusion -contains $Key) {
                return
            }
        
            if ($Value.GetType().BaseType.Name -eq "Array") {
                $Value = $Value -join ","
            }
        
            #Check for "?" in Uri and set delimiter
            if (!($Uri -replace "[^?]+")) {
                $Delimiter = "?"
            } else {
                $Delimiter = "&"
            }
        
            $Uri = "$Uri$Delimiter$Key=$Value"
        })

        $Splat = @{
            "Uri" = $Uri
            "Method" = "GET"
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Accept" = "application/vnd.api+json"
            }
        }
        Invoke-RestMethod @Splat
    }
}
#EndRegion './Public/Get-PuCommunications.ps1' 49
#Region './Public/Get-PuCommunicationTypeList.ps1' 0
function Get-PuCommunicationTypeList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                id = 1
                name = "Note"
            },
            [PSCustomObject]@{
                id = 2
                name = "Message"
            },
            [PSCustomObject]@{
                id = 3
                name = "Sms"
            },
            [PSCustomObject]@{
                id = 4
                name = "Solution"
            }
            [PSCustomObject]@{
                id = 5
                name = "PreviousSolution"
            },
            [PSCustomObject]@{
                id = 6
                name = "History"
            },
            [PSCustomObject]@{
                id = 7
                name = "InitialMessage"
            },
            [PSCustomObject]@{
                id = 8
                name = "TaskHistory"
            },
            [PSCustomObject]@{
                id = 9
                name = "Timeloghistory"
            },
            [PSCustomObject]@{
                id = 10
                name = "ServicetargetHistory"
            },
            [PSCustomObject]@{
                id = 11
                name = "AttachmentHistory"
            },
            [PSCustomObject]@{
                id = 12
                name = "RiskHistory"
            },
            [PSCustomObject]@{
                id = 13
                name = "RollbackPlanHistory"
            }
        )
    }
}
#EndRegion './Public/Get-PuCommunicationTypeList.ps1' 64
#Region './Public/Get-PuDepartments.ps1' 0
function Get-PuDepartments {
    [CmdletBinding()]
    param (
        
    )

    process {
        $Splat = @{
            "Uri" = "$(Get-PuEndpoint)/department"
            "Method" = "GET"
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Accept" = "application/vnd.api+json"
            }
        }
        $Result = Invoke-RestMethod @Splat
        $Result.departments
    }
}
#EndRegion './Public/Get-PuDepartments.ps1' 20
#Region './Public/Get-PuPhoneNumberTypeList.ps1' 0
function Get-PuPhonenNumberTypeList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                "id" = 0
                "name" = "Home"
            },
            [PSCustomObject]@{
                "id" = 1
                "name" = "Cellphone"
            }
        )
    }
}
#EndRegion './Public/Get-PuPhoneNumberTypeList.ps1' 20
#Region './Public/Get-PuPriorities.ps1' 0
function Get-PuPriorities {
    $Splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-PuEndpoint)/priority"
        "Headers" = @{
            "X-Authorization-Key" = Get-PuAccessToken
            "Accept" = "application/vnd.api+json"
        }
    }
    $Result = Invoke-RestMethod @splat
    $Result.priorities
}
#EndRegion './Public/Get-PuPriorities.ps1' 13
#Region './Public/Get-PuRequestTypeList.ps1' 0
function Get-PuRequestTypeList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                "id" = 1
                "name" = "Ticket"
            },
            [PSCustomObject]@{
                "id" = 2
                "name" = "Change"
            }
        )
    }
}
#EndRegion './Public/Get-PuRequestTypeList.ps1' 20
#Region './Public/Get-PuSources.ps1' 0
function Get-PuSources {
    param (
        [Parameter(Mandatory)][ValidateSet("Ticket","Change")][string]$Type
    )
    
    $Splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-PuEndpoint)/source/"
        "Headers" = @{
            "X-Authorization-Key" = Get-PuAccessToken
            "Accept" = "application/vnd.api+json"
        }
        "Body" = @{
            "filter" = "requestType.Key == `"$Type`""
        }
    }
    $Result = Invoke-RestMethod @Splat
    $Result.sources
}
#EndRegion './Public/Get-PuSources.ps1' 20
#Region './Public/Get-PuStatuses.ps1' 0
function Get-PuStatuses {
    $Splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-PuEndpoint)/status"
        "Headers" = @{
            "X-Authorization-Key" = Get-PuAccessToken
            "Accept" = "application/vnd.api+json"
        }
    }
    $Result = Invoke-RestMethod @Splat
    $Result.statuses
}
#EndRegion './Public/Get-PuStatuses.ps1' 13
#Region './Public/Get-PuTeams.ps1' 0
function Get-PuTeams {
    $Splat = @{
        "Method" = "GET"
        "Uri" = "$(Get-PuEndpoint)/team"
        "Headers" = @{
            "X-Authorization-Key" = Get-PuAccessToken
            "Accept" = "application/vnd.api+json"
        }
    }
    $Result = Invoke-RestMethod @Splat
    $Result.teams
}
#EndRegion './Public/Get-PuTeams.ps1' 13
#Region './Public/Get-PuTicket.ps1' 0
function Get-PuTicket {
    [CmdletBinding(DefaultParameterSetName="List tickets")]
    param (
        [Parameter(Mandatory,ParameterSetName="Get ticket by id")][int]$ticketId,
        [Parameter(Mandatory,ParameterSetName="Get ticket by requestnumber")][int]$requestNumber,
        [string]$filter,
        [Parameter(ParameterSetName="List tickets")][switch]$ListTickets
    )

    $Uri = "$(Get-PuEndpoint)/ticket"

    switch ($PsCmdlet.ParameterSetName) {
        "Get ticket by id" {
            $Uri += "/$ticketId"
        }
        "Get ticket by requestnumber" {
            $Uri += "/$requestNumber/requestNumber/"
        }
    }

    $splat = @{
        "Method" = "GET"
        "Uri" = $Uri
        "Headers" = @{
            "X-Authorization-Key" = Get-PuAccessToken
            "Accept" = "application/vnd.api+json"
        }
        "Body" = @{
            "filter" = $filter
        } | ConvertTo-Json -Depth 99
    }
    $Result = Invoke-RestMethod @splat
    $Result.tickets
}
#EndRegion './Public/Get-PuTicket.ps1' 35
#Region './Public/Get-PuTypes.ps1' 0
function Get-PuTypes {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][string][ValidateSet("Ticket","Change","Asset")]$TypeCategory,
        [string]$filter
    )

    process {
        $Uri = Get-PuEndpoint
        switch ($TypeCategory) {
            "Ticket"{
                $Uri += "/tickettype"
            }
            "Change"{
                $Uri += "/changetype"
            }
            "Asset" {
                $Uri += "/assettype"
            }
        }

        $ParameterExclusion = @("TypeCategory")
        $Body = $null
        $PSBoundParameters.Keys.ForEach({
            [string]$Key = $_
            $Value = $PSBoundParameters.$key
        
            if ($ParameterExclusion -contains $Key) {
                return
            }
        
            $Body = $Body + @{
                $Key = $Value
            }
        })

        $Splat = @{
            "Uri" = $Uri
            "Method" = "GET"
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Accept" = "application/vnd.api+json"
            }
            "Body" = $Body | ConvertTo-Json -Depth 99
        }
        $Result = Invoke-RestMethod @Splat

        switch ($TypeCategory) {
            "Ticket"{
                $Result.tickettypes
            }
            "Change"{
                $Result.changetypes
            }
            "Asset" {
                $Result.assettypes
            }
        }
    }
}
#EndRegion './Public/Get-PuTypes.ps1' 61
#Region './Public/Get-PuUserRoleList.ps1' 0
function Get-PuUserRoleList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                "id" = 0
                "name" = "None"
            },
            [PSCustomObject]@{
                "id" = 1
                "name" = "PendingActivate"
            },
            [PSCustomObject]@{
                "id" = 2
                "name" = "LocationPendingActivate"
            },
            [PSCustomObject]@{
                "id" = 10
                "name" = "Enduser"
            },
            [PSCustomObject]@{
                "id" = 20
                "name" = "Agent"
            },
            [PSCustomObject]@{
                "id" = 25
                "name" = "ZoneAdmin"
            },
            [PSCustomObject]@{
                "id" = 30
                "name" = "Administrator"
            },
            [PSCustomObject]@{
                "id" = 50
                "name" = "System"
            }
        )
    }
}
#EndRegion './Public/Get-PuUserRoleList.ps1' 44
#Region './Public/Get-PuUsers.ps1' 0
function Get-PuUsers {
    <#
    .SYNOPSIS
        Gets users from Pureservice using the Pureservice API.
    .PARAMETER filter
        The input $filter is based on predefined properties which you can find by running a get request of users.
        These aren't documented in the Pureservice API docs.
    .EXAMPLE
        Get-PuUsers -filter 'credentials.username=="navnav01"'
    .EXAMPLE
        Get-PuUsers -filter '(fullName == "Navn Navnesen")'
    #>

    param (
        [string]$filter,
        [int]$limit,
        [string]$sort,
        [string]$include
    )

    process{
        $Uri = "$(Get-PuEndpoint)/user"
        #Parameters to exclude in Uri build
        $ParameterExclusion = @()
        #Build request Uri
        $PSBoundParameters.Keys.ForEach({
            [string]$Key = $_
            $Value = $PSBoundParameters.$key
        
            #Check if parameter is excluded
            if ($ParameterExclusion -contains $Key) {
                return
            }
        
            if ($Value.GetType().BaseType.Name -eq "Array") {
                $Value = $Value -join ","
            }
        
            #Check for "?" in Uri and set delimiter
            if (!($Uri -replace "[^?]+")) {
                $Delimiter = "?"
            } else {
                $Delimiter = "&"
            }
        
            $Uri = "$Uri$Delimiter$Key=$Value"
        })     

        $splat = @{
            "Method" = "GET"
            "Uri" = $Uri
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Accept" = "application/vnd.api+json"
            }
        }
        $Result = Invoke-RestMethod @splat
        $Result.users
    }
}
#EndRegion './Public/Get-PuUsers.ps1' 60
#Region './Public/Get-PuVisibilityList.ps1' 0
function Get-PuVisibilityList {
    [CmdletBinding()]
    param (
        
    )

    process {
        @(
            [PSCustomObject]@{
                "id" = 0
                "name" = "Visible"
            },
            [PSCustomObject]@{
                "id" = 1
                "name" = "VisibleSilent"
            },
            [PSCustomObject]@{
                "id" = 2
                "name" = "NotVisible"
            }
        )
    }
}
#EndRegion './Public/Get-PuVisibilityList.ps1' 24
#Region './Public/New-PuAccessToken.ps1' 0
function New-PuAccessToken {
    param (
        [string]$accessTokenPath = "$env:USERPROFILE\.creds\Pureservice\PureserviceAccessToken.xml"
    )

    $apiKey = Read-Host "Enter Pureservice API key" -AsSecureString

    #Create parent folders of the access token file
    $accessTokenDir = $accessTokenPath.Substring(0, $accessTokenPath.lastIndexOf('\'))
    if (!(Test-Path $accessTokenDir)) {
        New-Item -ItemType Directory $accessTokenDir | Out-Null
    }

    #Create access token file
    $apiKey | Export-Clixml $accessTokenPath
}
#EndRegion './Public/New-PuAccessToken.ps1' 17
#Region './Public/New-PuCommunication.ps1' 0
function New-PuCommunication {
    param (
        [Parameter(Mandatory)][string]$text,
        [Parameter(Mandatory)][int]$type,
        [Parameter(Mandatory)][int]$direction,
        [Parameter(Mandatory)][int]$ticket
    )
    
    process {
        [array]$LinksParams = @(
            "ticket"
        )
    
        $Body = @{
            "links" = @{}
        }
    
        $PSBoundParameters.Keys.ForEach({
            [string]$Key = $_
            $Value = $PSBoundParameters.$key
    
            if ($LinksParams -contains $Key) {
                $Body.links.$Key += @{
                    "id" = $Value
                }
            } else {
                $Body += @{
                    $Key = $Value
                }
            }
        })
    
        $Body = @{
            "communications" = @($Body)
        }
    
        $splat = @{
            "Method" = "POST"
            "Uri" = "$(Get-PuEndpoint)/communication"
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Content-Type" = "application/vnd.api+json"
            }
            "Body" = [System.Text.Encoding]::UTF8.GetBytes(($Body | ConvertTo-Json -Depth 99))
        }
        Invoke-RestMethod @splat
    }
}
#EndRegion './Public/New-PuCommunication.ps1' 49
#Region './Public/New-PuEndpoint.ps1' 0
function New-PuEndpoint {
    param (
        [string]$endpointPath = "$env:USERPROFILE\.creds\Pureservice\PureserviceEndpoint.xml"
    )

    $pureserviceUrl = Read-Host "Enter Pureservice url"
    $endpoint = "$pureserviceUrl/agent/api"

    #Create parent folders of the access token file
    $endpointDir = $endpointPath.Substring(0, $endpointPath.lastIndexOf('\'))
    if (!(Test-Path $endpointDir)) {
        New-Item -ItemType Directory $endpointDir | Out-Null
    }

    #Create access token file
    $endpoint | Export-Clixml $endpointPath
}
#EndRegion './Public/New-PuEndpoint.ps1' 18
#Region './Public/New-PuInternalNote.ps1' 0
#NB: This uses ticketId and not requestId
function New-PuInternalNote {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][int]$ticketId, 
        [Parameter(Mandatory)][string]$text
    )

    process {
        $splat = @{
            "text" = $text
            "type" = (Get-PuCommunicationTypeList).where({$_.name -eq "Note"}).id
            "direction" = (Get-PuCommunicationDirectionList).where({$_.name -eq "None"}).id
            "ticket" = $ticketId
        }
        New-PuCommunication @splat
    }
}
#EndRegion './Public/New-PuInternalNote.ps1' 19
#Region './Public/New-PuTicket.ps1' 0
function New-PuTicket {
    param (
        [Parameter(Mandatory)][string]$subject,
        [Parameter(Mandatory)][string]$description,
        [string]$solution,
        [string]$cf_1,
        [string]$cf_2,
        [string]$cf_3,
        [string]$cf_4,
        [string]$cf_5,
        [string]$cf_6,
        [string]$cf_7,
        [string]$cf_8,
        [string]$cf_9,
        [Parameter(Mandatory)][int]$visibility,
        [string]$customerReference,
        [string]$coordinates,
        [bool]$isMarkedForDeletion,
        [Parameter(Mandatory)][int]$user,
        [Parameter(Mandatory)][int]$ticketType,
        [Parameter(Mandatory)][int]$priority,
        [Parameter(Mandatory)][int]$status,
        [Parameter(Mandatory)][int]$source,
        [Parameter(Mandatory)][int]$assignedDepartment,
        [Parameter(Mandatory)][int]$assignedTeam,
        [Parameter(Mandatory)][int]$assignedAgent,
        [int]$form,
        [int]$category1,
        [int]$category2,
        [int]$category3
    )

    process {
        #Set default params
        $Params = $PSBoundParameters + @{
            "requestType" = (Get-PuRequestTypeList).where({$_.name -eq "Ticket"})[0].id
        }

        [array]$LinksParams = @(
            "requestType",
            "user",
            "ticketType",
            "priority",
            "status",
            "source",
            "assignedDepartment",
            "assignedTeam",
            "assignedAgent",
            "form",
            "category1",
            "category2",
            "category3"
        )
    
        $Body = @{
            "links" = @{}
        }
    
        $Params.Keys.ForEach({
            [string]$Key = $_
            $Value = $Params.$key
    
            if ($LinksParams -contains $Key) {
                $Body.links.$Key += @{
                    "id" = $Value
                }
            } else {
                $Body += @{
                    $Key = $Value
                }
            }
        })
    
        $Body = @{
            "tickets" = @($Body)
        }
    
        $splat = @{
            "Method" = "POST"
            "Uri" = "$(Get-PuEndpoint)/ticket"
            "Headers" = @{
                "X-Authorization-Key" = Get-PuAccessToken
                "Content-Type" = "application/vnd.api+json"
            }
            "Body" = [System.Text.Encoding]::UTF8.GetBytes(($Body | ConvertTo-Json -Depth 99))
        }
        Invoke-RestMethod @splat
    }
}
#EndRegion './Public/New-PuTicket.ps1' 90