Skip to content

Commit

Permalink
Feature: Implement the Set-BapEnvironmentVirtualEntity cmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
Splaxi committed Mar 3, 2024
1 parent 0756f9b commit 1f3a85d
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 14 deletions.
2 changes: 2 additions & 0 deletions d365bap.tools/d365bap.tools.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
, 'Get-BapEnvironmentVirtualEntity'

, 'Invoke-BapEnvironmentInstallD365App'

, 'Set-BapEnvironmentVirtualEntity'
)

# Cmdlets to export from this module
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Compare-BapEnvironmentD365App.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Compare environment D365 Apps
.DESCRIPTION
This enables the user to compare 2 x environments, with one as a source and the other as a destination
Enables the user to compare 2 x environments, with one as a source and the other as a destination
It will only look for installed D365 Apps on the source, and use this as a baseline against the destination
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Compare-BapEnvironmentUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Compare the environment users
.DESCRIPTION
This enables the user to compare 2 x environments, with one as a source and the other as a destination
Enables the user to compare 2 x environments, with one as a source and the other as a destination
It will only look for users on the source, and use this as a baseline against the destination
Expand Down
3 changes: 2 additions & 1 deletion d365bap.tools/functions/Get-BapEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Get environment info
.DESCRIPTION
This enables the user to query and validate all environments that are available from inside PPAC
Enables the user to query and validate all environments that are available from inside PPAC
It utilizes the "https://api.bap.microsoft.com" REST API
Expand Down Expand Up @@ -103,6 +103,7 @@ function Get-BapEnvironment {
@{Name = "LinkedMetaPpacOrgId"; Expression = { $envObj.Properties.linkedEnvironmentMetadata.resourceId } },
@{Name = "LinkedMetaPpacUniqueId"; Expression = { $envObj.Properties.linkedEnvironmentMetadata.uniqueName } },
@{Name = "LinkedMetaPpacEnvUri"; Expression = { $envObj.Properties.linkedEnvironmentMetadata.instanceUrl -replace "com/", "com" } },
@{Name = "LinkedMetaPpacEnvApiUri"; Expression = { $envObj.Properties.linkedEnvironmentMetadata.instanceApiUrl -replace "com/", "com" } },
@{Name = "LinkedMetaPpacEnvLanguage"; Expression = { $envObj.Properties.linkedEnvironmentMetadata.baseLanguage } },
@{Name = "PpacClusterIsland"; Expression = { $envObj.Properties.cluster.uriSuffix } },
"*"
Expand Down
2 changes: 1 addition & 1 deletion d365bap.tools/functions/Get-BapEnvironmentD365App.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Get D365 App from the environment
.DESCRIPTION
This enables the user to analyze and validate the current D365 Apps and their state, on a given environment
Enables the user to analyze and validate the current D365 Apps and their state, on a given environment
It can show all available D365 Apps - including their InstallState
Expand Down
14 changes: 12 additions & 2 deletions d365bap.tools/functions/Get-BapEnvironmentVirtualEntity.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

<#
.SYNOPSIS
Get Virutal Entity
Get Virtual Entity from environment
.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
This will help determine which Virtual Entities are already enabled / visible and their state, as they are seeing from the Dataverse environment
.PARAMETER EnvironmentId
The id of the environment that you want to work against
Expand Down Expand Up @@ -80,6 +80,16 @@
DimAttributeRetailTerminalEnt… False False 00002893-0000-0000-6e07-005001000000
EcoResRetailProductEntity False False 00002893-0000-0000-ae06-005001000000
.EXAMPLE
PS C:\> Get-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name AccountantEntity
This will fetch the specific virtual entity from the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
AccountantEntity False False 00002893-0000-0000-0003-005001000000
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Invoke the installation of a D365 App in a given environment
.DESCRIPTION
This enables the invocation of the installation process against the PowerPlatform API (https://api.powerplatform.com)
Enables the invocation of the installation process against the PowerPlatform API (https://api.powerplatform.com)
The cmdlet will keep requesting the status of all invoked installations, until they all have a NON "Running" state
Expand Down
217 changes: 217 additions & 0 deletions d365bap.tools/functions/Set-BapEnvironmentVirtualEntity.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@

<#
.SYNOPSIS
Set Virtual Entity configuration in environment
.DESCRIPTION
Enables the user to update the configuration for any given Virtual Entity in the environment
The configuration is done against the Dataverse environment, and allows the user to update the Visibility or TrackingChanges, for a given Virtual Entity
.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
.PARAMETER VisibilityOn
Instructs the cmdlet to turn "ON" the Virtual Entity
Can be used in combination with TrackingOn / TrackingOff
.PARAMETER VisibilityOff
Instructs the cmdlet to turn "OFF" the Virtual Entity
Can be used in combination with TrackingOn / TrackingOff
.PARAMETER TrackingOn
Instructs the cmdlet to enable ChangeTracking on the Virtual Entity
Can be used in combination with VisibilityOn / VisibilityOff
.PARAMETER TrackingOff
Instructs the cmdlet to disable ChangeTracking on the Virtual Entity
Can be used in combination with VisibilityOn / VisibilityOff
.EXAMPLE
PS C:\> Set-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name AccountantEntity -VisibilityOn -TrackingOff
This will enable the Virtual Entity "AccountantEntity" on the environment.
It will turn off the ChangeTracking at the same time.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
AccountantEntity True False 00002893-0000-0000-0003-005001000000
.EXAMPLE
PS C:\> Set-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name AccountantEntity -VisibilityOff
This will disable the Virtual Entity "AccountantEntity" on the environment.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
AccountantEntity False False 00002893-0000-0000-0003-005001000000
.EXAMPLE
PS C:\> Set-BapEnvironmentVirtualEntity -EnvironmentId eec2c11a-a4c7-4e1d-b8ed-f62acc9c74c6 -Name AccountantEntity -TrackingOn
This will update the Virtual Entity "AccountantEntity" on the environment.
It will enable the ChangeTracking for the entity.
Sample output:
EntityName IsVisible ChangeTrackingEnabled EntityGuid
---------- --------- --------------------- ----------
AccountantEntity True True 00002893-0000-0000-0003-005001000000
.NOTES
Author: Mötz Jensen (@Splaxi)
#>
function Set-BapEnvironmentVirtualEntity {
[CmdletBinding()]
param (
[parameter (mandatory = $true)]
[string] $EnvironmentId,

[parameter (mandatory = $true)]
[string] $Name,

[switch] $VisibilityOn,

[switch] $VisibilityOff,

[switch] $TrackingOn,

[switch] $TrackingOff
)

begin {
if (-not($VisibilityOn -or $VisibilityOff -or $TrackingOn -or $TrackingOff)) {
$messageString = "You need to select atleast one of the ParameterSets: You have to use either <c='em'>-VisibilityOn</c> / <c='em'>-VisibilityOff</c> or <c='em'>-TrackingOn</c> / <c='em'>-TrackingOff</c>."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because intent of the operation is NOT clear." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if ($VisibilityOn -and $VisibilityOff) {
$messageString = "The supplied parameter combination is not valid. You have to use either <c='em'>-VisibilityOn</c> or <c='em'>-VisibilityOff</c>."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because intent of the operation is NOT clear." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if ($TrackingOn -and $TrackingOff) {
$messageString = "The supplied parameter combination is not valid. You have to use either <c='em'>-TrackingOn</c> or <c='em'>-TrackingOff</c>."
Write-PSFMessage -Level Host -Message $messageString
Stop-PSFFunction -Message "Stopping because intent of the operation is NOT clear." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
}

if (Test-PSFFunctionInterrupt) { return }

# 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.LinkedMetaPpacEnvApiUri
$tokenWebApi = Get-AzAccessToken -ResourceUrl $baseUri
$headersWebApi = @{
"Authorization" = "Bearer $($tokenWebApi.Token)"
}

$entities = @(Get-BapEnvironmentVirtualEntity -EnvironmentId $EnvironmentId -Name $Name)

if ($entities.Count -ne 1) {
$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 }

$entity = $entities[0]
}

process {
if (Test-PSFFunctionInterrupt) { return }

$localUri = $($baseUri + '/XRMServices/2011/Organization.svc/web?SDKClientVersion=9.2.49.3165' -f $entity.EntityGuid)
$tempObj = [ordered]@{}

# Base payload for updating the virtual entity configuration
$body = @'
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<UserType xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">CrmUser</UserType>
<SdkClientVersion xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">9.2.49.3165</SdkClientVersion>
<x-ms-client-request-id xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">{0}</x-ms-client-request-id>
</s:Header>
<s:Body>
<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">
<request i:type="a:UpdateRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Parameters xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<a:KeyValuePairOfstringanyType>
<b:key>Target</b:key>
<b:value i:type="a:Entity">
<a:Attributes>
##INPUT##
</a:Attributes>
<a:EntityState i:nil="true"/>
<a:FormattedValues/>
<a:Id>{1}</a:Id>
<a:KeyAttributes xmlns:c="http://schemas.microsoft.com/xrm/7.1/Contracts"/>
<a:LogicalName>mserp_financeandoperationsentity</a:LogicalName>
<a:RelatedEntities/>
<a:RowVersion i:nil="true"/>
</b:value>
</a:KeyValuePairOfstringanyType>
</a:Parameters>
<a:RequestId>{0}</a:RequestId>
<a:RequestName>Update</a:RequestName>
</request>
</Execute>
</s:Body>
</s:Envelope>
'@ -f $([System.Guid]::NewGuid().Guid), $entity.EntityGuid


if ($VisibilityOn -or $VisibilityOff) {
# We need to set the Visibility configuration for the virtual entity
$visibility = @'
<a:KeyValuePairOfstringanyType>
<b:key>mserp_hasbeengenerated</b:key>
<b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">{0}</b:value>
</a:KeyValuePairOfstringanyType>
'@ -f $(if ($VisibilityOn) { "true" }else { "false" })
}


if ($TrackingOn -or $TrackingOff) {
# We need to set the Tracking configuration for the virtual entity
$tracking = @'
<a:KeyValuePairOfstringanyType>
<b:key>mserp_changetrackingenabled</b:key>
<b:value i:type="c:boolean" xmlns:c="http://www.w3.org/2001/XMLSchema">{0}</b:value>
</a:KeyValuePairOfstringanyType>
'@ -f $(if ($TrackingOn) { "true" }else { "false" })
}

$body = $body -replace $("##INPUT##", "$visibility`r`n$tracking")

$headersWebApi."Content-Type" = "text/xml; charset=utf-8"
$headersWebApi.SOAPAction = "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute"
Invoke-RestMethod -Method Post -Uri $localUri -Headers $headersWebApi -Body $body -ContentType "text/xml; charset=utf-8" > $null

Get-BapEnvironmentVirtualEntity -EnvironmentId $EnvironmentId -Name $Name
}

end {

}
}
Loading

0 comments on commit 1f3a85d

Please sign in to comment.