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 {       
        
        $durationInMinutes = $PSBoundParameters.DurationInMinutes;
        if ($durationInMinutes) {            

            if(-not $CdsClient.OrganizationServiceProxy)  {
                $Global:CdsClientInitialTimeout = $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.ReceiveTimeout.TotalMinutes;                
                
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.OpenTimeout = (New-TimeSpan -Minutes $durationInMinutes);     
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.SendTimeout = (New-TimeSpan -Minutes $durationInMinutes);  
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.ReceiveTimeout = (New-TimeSpan -Minutes $durationInMinutes);   
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.CloseTimeout = (New-TimeSpan -Minutes $durationInMinutes);   
 
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.OpenTimeout = (New-TimeSpan -Minutes $durationInMinutes);     
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.SendTimeout = (New-TimeSpan -Minutes $durationInMinutes);  
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.ReceiveTimeout = (New-TimeSpan -Minutes $durationInMinutes);   
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.CloseTimeout = (New-TimeSpan -Minutes $durationInMinutes);        
            }
            else {
                $Global:CdsClientInitialTimeout = $CdsClient.OrganizationServiceProxy.Timeout.TotalMinutes;
                $CdsClient.OrganizationServiceProxy.Timeout = (New-TimeSpan -Minutes $durationInMinutes); 
            }
        }

        if ($Revert) {
            $initialTimeOutMinutes = $Global:CdsClientInitialTimeout;

            if(-not $CdsClient.OrganizationServiceProxy)  {          
                
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.OpenTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);     
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.SendTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);  
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.ReceiveTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);   
                $CdsClient.OrganizationWebProxyClient.Endpoint.Binding.CloseTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);   
 
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.OpenTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);     
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.SendTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);  
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.ReceiveTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);   
                $CdsClient.OrganizationWebProxyClient.ChannelFactory.Endpoint.Binding.CloseTimeout = (New-TimeSpan -Minutes $initialTimeOutMinutes);               
            }
            else {
                $CdsClient.OrganizationServiceProxy.Timeout = (New-TimeSpan -Minutes $initialTimeOutMinutes); 
            }
        }
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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