Public/Get-RemedySolution.ps1

Function Get-RemedySolution {
<#
.SYNOPSIS
    Retrieves BMC Remedy Solution details via the API by ID number or other specified criteria such as Assignee, Customer, Team.
 
.DESCRIPTION
    This cmdlet queries the Remedy API for Solutions as specified by ID number or by combining one or more of the filter parameters.
    Beware that the Remedy API will return a maximum of 5000 incidents in a single request. If you need to exceed this, make multiple
    requests (e.g by separating by date range) and then combine the results.
 
.EXAMPLE
    Get-RemedySolution -ID 1234567
 
    Returns the specified Solution.
 
.EXAMPLE
    Get-RemedySolution -Status Open -Team Windows
 
    Returns all open solutions for the specified team.
 
.EXAMPLE
    Get-RemedySolution -Status Open -Customer 'Contoso'
 
    Returns all open solutions for the specified customer.
 
.EXAMPLE
    Get-RemedySolution -Status Open -Team Windows -Customer 'Fabrikam'
 
    Returns all open solutions for the specified customer where assigned to the specified team.
 
.EXAMPLE
    Get-RemedySolution -Team Windows -After 01/15/2017 -Before 02/15/2017
 
    Returns all solutions for the specified team between the specified dates.
#>

    [cmdletbinding()]
    Param(
        # One or more Solution ID numbers.
        [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [String[]]$ID = '',
        
        # Solutions assigned to the specified team.
        [String]$Team,
        
        # Solutions raised by the specified customer.
        [String]$Customer,
        
        # Solutions raised for a specific configuration item, e.g a server or other device.
        [Alias('CI')]
        [String]$ConfigurationItem,
        
        # Solutions assigned to the specified individual.
        [String[]]$Assignee,

        # Solutions submitted by the specified individual.
        [String[]]$Submitter,

        # Solutions filtered by specified status. You can also specific 'AllOpen' or 'AllClosed': AllOpen = ('Active'); AllClosed = ('Inactive')
        [ValidateSet('AllOpen','AllClosed','Inactive','Active','')] 
        [String]$Status,
        
        # Include Solutions of one or more specific types: Normal, Standard, Expedited.
        [ValidateSet('Incident','Change','Problem Investigation','Known Error','Solution','Task','CI Unavailability','Purchase Requisition','Release','Activity','')]
        [String[]]$Type,

        # Exclude Solutions of one or more specific types: Normal, Standard, Expedited.
        [ValidateSet('Incident','Change','Problem Investigation','Known Error','Solution','Task','CI Unavailability','Purchase Requisition','Release','Activity','')]
        [String[]]$ExcludeType,

        # Include Solutions of one or more specific priorities: Low, Medium, High, Critical.
        [ValidateSet('Low','Medium','High','Critical','')]
        [String[]]$Priority = '',
        
        # Solutions with a 'submit date' that is after this date. Use US date format: mm/dd/yyyy
        [DateTime]$After,
        
        # Solutions with a 'submit date' that is before this date. Use US date format: mm/dd/yyyy
        [DateTime]$Before,

        # Return all available data fields from Remedy.
        [Switch]$Full,

        # Match the string exactly.
        [Switch]$Exact,

        # An encoded string representing your Remedy Credentials as generated by the Set-RemedyApiConfig cmdlet.
        [String]$EncodedCredentials = (Get-RemedyApiConfig).Credentials,

        # The Remedy API URL. E.g: https://<localhost>:<port>/api
        [String]$APIURL = (Get-RemedyApiConfig).APIURL
    )
    
    If (-not $EncodedCredentials -or -not $APIURL) { Throw 'Remedy API Config not set. Set-RemedyApiConfig first.' }
    
    If (-not (Test-RemedyApiConfig)) { Throw 'Remedy API Test failed. Ensure the config has been set correctly via Set-RemedyApiConfig.' }

    Switch ($Status) {
        'AllOpen'   { $Filter = 'Active' }
        'AllClosed' { $Filter = 'Inactive' }
        Default     { $Filter = $Status }  
    }
    
    If ($Filter)      { $StatusString = ($Filter | ForEach-Object { "('Solution Status'=""$_"")" }) -join 'OR' }
    If ($Type)        { $TypeString = ($Type | ForEach-Object { "('TicketType'=""$_"")" }) -join 'OR' }
    If ($ExcludeType) { $ExcludeTypeString = ($ExcludeType | ForEach-Object { "('TicketType'!=""$_"")" }) -join 'AND' }
    
    If ($Priority) { $PriorityString = ($Priority | ForEach-Object { "('Priority'=""$_"")" }) -join 'OR' }
     
    ForEach ($IDNum in $ID) {
        Write-Verbose "$IDNum"
        
        $Filter = @()
        
        If ($Exact) { $Op = '='; $Wc = '' } Else { $Op = 'LIKE'; $Wc = '%25' }
        If ($Exact -or $IDNum -match '^SDB\d{12}') { $IDOp = '='; $IDWc = '' } Else { $IDOp = 'LIKE'; $IDWc = '%25' }
            
        If ($IDNum)    { $Filter += "'Solution Database ID'$IDOp""$IDWc$IDNum""" }
        If ($Team)     { $Filter += "'Assigned Group'=""$Team""" }
        If ($Customer) { $Filter += "'Company'$Op""$Wc$Customer$Wc""" }
        If ($ConfigurationItem) { $Filter += "'SD_CI'$Op""$Wc$ConfigurationItem$Wc""" }
        
        If ($Assignee -is [Array]) { 
            $AssigneeString = ($Assignee | ForEach-Object { "('Assignee'$Op""$Wc$_$Wc"")" }) -join 'OR'
            $Filter += "($AssigneeString)"
        } ElseIf ($Assignee -is [String]) {
             $Filter += "'Assignee'$Op""$Wc$Assignee$Wc""" 
        }
        
        If ($Submitter -is [Array]) { 
            $SubmitterString = ($Submitter | ForEach-Object { "('Submitter'$Op""$Wc$_$Wc"")" }) -join 'OR'
            $Filter += "($SubmitterString)"
        } ElseIf ($Submitter -is [String]) {
             $Filter += "'Submitter'$Op""$Wc$Submitter$Wc""" 
        }
         
        If ($Type)          { $Filter += "($TypeString)" }
        If ($ExcludeType)   { $Filter += "($ExcludeTypeString)" }
        
        If ($Priority)      { $Filter += "($PriorityString)" }
        
        If ($After)  { $Filter += "'Submit Date'>""$($After.ToString("yyyy-MM-dd"))""" }
        If ($Before) { $Filter += "'Submit Date'<""$($Before.ToString("yyyy-MM-dd"))""" }

        If ($StatusString) { $Filter += "($StatusString)" }
        $FilterString = $Filter -Join 'AND'

        $Headers = @{
            Authorization = "Basic $EncodedCredentials"
        }

        If (-not $FilterString) { Throw 'Please provide at least one search criteria. Enter Help Get-RemedySolution for further guidance.' }

        $URL = "$APIURL/PBM:Solution Database/$FilterString"
    
    
        Try {
            $Result = Invoke-RestMethod -URI $URL -Headers $Headers -ErrorAction Stop
            
            $Solutions = @()
            $Result.PSObject.Properties | ForEach-Object { $Solutions += $_.Value }
            
            #Convert all date containing fields to PS datetime
            ForEach ($Solution in $Solutions) { 
                
                $Solution.PSObject.Properties.Name -like '* Date*' | ForEach-Object {
                        
                    If ($Solution.$_ -match 'UTC'){
                        $Solution.$_ = [datetime]::ParseExact(($Solution.$_ -Replace 'UTC ',''), 'ddd MMM dd HH:mm:ss yyyy', $null)
                    }
                }
            }
                                
            #Could replace this with a format.ps1.xml
            If (-not $Full){
                $Solutions = $Solutions |  Select-Object 'Solution Database ID','Company','Abstract','Solution','Solution Status',
                                                         'Assigned Group','Assignee',@{N='CI';E={$_.SD_CI}},'Submit Date',
                                                         'Last Modified Date','Last Modified By'
            }
        } Catch {
            
            Write-Error "Error: $_"
        }

        If ($null -ne $Solutions.'Solution Database ID') {
            $Solutions.psobject.TypeNames.Insert(0, 'RemedySolution')
            $Solutions
        } Else {
            Write-Verbose 'No Solution data returned'
        }
    }
}