Skip to content

Commit f762c7c

Browse files
authored
New release
Support for UDE
2 parents ae4d55b + e2c7be6 commit f762c7c

File tree

95 files changed

+7005
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+7005
-492
lines changed

d365bap.tools/d365bap.tools.psd1

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,55 @@
4242

4343
# Functions to export from this module
4444
FunctionsToExport = @(
45-
'Compare-BapEnvironmentD365App'
45+
'Clear-UdeCredentialCache'
46+
, 'Clear-UdeOrphanedConfig'
47+
48+
, 'Compare-BapEnvironmentD365App'
4649
, 'Compare-BapEnvironmentUser'
4750
, 'Compare-BapEnvironmentVirtualEntity'
4851

4952
, 'Confirm-BapEnvironmentIntegration'
5053

5154
, 'Export-BapEnvironmentSolution'
52-
55+
5356
, 'Get-BapEnvironment'
5457
, 'Get-BapEnvironmentApplicationUser'
55-
5658
, 'Get-BapEnvironmentD365App'
57-
5859
, 'Get-BapEnvironmentLinkEnterprisePolicy'
59-
60+
, 'Get-BapEnvironmentPowerApp'
6061
, 'Get-BapEnvironmentSecurityRole'
62+
, 'Get-BapEnvironmentSecurityRoleMember'
6163
, 'Get-BapEnvironmentSolution'
62-
6364
, 'Get-BapEnvironmentUser'
64-
6565
, 'Get-BapEnvironmentVirtualEntity'
66+
, 'Get-BapTenant'
67+
, 'Get-BapTenantDetail'
68+
69+
, 'Get-UdeEnvironment'
70+
, 'Get-UdeConfig'
71+
, 'Get-UdeConnection'
72+
, 'Get-UdeCredentialCache'
73+
, 'Get-UdeDbJit'
74+
, 'Get-UdeDbJitCache'
75+
, 'Get-UdeDeveloperFile'
76+
, 'Get-UdeXrefDb'
6677

6778
, 'Invoke-BapEnvironmentInstallD365App'
68-
79+
, 'Invoke-BapInstallAzCopy'
80+
81+
, 'Set-BapAzCopyPath'
6982
, 'Set-BapEnvironmentLinkEnterprisePolicy'
7083
, 'Set-BapEnvironmentVirtualEntity'
84+
, 'Set-BapTenantDetail'
85+
86+
, 'Set-UdeConfig'
87+
, 'Set-UdeDbJitCache'
7188

89+
, 'Start-UdeDbSsms'
90+
, 'Switch-BapTenant'
91+
7292
, 'Update-BapEnvironmentVirtualEntityMetadata'
93+
7394
)
7495

