src/Instances/Get-XrmInstance.ps1

<#
    .SYNOPSIS
    Retrieve instance by name

    .DESCRIPTION
    Get Microsoft Dataverse instance object by its domainname / urlhostname
    
    .PARAMETER Name
    Instance domain name for instance url (myinstance => myinstance.crm.dynamics1.com)

    .OUTPUTS
    PSCustomObject. Instance object (see New-XrmInstance), with the connection string built from the current connection. An error is raised when no instance has this domain name.

    .EXAMPLE
    Connect-XrmAdmin -TenantId $tenantId -ApplicationId $applicationId -ClientSecret $clientSecret;
    $instance = Get-XrmInstance -Name "contoso-dev";
    $xrmClient = New-XrmClient -ConnectionString $instance.ConnectionString;

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Get-XrmInstance.md
#>

function Get-XrmInstance {
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    param
    (
        [Parameter(Mandatory = $true)]
        [String]
        $Name
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {
        $instances = Get-XrmInstances;
        $instance = $instances | Where-Object -Property "Name" -EQ -Value $Name;
        if (-not $instance) {
            throw "Instance $Name not found!";
        }
        $instance;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Get-XrmInstance -Alias *;