src/Types/New-XrmContext.ps1
|
<# .SYNOPSIS Initialize new object that represent a Xrm Context. .DESCRIPTION Core module cmdlet that create new object to store context information. The connection details (URL, organization, online or on-premises, user id read with a WhoAmI request) are only filled when XrmClient is passed explicitly. New-XrmClient stores its result in $Global:XrmContext. .PARAMETER XrmClient Xrm connector initialized to target instance. Use latest one by default. (Dataverse ServiceClient) .PARAMETER ConnectionString Connection string whose parameters (Url, AuthType, UserName, ClientId...) fill the CurrentConnection property (see New-XrmConnection). .OUTPUTS PSCustomObject. Context with UserId, IsOnPremise, IsOnline, IsAdminConnected, IsUserConnected, IsDevOps, CurrentConnection, CurrentInstance, CurrentUrl and IsEncrypted. .EXAMPLE $xrmClient = New-XrmClient -ConnectionString $connectionString; $context = New-XrmContext -XrmClient $xrmClient -ConnectionString $connectionString; $isOnline = $context.IsOnline; .LINK https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/New-XrmContext.md #> function New-XrmContext { [CmdletBinding()] [OutputType([PSCustomObject])] param ( [Parameter(Mandatory = $false)] [Microsoft.PowerPlatform.Dataverse.Client.ServiceClient] $XrmClient = $Global:XrmClient, [Parameter(Mandatory = $false)] [String] [ValidateNotNullOrEmpty()] $ConnectionString ) begin { $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); } process { $hash = @{ }; $hash["UserId"] = [guid]::Empty; $hash["IsOnPremise"] = $false; $hash["IsOnline"] = $false; $hash["IsAdminConnected"] = $false; $hash["IsUserConnected"] = $false; $hash["IsDevOps"] = ($null -ne $ENV:SYSTEM_COLLECTIONID); $hash["CurrentConnection"] = New-XrmConnection; $hash["CurrentInstance"] = $null; $hash["CurrentUrl"] = $null; $hash["IsEncrypted"] = $false; $object = New-Object PsObject -Property $hash; if ($PSBoundParameters.ContainsKey('ConnectionString')) { $object.CurrentConnection = New-XrmConnection -ConnectionString $ConnectionString; } if ($PSBoundParameters.ContainsKey('XrmClient')) { $object.IsUserConnected = $XrmClient.IsReady; $object.CurrentInstance = $XrmClient.OrganizationDetail; $object.CurrentUrl = $XrmClient.ConnectedOrgPublishedEndpoints["WebApplication"]; $object.IsOnline = $object.CurrentUrl.Contains('.dynamics.com'); $object.IsOnPremise = -not $object.IsOnline; $object.CurrentConnection.TenantId = $XrmClient.TenantId; $object.CurrentConnection.Name = $XrmClient.OrganizationDetail.UrlName; $object.CurrentConnection.Region = $XrmClient.OrganizationDetail.Geo; $userId = Get-XrmWhoAmI -XrmClient $XrmClient; $object.UserId = $userId; } $object; } end { $StopWatch.Stop(); Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch; } } Export-ModuleMember -Function New-XrmContext -Alias *; |