Public/Get-RemedyWorkLog.ps1

Function Get-RemedyWorkLog {
<#
.SYNOPSIS
    Retrieves BMC Remedy Work Log details via the API by ID number or other specified criteria such as Team, Submitter, Note, Title.
.DESCRIPTION
    This cmdlet queries the Remedy API for Incidents 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-RemedyWorkLog -ID 2149423
.EXAMPLE
    Get-RemedyWorkLog -Team Windows -After 01/15/2016 -Before 02/15/2016
#>

    [cmdletbinding()]
    Param(
        #One or more Incident ID numbers.
        [Parameter(Position=0,ValueFromPipelineByPropertyName=$true)]
        [Alias('Incident Number')]
        [String[]]$ID = '',
        
        #Work log entries created by the specified team.
        [String]$Team,
        
        #Work log entries with specific text in the work note title.
        [String]$Title,
        
        #Work log entries with specific text in the work note text.
        [String]$Note,
        
        #Work log entries submitted by the specified individual.
        [String]$Submitter,

        #Work log entries with a 'submit date' that is after this date. Use US date format: mm/dd/yyyy
        [DateTime]$After,
        
        #Work log entries 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,

        #Exact match.
        [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
    )
    Begin {
        If (-not (Test-RemedyApiConfig)) { Throw 'Remedy API Test failed. Ensure the config has been set correctly via Set-RemedyApiConfig.' }
    }
    Process {
        ForEach ($IDNum in $ID) {
            Write-Verbose "$IDNum"
        
            $Filter = @()
            
            If ($Exact) { $Op = '='; $Wc = '' } Else { $Op = 'LIKE'; $Wc = '%25' }
            
            If ($IDNum)    { $Filter += "'Incident Number'$Op""$Wc$IDNum""" }
            If ($Team)     { $Filter += "'Assigned Group'=""$Team""" }
            If ($Title)    { $Filter += "'Description'$Op""$Wc$Title$Wc""" }
            If ($Note)     { $Filter += "'Detailed Description'$Op""$Wc$Note$Wc""" }
            If ($Submitter){ $Filter += "'Submitter'$Op""$Wc$Submitter$Wc""" }
        
            If ($After)    { $Filter += "'Submit Date'>""$($After.ToString("yyyy-MM-dd"))""" }
            If ($Before)   { $Filter += "'Submit Date'<""$($Before.ToString("yyyy-MM-dd"))""" }

            $FilterString = $Filter -Join 'AND'

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

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

            $URL = "$APIURL/HPD:WorkLog/$FilterString"
    
    
            Try {
                $Result = Invoke-RestMethod -URI $URL -Headers $Headers -ErrorAction Stop

                $Tickets = @()
                $Result.PSObject.Properties | ForEach-Object { $Tickets += $_.Value }
        
                #Convert all date containing fields to PS datetime
                
                ForEach ($Ticket in $Tickets) { 
                    $Ticket.PSObject.Properties.Name -like '* Date*' | ForEach-Object {
                        #$DateString = $Ticket.$_
                        If ($Ticket.$_ -match 'UTC'){
                            $Ticket.$_ = [datetime]::ParseExact(($Ticket.$_ -Replace 'UTC ',''),'ddd MMM dd HH:mm:ss yyyy',$null)
                        }
                    }
                }
                
                <#Replace this with a format.ps1.xml#>
                If (-not $Full){
                    $Tickets = $Tickets | 
                        Select-Object 'Submit Date','Detailed Description',Submitter,Company,
                                      'Work Log Type','View Access','Assigned Group'
                }

            } Catch {
                Write-Error "Error: $_"
            }

            Write-Output $Tickets
        }
    }
}