src/Connection/New-XrmConnection.ps1
|
<# .SYNOPSIS Initialize new object that represent a Dataverse Connection. .DESCRIPTION Build a connection object without calling Dataverse: Name, Url, AuthType, Credentials, UserName, Password, TenantId, Region, ApplicationId, ClientSecret, CertificateThumbprint, Instances and DevOpsSettings (see New-XrmDevOpsSettings), all empty. With ConnectionString, Url, AuthType, UserName, Password, ApplicationId (ClientId), ClientSecret and CertificateThumbprint (Thumbprint) are read from it. .PARAMETER ConnectionString Dataverse connection string to read the connection settings from. (Default: none, the settings stay empty) .OUTPUTS PSCustomObject. The connection object. .EXAMPLE $connection = New-XrmConnection -ConnectionString "AuthType=ClientSecret;Url=https://contoso.crm.dynamics.com;ClientId=$clientId;ClientSecret=$clientSecret"; $connection.Url; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/New-XrmConnection.md #> function New-XrmConnection { [CmdletBinding()] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [String] $ConnectionString ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $hash = @{ }; $hash["Name"] = [String]::Empty; $hash["Url"] = [String]::Empty; $hash["AuthType"] = [String]::Empty; $hash["Credentials"] = [System.Management.Automation.PSCredential]::Empty; $hash["UserName"] = [String]::Empty; $hash["Password"] = [String]::Empty; $hash["TenantId"] = [String]::Empty; $hash["Region"] = [String]::Empty; $hash["ApplicationId"] = [String]::Empty; $hash["ClientSecret"] = [String]::Empty; $hash["CertificateThumbprint"] = [String]::Empty; $hash["Instances"] = $null; $hash["DevOpsSettings"] = New-XrmDevOpsSettings; $object = New-Object PsObject -Property $hash; if ($PSBoundParameters.ContainsKey('ConnectionString')) { $object.Url = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "Url"; $object.AuthType = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "AuthType"; $object.UserName = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "UserName"; $object.Password = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "Password"; $object.ApplicationId = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "ClientId"; $object.ClientSecret = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "ClientSecret"; $object.CertificateThumbprint = $ConnectionString | Out-XrmConnectionStringParameter -ParameterName "Thumbprint"; } $object; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function New-XrmConnection -Alias *; |