Functions/Client/New-CdsClient.ps1
<#
.SYNOPSIS Initialize CrmserviceClient instance. #> function New-CdsClient { [CmdletBinding()] [OutputType("Microsoft.Xrm.Tooling.Connector.CrmServiceClient")] param ( # https://docs.microsoft.com/fr-fr/powerapps/developer/common-data-service/xrm-tooling/use-connection-strings-xrm-tooling-connect [Parameter(Mandatory = $true)] [String] $ConnectionString, [Parameter(Mandatory = $false)] [int] $MaxCrmConnectionTimeOutMinutes = 2 ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { # Optimizations [System.Net.ServicePointManager]::Expect100Continue = $false; [System.Net.ServicePointManager]::UseNagleAlgorithm = $false; [System.Net.ServicePointManager]::DefaultConnectionLimit = 1000; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; # Initialize CRM Client $CdsClient = Get-CrmConnection -ConnectionString $ConnectionString -MaxCrmConnectionTimeOutMinutes $MaxCrmConnectionTimeOutMinutes -ErrorAction Stop; if ($CdsClient.IsReady -eq $false) { throw $CdsClient.LastCrmError; } $Global:CdsClient = $CdsClient; $url = $CdsClient.ConnectedOrgPublishedEndpoints["WebApplication"]; $userId = $CdsClient.GetMyCrmUserId(); # Store current settings to context as connection could be initiated with a simple connectionstring and we need thoose parameters for admin operations $userName = $ConnectionString | Out-CdsConnectionStringParameter -ParameterName "Username"; if(-not $userName) { $userName = $userId; } # TODO : Review context definition on CrmServiceClient setup # $userPassword = $ConnectionString | Out-CdsConnectionStringParameter -ParameterName "Password"; # # $Global:CdsContext = New-CdsContext; # $Global:CdsContext.UserId = $userId; # if ($userName -and $userPassword) { # $Global:CdsContext.Credentials = Set-CdsCredentials -Login $userName -Password $userPassword; # } # $Global:CdsContext.IsOnPremise = ($CdsClient.TenantId -eq [Guid]::Empty); # $Global:CdsContext.IsOnline = (-not $Global:CdsContext.IsOnPremise); # # $cdsConnection = New-CdsConnection; # $cdsConnection.AuthType = $CdsClient.ActiveAuthenticationType.ToString(); # $cdsConnection.UserName = $userName; # $cdsConnection.UserPassword = $userPassword; # $Global:CdsContext.CurrentConnection = $cdsConnection; # # $cdsInstance = New-CdsInstance; # $cdsInstance.Id = $CdsClient.ConnectedOrgId; # $cdsInstance.DisplayName = $CdsClient.ConnectedOrgFriendlyName; # $cdsInstance.UniqueName = $CdsClient.ConnectedOrgUniqueName; # $cdsInstance.Url = $url; # $cdsInstance.TenantId = $CdsClient.TenantId; # $cdsInstance.EnviromentId = $CdsClient.EnvironmentId; # $Global:CdsContext.CurrentInstance = $cdsInstance; Write-HostAndLog -Message "Connected to $($CdsClient.ConnectedOrgFriendlyName)! [Url = $url | User : $userName]" -ForegroundColor Yellow; $CdsClient; } end { $StopWatch.Stop(); Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function New-CdsClient -Alias *; |