Skip to content

Commit

Permalink
Merge pull request #4 from d365collaborative/development
Browse files Browse the repository at this point in the history
Next version
  • Loading branch information
Splaxi authored Mar 3, 2024
2 parents fbf444b + b523742 commit a7a6b11
Show file tree
Hide file tree
Showing 11 changed files with 758 additions and 0 deletions.
4 changes: 4 additions & 0 deletions d365bap.tools/d365bap.tools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@
'Compare-BapEnvironmentD365App'
, 'Compare-BapEnvironmentUser'

, 'Confirm-BapEnvironmentIntegration'

, 'Get-BapEnvironment'
, 'Get-BapEnvironmentApplicationUser'

, 'Get-BapEnvironmentD365App'

, 'Get-BapEnvironmentUser'

, 'Get-BapEnvironmentVirtualEntity'

, 'Invoke-BapEnvironmentInstallD365App'
)
Expand Down
88 changes: 88 additions & 0 deletions d365bap.tools/functions/Confirm-BapEnvironmentIntegration.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

<#
.SYNOPSIS
Test the integration status
.DESCRIPTION
Invokes the validation of the PowerPlatform integration, from the Dataverse perspective
If it returns an output, the Dataverse is fully connected to the D365FO environment
.PARAMETER EnvironmentId
The id of the environment that you want to work against
.PARAMETER AsExcelOutput
Instruct the cmdlet to output all details directly to an Excel file
This makes it easier to deep dive into all the details returned from the API, and makes it possible for the user to persist the current state
.EXAMPLE
PS C:\> Confirm-BapEnvironmentIntegration -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6
This will invoke the validation from the Dataverse environment.
It will only output details if the environment is fully connected and working.
Sample output:
LinkedAppLcsEnvId LinkedAppLcsEnvUri IsUnifiedDatabase TenantId
----------------- ------------------ ----------------- --------
0e52661c-0225-4621-b1b4-804712cf6d9a https://new-test.sandbox.operations.eu.dynamics.c… False 8ccb796b-37b…
.EXAMPLE
PS C:\> Confirm-BapEnvironmentIntegration -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -AsExcelOutput
This will invoke the validation from the Dataverse environment.
It will only output details if the environment is fully connected and working.
Will output all details into an Excel file, that will auto open on your machine.
The excel file will be empty if the integration isn't working.
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Confirm-BapEnvironmentIntegration {
[CmdletBinding()]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,

[switch] $AsExcelOutput
)

begin {
# Make sure all *BapEnvironment* cmdlets will validate that the environment exists prior running anything.
$envObj = Get-BapEnvironment -EnvironmentId $EnvironmentId | Select-Object -First 1

if ($null -eq $envObj) {
$messageString = "The supplied EnvironmentId: <c='em'>$EnvironmentId</c> didn't return any matching environment details. Please verify that the EnvironmentId is correct - try running the <c='em'>Get-BapEnvironment</c> cmdlet."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because environment was NOT found based on the id." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if (Test-PSFFunctionInterrupt) { return }

$baseUri = $envObj.LinkedMetaPpacEnvUri
$tokenWebApi = Get-AzAccessToken -ResourceUrl $baseUri
$headersWebApi = @{
"Authorization" = "Bearer $($tokenWebApi.Token)"
}
}

process {
$resValidate = Invoke-RestMethod -Method Get -Uri $($baseUri + '/api/data/v9.2/RetrieveFinanceAndOperationsIntegrationDetails') -Headers $headersWebApi

$temp = $resValidate | Select-PSFObject -TypeName "D365Bap.Tools.Environment.Integration" -ExcludeProperty "@odata.context" -Property "Id as LinkedAppLcsEnvId",
"Url as LinkedAppLcsEnvUri",
*

if ($AsExcelOutput) {
$temp | Export-Excel
return
}

$temp
}

end {

}
}
192 changes: 192 additions & 0 deletions d365bap.tools/functions/Get-BapEnvironmentVirtualEntity.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@

