Public/Find-SonarrSeries.ps1

function Find-SonarrSeries
{
    <#
        .SYNOPSIS
            Search to find a series in order to add to Sonarr.

        .DESCRIPTION
            This uses the lookup service within Sonarr to search for a series by name, TVDB ID, or IMDB ID.
            It does not search your local Sonarr library, but rather The Movie Database (TMDb).

        .PARAMETER Name
            The name of the series to search for.

        .PARAMETER TMDBID
            The TMDB ID of the series to search for.

        .PARAMETER TVDBID
            The TVDB ID of the series to search for.

        .PARAMETER IMDBID
            The IMDB ID of the series to search for.

        .EXAMPLE
            Find-SonarrSeries -Name "The Matrix"

        .NOTES
            If you have the IMDB ID or TVDB ID of a series, it's better to use this to search.
    #>


    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true, ParameterSetName = 'Name')]
        [String]$Name,

        [Parameter(Mandatory = $false, ParameterSetName = 'Name')]
        [Switch]$ExactMatch,

        [Parameter(Mandatory = $true, ParameterSetName = 'IMDBID')]
        [ValidatePattern('^(tt)?\d{5,9}$')]
        [String]$IMDBID,

        [Parameter(Mandatory = $true, ParameterSetName = 'TMDBID')]
        [String]$TMDBID,

        [Parameter(Mandatory = $true, ParameterSetName = 'TVDBID')]
        [String]$TVDBID
    )

    ####################################################################################################
    #Region Import configuration
    try
    {
        Import-Configuration -ErrorAction Stop
    }
    catch
    {
        throw $_
    }
    #EndRegion


    ####################################################################################################
    # If using IMDB, ensure the ID is in the correct format
    if($PSCmdlet.ParameterSetName -eq 'IMDBID' -and $IMDBID -notmatch '^tt')
    {
        $IMDBID = 'tt' + $IMDBID
    }


    ####################################################################################################
    #Region Define the parameters
    try
    {
        if($Name)
        {
            $Params = @{
                term = $Name
            }
        }
        elseif($IMDBID)
        {
            $Params = @{
                term = "imdb:$IMDBID"
            }
        }
        elseif($TMDBID)
        {
            $Params = @{
                term = "tmdb:$TMDBID"
            }
        }
        elseif($TVDBID)
        {
            $Params = @{
                term = "tvdb:$TVDBID"
            }
        }
        else
        {
            throw 'You must specify a name, TVDBID, or IMDBID.'
        }
    }
    catch
    {
        throw $_
    }
    #EndRegion


    ####################################################################################################
    #Region make the main request
    try
    {
        $Data = Invoke-SonarrRequest -Path '/series/lookup' -Method GET -Params $Params -ErrorAction Stop
        if($Data)
        {
            # If ExactMatch is specified, filter the results to only include the exact match
            if($ExactMatch)
            {
                $Data = $Data | Where-Object { $_.title -eq $Name }
            }

            # When searching by a specific ID, filter to ensure only the correct ID type matches
            # This prevents false positives where the searched ID matches a different ID field
            if($TMDBID)
            {
                $Data = $Data | Where-Object { $_.tmdbId -eq [int]$TMDBID }
            }
            elseif($TVDBID)
            {
                $Data = $Data | Where-Object { $_.tvdbId -eq [int]$TVDBID }
            }
            elseif($IMDBID)
            {
                $Data = $Data | Where-Object { $_.imdbId -eq $IMDBID }
            }

            # If the parameterset matches "ID" and there is more than 1 result, throw an error
            if($PSCmdlet.ParameterSetName -match 'ID' -and $Data.Count -gt 1)
            {
                throw "Multiple series found for the provided ID. This should not happen!"
            }

            return $Data
        }
        else
        {
            Write-Warning -Message "No series found."
            return
        }
    }
    catch
    {
        throw $_
    }
    #EndRegion
}