src/LanguagePack/Add-XrmLanguagePack.ps1

<#
    .SYNOPSIS
    Activate given language.

    .DESCRIPTION
    Provision a language in the target instance with ProvisionLanguageAsync, then wait for the end of the system job (several minutes, up to an hour depending on the installed solutions).
    Raises an error when the job fails, or when it is not finished after TimeoutInMinutes: the job then keeps running on the platform, check with Get-XrmProvisionedLanguages before starting it again.
    Get-XrmProvisionedLanguages may list the language only a minute after the end of the job. A language already provisioned is provisioned again, without error.

    .PARAMETER XrmClient
    Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient)
    
    .PARAMETER Language
    Language name LCID (English = 1033, French = 1036, ...)

    .PARAMETER TimeoutInMinutes
    Maximum wait for the system job. (Default: 60)

    .OUTPUTS
    Microsoft.Xrm.Sdk.OrganizationResponse. Response of ProvisionLanguageAsync (AsyncOperationId), once the job has succeeded.

    .EXAMPLE
    Add-XrmLanguagePack -XrmClient $xrmClient -Language 1036;

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

function Add-XrmLanguagePack {
    [CmdletBinding(SupportsShouldProcess)]
    [OutputType([Microsoft.Xrm.Sdk.OrganizationResponse])]
    param
    (        
        [Parameter(Mandatory = $false, ValueFromPipeline)]
        [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient]
        $XrmClient = $Global:XrmClient,

        [Parameter(Mandatory = $true)]
        [int]
        $Language,

        [Parameter(Mandatory = $false)]
        [ValidateRange(1, 1440)]
        [int]
        $TimeoutInMinutes = 60
    )
    begin {
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters);       
    }    
    process {
        $provisionLanguageRequest = New-XrmRequest -Name "ProvisionLanguageAsync";
        $provisionLanguageRequest | Add-XrmRequestParameter -Name "Language" -Value $Language | Out-Null;

        $response = $XrmClient | Invoke-XrmRequest -Request $provisionLanguageRequest;
        # Skipped by -WhatIf, or failed (the error is already written)
        if ($null -eq $response) {
            return;
        }
        $XrmClient | Watch-XrmAsynchOperation -AsyncOperationId $response.Results["AsyncOperationId"] -TimeoutInMinutes $TimeoutInMinutes -ThrowOnFailure | Out-Null;
        $response;
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Add-XrmLanguagePack -Alias *;