<#
.SYNOPSIS
Get Virutal Entity
.DESCRIPTION
Enables the user to query against the Virtual Entities from the D365FO environment
This will help determine which Virtual Entities are already enabled / visible and their state
.PARAMETER EnvironmentId
The id of the environment that you want to work against
.PARAMETER Name
The name of the virtual entity that you are looking for
The parameter supports wildcards, but will resolve them into a strategy that matches best practice from Microsoft documentation
It means that you can only have a single search phrase. E.g.
* -Name "*Retail"
* -Name "Retail*"
* -Name "*Retail*"
Multiple search phrases are not going to produce an output, as it will be striped into an invalid search string. E.g.
! -Name "*Retail*Entity*" -> "RetailEntity"
! -Name "Retail*Entity" -> "RetailEntity"
! -Name "*Retail*Entity" -> "RetailEntity"
! -Name "Retail*Entity*" -> "RetailEntity"
.PARAMETER VisibleOnly
Instruct the cmdlet to only output those virtual entities that are enabled / visible
.PARAMETER AsExcelOutput
Instruct the cmdlet to output all details directly to an Excel file
This makes it easier to deep dive into all the details returned from the API, and makes it possible for the user to persist the current state
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6
This will fetch all virtual entities from the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
AadWorkerIntegrationEntity False False 00002893-0000-0000-560a-005001000000
AbbreviationsEntity False False 00002893-0000-0000-5002-005001000000
AccountantEntity False False 00002893-0000-0000-0003-005001000000
AccountingDistributionBiEntity False False 00002893-0000-0000-d914-005001000000
AccountingEventBiEntity False False 00002893-0000-0000-d414-005001000000
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -VisibleOnly
This will fetch visble only virtual entities from the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
CurrencyEntity True False 00002893-0000-0000-c30b-005001000000
WMHEOutboundQueueEntity True False 00002893-0000-0000-f30b-005001000000
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -AsExcelOutput
This will fetch all virtual entities from the environment.
Will output all details into an Excel file, that will auto open on your machine.
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name "Retail*"
This will fetch all virtual entities that contains the "Retail" text in the name, from the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
CustHierarchyRetailChannelEnt… False False 00002893-0000-0000-e314-005001000000
DimAttributeRetailChannelEnti… False False 00002893-0000-0000-0804-005001000000
DimAttributeRetailStoreEntity False False 00002893-0000-0000-0f03-005001000000
DimAttributeRetailTerminalEnt… False False 00002893-0000-0000-6e07-005001000000
EcoResRetailProductEntity False False 00002893-0000-0000-ae06-005001000000
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Get-BapEnvironmentVirtualEntity {
[CmdletBinding()]
[OutputType('System.Object[]')]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,

[string] $Name = "*",

[switch] $VisibleOnly,

[switch] $AsExcelOutput
)