7596
# Cmdlets to export from this module
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
<#
3+
.SYNOPSIS
4+
Clears the cached credentials used by Visual Studio for connecting to Dynamics 365 / Power Platform environments.
5+
6+
.DESCRIPTION
7+
Clears the cached credentials used by Visual Studio for connecting to Dynamics 365 / Power Platform environments by removing the credential cache directory located at %APPDATA%\Microsoft\CRMDeveloperToolKit.
8+
9+
.PARAMETER Force
10+
Instructs the function to proceed with clearing the credential cache.
11+
12+
Nothing happens unless this parameter is supplied.
13+
14+
.EXAMPLE
15+
PS C:\> Clear-UdeCredentialCache
16+
17+
This will prompt the user to make sure that they understand what will be removed/cleared.
18+
However, no action will be taken unless the -Force parameter is supplied.
19+
20+
.EXAMPLE
21+
PS C:\> Clear-UdeCredentialCache -Force
22+
23+
This will remove all Visual Studio cached credentials for connecting to Dynamics 365 / Power Platform environments.
24+
Force parameter is needed for the function to proceed.
25+
26+
.NOTES
27+
Author: Mötz Jensen (@Splaxi)
28+
29+
#>
30+
function Clear-UdeCredentialCache {
31+
[CmdletBinding()]
32+
param (
33+
[switch] $Force
34+
)
35+
36+
$path = "$env:APPDATA\Microsoft\CRMDeveloperToolKit"
37+
38+
if (-not $Force) {
39+
$messageString = "This will remove all Visual Studio cached credentials for connecting to Dynamics 365 / Power Platform environments. If you are sure, please re-run the command with the <c='em'>-Force</c> parameter."
40+
41+
Write-PSFMessage -Level Host -Message $messageString -Target Host
42+
Stop-PSFFunction -Message "Stopping because Force parameter wasn't supplied." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
43+
return
44+
}
45+
46+
Remove-Item -Path $path `
47+
-Recurse `
48+
-Force `
49+
-ErrorAction SilentlyContinue `
50+
-Confirm:$false
51+
}
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
2+
<#
3+
.SYNOPSIS
4+
Clears orphaned UDE configurations and their associated resources.
5+
6+
.DESCRIPTION
7+
This function identifies and removes orphaned UDE configurations, including their package directories, symlinks, and XRef databases.
8+
9+
.PARAMETER Force
10+
Instructs the function to proceed with clearing orphaned configurations.
11+
12+
Nothing happens unless this parameter is supplied.
13+
14+
.PARAMETER IncludePackages
15+
Instructs the function to include package directories in the clearing process.
16+
17+
Please note that package directories might be needed for other environments later on, so to avoid downloading packages again, this parameter is optional.
18+
19+
.EXAMPLE
20+
PS C:\> Clear-UdeOrphanedConfig
21+
22+
This will collect all orphaned UDE configurations and display them, but will not proceed with the clearing process.
23+
It will list the XppConfig directories and XRef databases that are considered orphaned.
24+
25+
.EXAMPLE
26+
PS C:\> Clear-UdeOrphanedConfig -Force
27+
28+
This will remove all orphaned UDE configurations and their associated resources.
29+
It will only process the XppConfig directories and XRef databases that are considered orphaned.
30+
31+
.EXAMPLE
32+
PS C:\> Clear-UdeOrphanedConfig -IncludePackages
33+
34+
This will collect all orphaned UDE configurations, including package directories, and display them, but will not proceed with the clearing process.
35+
It will list the package directories, XppConfig directories, and XRef databases that are considered orphaned.
36+
37+
.EXAMPLE
38+
PS C:\> Clear-UdeOrphanedConfig -IncludePackages -Force
39+
40+
This will remove all orphaned UDE configurations and their associated resources, including package directories.
41+
It will process the package directories, XppConfig directories, and XRef databases that are considered orphaned.
42+
Removing package directories might lead to re-downloading packages later on if they are needed for other environments.
43+
44+
.NOTES
45+
Author: Mötz Jensen (@Splaxi)
46+
47+
#>
48+
function Clear-UdeOrphanedConfig {
49+
[CmdletBinding()]
50+
param (
51+
[switch] $Force,
52+
53+
[switch] $IncludePackages
54+
)
55+
56+
begin {
57+
}
58+
59+
process {
60+
$colConfigs = Get-UdeConfig
61+
$pathBase = "$env:LOCALAPPDATA\Microsoft\Dynamics365"
62+
$orphanedDirs = [System.Collections.Generic.List[string]]::new()
63+
64+
if ($IncludePackages) {
65+
# We need to find all package directories - directly under the Dynamics365 local app data path
66+
$pathPackages = "$pathBase\10.*"
67+
$colPackageDirs = Get-Item -Path $pathPackages | `
68+
Select-Object -ExpandProperty FullName
69+
70+
# Then we need to find all active package directories - based on the UDE configs
71+
$activePackageDirs = @($colConfigs.PackagesLocalDirectory | `
72+
ForEach-Object { Split-Path -Path $_ -Parent }) | `
73+
Select-Object -Unique
74+
75+
# Finally, we need to find all orphaned package directories
76+
$colPackageDirs | Where-Object { $_ -notin $activePackageDirs } | ForEach-Object {
77+
$orphanedDirs.Add($_)
78+
}
79+
}
80+
81+
# We need to find all symlinked runtime package directories - directly under the RuntimeSymLinks path
82+
$pathSymlinks = "$pathBase\RuntimeSymLinks"
83+
$colSymlinkDirs = @(Get-ChildItem `
84+
-Path $pathSymlinks `
85+
-Directory | `
86+
Select-Object -ExpandProperty FullName)
87+
88+
# Then we need to find all active symlinked runtime package directories - based on the UDE configs
89+
$activeSymlinks = @($colConfigs.RuntimePackagesDirectory) | `
90+
Select-Object -Unique
91+
92+
# Finally, we need to find all orphaned symlinked runtime package directories
93+
$colSymlinkDirs | Where-Object { $_ -notin $activeSymlinks } | ForEach-Object {
94+
$orphanedDirs.Add($_)
95+
}
96+
97+
# We need to find all XRef Db directories - directly under the XPPConfig path
98+
$pathXRefDir = "$pathBase\XPPConfig"
99+
$colXRefDirs = @(Get-ChildItem -Path $pathXRefDir -Directory | `
100+
Select-Object -ExpandProperty FullName
101+
)
102+
103+
# Finally, we need to find all orphaned XRef Db directories
104+
$colXRefDirs | Where-Object {
105+
$(Split-Path -Path "$($_.Split("_")[0])" -Leaf) -notin @($colConfigs.Name) } | ForEach-Object {
106+
$orphanedDirs.Add($_)
107+
}
108+
109+
# We need to find all XRef DBs - directly under the LocalDB instance
110+
$colXRefDbs = Get-UdeXrefDb | Where-Object Name -ne 'SampleXRef'
111+
112+
# Then we need to find all active XRef DBs - based on the UDE configs
113+
$activeXRefDbs = @($colConfigs.XRefDatabase) | Select-Object -Unique
114+
115+
# Finally, we need to find all orphaned XRef DBs
116+
$orphanedXRefDbs = $colXRefDbs | Where-Object {
117+
$_.Name -notin $activeXRefDbs
118+
} | Select-Object -ExpandProperty Name
119+
120+
if ($orphanedDirs.Count -lt 1 -and $orphanedXRefDbs.Count -lt 1) {
121+
Write-PSFMessage -Level Host -Message "No orphaned UDE directories or XRef databases found. <c='em'>Nothing to do.</c>"
122+
return
123+
}
124+
125+
if (-not $Force) {
126+
if ($orphanedDirs.Count -gt 0) {
127+
Write-PSFMessage -Level Host -Message "The following directories are orphaned and will be removed:"
128+
$orphanedDirs.ToArray() | ForEach-Object { Write-PSFMessage -Level Host -Message " - <c='em'>$_</c>" }
129+
}
130+
131+
if ($orphanedXRefDbs.Count -gt 0) {
132+
Write-PSFMessage -Level Host -Message "The following XRef databases are orphaned and will be removed:"
133+
$orphanedXRefDbs | ForEach-Object { Write-PSFMessage -Level Host -Message " - <c='em'>$_</c>" }
134+
}
135+
136+
$messageString = "This will remove all listed package directories and XRef DBs. If you are sure, please re-run the command with the <c='em'>-Force</c> parameter."
137+
138+
Write-PSFMessage -Level Host -Message $messageString -Target Host
139+
Stop-PSFFunction -Message "Stopping because Force parameter wasn't supplied." -Exception $([System.Exception]::new($($messageString -replace '<[^>]+>', '')))
140+
return
141+
}
142+
143+
# Proceed with the removal of the directories
144+
foreach ($dir in $orphanedDirs) {
145+
Remove-Item -Path $dir `
146+
-Recurse `
147+
-Force `
148+
-ErrorAction SilentlyContinue `
149+
-Confirm:$false
150+
}
151+
152+
# Ensure that LocalDB sqlservr process is stopped before dropping databases
153+
$procSql = Get-Process -Name sqlservr `
154+
-FileVersionInfo `
155+
-ErrorAction SilentlyContinue
156+
157+
if ($null -ne $procSql.FileName -and $procSql.FileName -contains "*LocalDB*") {
158+
Get-Process -Name sqlservr | Stop-Process
159+
}
160+
161+
# Proceed with the removal of the directories
162+
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
163+
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand
164+
165+
try {
166+
$sqlConnection.ConnectionString = "Server='(LocalDB)\MSSQLLocalDB';Database='master';Integrated Security='SSPI';Application Name='d365bap.tools'"
167+
168+
$sqlCommand.Connection = $sqlConnection
169+
$sqlCommand.CommandTimeout = 0
170+
171+
$sqlCommand.Connection.Open()
172+
173+
foreach ($db in $orphanedXRefDbs) {
174+
$sqlCommand.CommandText = "DROP DATABASE [$db]"
175+
$sqlCommand.ExecuteNonQuery() > $null
176+
}
177+
178+
$sqlCommand.Connection.Close()
179+
}
180+
catch {
181+
Write-PSFMessage -Level Host -Message "Something went wrong while working with the sql server connection objects or the database server threw an unexpected error." -Exception $PSItem.Exception
182+
Stop-PSFFunction -Message "Stopping because of errors"
183+
return
184+
}
185+
finally {
186+
if ($sqlCommand.Connection.State -ne [System.Data.ConnectionState]::Closed) {
187+
$sqlCommand.Connection.Close()
188+
}
189+
190+
$sqlCommand.Dispose()
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)