Functions/Client/Set-CdsClientTimeout.ps1

<#
    .SYNOPSIS
    Specify CrmserviceClient timeout.
#>

function Set-CdsClientTimeout {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, ValueFromPipeline)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]
        $CdsClient = $Global:CdsClient,
        
        [Parameter(Mandatory = $false)]
        [Switch]
        $Revert
    )
    dynamicparam {
        $dynamicParameters = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameterDictionary;
             
        # Define Dynamic Parameter : Duration
        $durationParameterAttributes = New-Object -TypeName System.Collections.ObjectModel.Collection[System.Attribute];
        $durationParameterAttribute = New-Object System.Management.Automation.ParameterAttribute;
        $durationParameterAttribute.Mandatory = (-not $Revert);
        $durationParameterAttributes.Add($durationParameterAttribute);       
        $durationParameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter("DurationInMinutes", [int], $durationParameterAttributes);
        $dynamicParameters.Add("DurationInMinutes", $durationParameter);                       
              
        return $dynamicParameters;    
    }
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
    }    
    process {

        if(-not $CdsClient.OrganizationServiceProxy)
        {
            return;
        }
        
        $durationInMinutes = $PSBoundParameters.DurationInMinutes;
        if ($durationInMinutes) {            
            $Global:CdsClientInitialTimeout = $CdsClient.OrganizationServiceProxy.Timeout.TotalMinutes;
            $CdsClient.OrganizationServiceProxy.Timeout = (New-TimeSpan -Minutes $durationInMinutes); 
        }

        if ($Revert) {
            $CdsClient.OrganizationServiceProxy.Timeout = (New-TimeSpan -Minutes $Global:CdsClientInitialTimeout); 
        }
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Set-CdsClientTimeout -Alias *;