begin {
# Make sure all *BapEnvironment* cmdlets will validate that the environment exists prior running anything.
$envObj = Get-BapEnvironment -EnvironmentId $EnvironmentId | Select-Object -First 1

if ($null -eq $envObj) {
$messageString = "The supplied EnvironmentId: <c='em'>$EnvironmentId</c> didn't return any matching environment details. Please verify that the EnvironmentId is correct - try running the <c='em'>Get-BapEnvironment</c> cmdlet."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because environment was NOT found based on the id." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if (Test-PSFFunctionInterrupt) { return }

$baseUri = $envObj.LinkedMetaPpacEnvUri
$tokenWebApi = Get-AzAccessToken -ResourceUrl $baseUri
$headersWebApi = @{
"Authorization" = "Bearer $($tokenWebApi.Token)"
}

# Fetch all meta data - for all entities in the environment
$entitiesMetaRaw = Invoke-RestMethod -Method Get -Uri $($baseUri + '/api/data/v9.2/entities') -Headers $headersWebApi
$templateMeta = $entitiesMetaRaw.value[0]

# Filter down to only those who are connected to Virtual Entities
$entitiesMeta = $entitiesMetaRaw.value | Where-Object { -not [System.String]::IsNullOrEmpty($_.externalname) }
}

process {
$localUri = $($baseUri + '/api/data/v9.2/mserp_financeandoperationsentities')

[System.Collections.Generic.List[System.String]]$filters = @()

# Is the user search for specific entities?
if ($Name -ne "*") {
if ($Name.IndexOfAny("*") -gt -1) {
# It is a wildcard search
$filters.Add("contains(mserp_physicalname, '$($Name.Replace('*',''))')")
}
else {
# It is a full named search
$filters.Add("mserp_physicalname eq '$Name'")
}
}

if ($VisibleOnly) {
$filters.Add("mserp_hasbeengenerated eq true")
}

if ($filters.count -gt 0) {
# We need to handle multiple filters
$localUri += '?$filter='
$localUri += $($filters.ToArray() -join " and ")
}

$resEntities = Invoke-RestMethod -Method Get -Uri $localUri -Headers $headersWebApi

$resCol = @(
foreach ($virEntity in $($resEntities.value | Sort-Object -Property mserp_physicalname)) {

$tempMeta = $entitiesMeta | Where-Object externalname -eq $virEntity.mserp_physicalname | Select-Object -First 1

if ($null -ne $tempMeta) {
# Work against the meta data found
foreach ($prop in $tempMeta.PsObject.Properties) {
$virEntity | Add-Member -MemberType NoteProperty -Name "meta_$($prop.Name)" -Value $prop.Value
}
}
else {
# Create empty properties for those who doesn't have meta data available
foreach ($prop in $templateMeta.PsObject.Properties) {
$virEntity | Add-Member -MemberType NoteProperty -Name "meta_$($prop.Name)" -Value $null
}
}

$virEntity | Select-PSFObject -TypeName "D365Bap.Tools.VirtualEntity" -Property "mserp_physicalname as EntityName",
"mserp_hasbeengenerated as IsVisible",
"mserp_changetrackingenabled as ChangeTrackingEnabled",
"mserp_financeandoperationsentityid as EntityGuid",
*
}
)

if ($AsExcelOutput) {
$resCol | Export-Excel
return
}

$resCol
}

end {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Describe "Confirm-BapEnvironmentIntegration Unit Tests" -Tag "Unit" {
BeforeAll {
# Place here all things needed to prepare for the tests
}
AfterAll {
# Here is where all the cleanup tasks go
}

Describe "Ensuring unchanged command signature" {
It "should have the expected parameter sets" {
(Get-Command Confirm-BapEnvironmentIntegration).ParameterSets.Name | Should -Be '__AllParameterSets'
}

It 'Should have the expected parameter EnvironmentId' {
$parameter = (Get-Command Confirm-BapEnvironmentIntegration).Parameters['EnvironmentId']
$parameter.Name | Should -Be 'EnvironmentId'
$parameter.ParameterType.ToString() | Should -Be System.String
$parameter.IsDynamic | Should -Be $False
$parameter.ParameterSets.Keys | Should -Be '__AllParameterSets'
$parameter.ParameterSets.Keys | Should -Contain '__AllParameterSets'
$parameter.ParameterSets['__AllParameterSets'].IsMandatory | Should -Be $True
$parameter.ParameterSets['__AllParameterSets'].Position | Should -Be 0
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipeline | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipelineByPropertyName | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromRemainingArguments | Should -Be $False
}
It 'Should have the expected parameter AsExcelOutput' {
$parameter = (Get-Command Confirm-BapEnvironmentIntegration).Parameters['AsExcelOutput']
$parameter.Name | Should -Be 'AsExcelOutput'
$parameter.ParameterType.ToString() | Should -Be System.Management.Automation.SwitchParameter
$parameter.IsDynamic | Should -Be $False
$parameter.ParameterSets.Keys | Should -Be '__AllParameterSets'
$parameter.ParameterSets.Keys | Should -Contain '__AllParameterSets'
$parameter.ParameterSets['__AllParameterSets'].IsMandatory | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].Position | Should -Be -2147483648
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipeline | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromPipelineByPropertyName | Should -Be $False
$parameter.ParameterSets['__AllParameterSets'].ValueFromRemainingArguments | Should -Be $False
}
}

Describe "Testing parameterset __AllParameterSets" {
<#
__AllParameterSets -EnvironmentId
__AllParameterSets -EnvironmentId -AsExcelOutput
#>
}

}
Loading

0 comments on commit a7a6b11

Please sign in to comment.