From 0f4d74801ed6935654a497d99bdca6a1a152ede2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B6tz=20Jensen?= Date: Tue, 14 May 2024 18:01:02 +0200 Subject: [PATCH] Fix: Add comment section --- .../functions/repair-d365bacpacmodel.ps1 | 129 +++++++++++++++++- .../functions/repair-bacpacmodelqualifier.ps1 | 61 +++++++-- .../functions/repair-bacpacmodelreplace.ps1 | 39 ++++-- .../repair-bacpacmodelsimpleremove.ps1 | 60 ++++++-- 4 files changed, 254 insertions(+), 35 deletions(-) diff --git a/d365fo.tools/functions/repair-d365bacpacmodel.ps1 b/d365fo.tools/functions/repair-d365bacpacmodel.ps1 index e2891b09..6eb4a3b5 100644 --- a/d365fo.tools/functions/repair-d365bacpacmodel.ps1 +++ b/d365fo.tools/functions/repair-d365bacpacmodel.ps1 @@ -1,4 +1,115 @@ -function Repair-D365BacpacModel { +<# +.SYNOPSIS +Repair a bacpac model file + +.DESCRIPTION +As the backend of the Azure SQL infrastructure keeps evovling, the bacpac file can contain invalid instructions while we are trying to import into a local SQL Server installation on a Tier1 environment + +.PARAMETER Path +Path to the bacpac model file that you want to work against + +.PARAMETER OutputPath +Path to where the repaired model file should be placed + +.PARAMETER PathRepairSimple +Path to the json file, that contains all the instructions to be executed in the "Simple" section + +Simple means, that we can remove complex elements, based on some basic logic. E.g. + +{ + "Search": "**" +} + +"**" - we know when to stop. + + + + + + + + + + + + + + + + + + + + +.PARAMETER PathRepairQualifier +Path to the json file, that contains all the instructions to be executed in the "Qualifier" section + +Qualifier means, that we can remove complex elements, based on some basic logic. E.g. + +{ + "Search": "**", + "Qualifier": "**" +} + +"**" can identify below, "**" - we know when to stop. + + + + + + + + + + + + + + +.PARAMETER PathRepairReplace +Path to the json file, that contains all the instructions to be executed in the "Replace" section + +Replace means, that we can replace/remove strings, based on some basic logic. E.g. + +{ + "Search": "", + "Replace": "" +} + +"" can identify below, and "" is the value we want to replace with it. + + + +.PARAMETER KeepFiles +Instruct the cmdlet to keep the files from the repair process + +The files are very large, so only use this as a way to analyze why your model file didn't end up in the desired state + +Use it while you evolve/develop your instructions, but remove it from ANY full automation scripts + +.EXAMPLE +An example + +.NOTES +Author: Mötz Jensen (@Splaxi) + +Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended. + +This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech) + + His github profile can be found here: + https://github.com/batetech + +Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file + +The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e + +His github profile can be found here: + https://github.com/FH-Inway +#> +function Repair-D365BacpacModel { [CmdletBinding()] param ( [string] $Path, @@ -9,7 +120,9 @@ [string] $PathRepairQualifier = "$script:ModuleRoot\internal\misc\RepairBacpac.Qualifier.json", - [string] $PathRepairReplace = "$script:ModuleRoot\internal\misc\RepairBacpac.Replace.json" + [string] $PathRepairReplace = "$script:ModuleRoot\internal\misc\RepairBacpac.Replace.json", + + [switch] $KeepFiles ) # Load all the simple delete instructions @@ -46,6 +159,10 @@ # Clone input file to the local temporary file Copy-Item -Path $forOutput -Destination $localInput -Force + if (-not $KeepFiles) { + Get-ChildItem -Path "$($directoryObj.FullName)\*.simple.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue + } + for ($i = 0; $i -lt $arrQualifier.Count; $i++) { $forInput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.qualifier.input.xml" $forOutput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.qualifier.output.xml" @@ -68,6 +185,10 @@ # Clone input file to the local temporary file Copy-Item -Path $forOutput -Destination $localInput -Force + if (-not $KeepFiles) { + Get-ChildItem -Path "$($directoryObj.FullName)\*.qualifier.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue + } + for ($i = 0; $i -lt $arrReplace.Count; $i++) { $forInput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.replace.input.xml" $forOutput = Join-Path -Path $directoryObj.FullName -ChildPath "$i.replace.output.xml" @@ -79,4 +200,8 @@ } Copy-Item -Path $forOutput -Destination $OutputPath -Force + + if (-not $KeepFiles) { + Get-ChildItem -Path "$($directoryObj.FullName)\*.replace.*.xml" | Remove-Item -Force -ErrorAction SilentlyContinue -WarningAction SilentlyContinue + } } \ No newline at end of file diff --git a/d365fo.tools/internal/functions/repair-bacpacmodelqualifier.ps1 b/d365fo.tools/internal/functions/repair-bacpacmodelqualifier.ps1 index 44aa928f..b3d5ae24 100644 --- a/d365fo.tools/internal/functions/repair-bacpacmodelqualifier.ps1 +++ b/d365fo.tools/internal/functions/repair-bacpacmodelqualifier.ps1 @@ -1,34 +1,71 @@ <# -."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelqualifier.ps1" .SYNOPSIS -Short description +Repair a bacpac model file - using qualification logic .DESCRIPTION -Long description +Will use a search pattern, qualification and end pattern, to remove an element from the model file .PARAMETER Path -Parameter description +Path to the bacpac model file that you want to work against .PARAMETER OutputPath -Parameter description +Path to where the repaired model file should be placed .PARAMETER Search -Parameter description +Search pattern that is used to start the removable of the element + +Supports wildcard - as it utilizes the -Like operation that is available directly in powershell + +E.g. "**" .PARAMETER Qualifier -Parameter description +Qualifier pattern that is used to qualify the element, based on a nested line value + +Supports wildcard - as it utilizes the -Like operation that is available directly in powershell + +E.g. "**" .EXAMPLE -An example +PS C:\> Repair-BacpacModelQualifier -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "**" -Qualifier "**" + +This will remove the below section from the model file + + + + + + + + + + + + + .NOTES -General notes +Author: Mötz Jensen (@Splaxi) + +Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended. + +This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech) + + His github profile can be found here: + https://github.com/batetech + +Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file + +The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e -Repair-BacpacModelQualifier -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '**' -Qualifier '**' -Repair-BacpacModelQualifier -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '**' -Qualifier '**' +His github profile can be found here: + https://github.com/FH-Inway #> function Repair-BacpacModelQualifier { [CmdletBinding()] diff --git a/d365fo.tools/internal/functions/repair-bacpacmodelreplace.ps1 b/d365fo.tools/internal/functions/repair-bacpacmodelreplace.ps1 index d6061405..8c00b52c 100644 --- a/d365fo.tools/internal/functions/repair-bacpacmodelreplace.ps1 +++ b/d365fo.tools/internal/functions/repair-bacpacmodelreplace.ps1 @@ -1,30 +1,49 @@ <# -."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelreplace.ps1" .SYNOPSIS -Short description +Repair a bacpac model file - using replace logic .DESCRIPTION -Long description +Will use a search value, to replace an text value from the model file .PARAMETER Path -Parameter description +Path to the bacpac model file that you want to work against .PARAMETER OutputPath -Parameter description +Path to where the repaired model file should be placed .PARAMETER Search -Parameter description +Search pattern that is used to replace the value + +Works directly on the value entered, not wildcard or regex is supported at all + +E.g. "" .PARAMETER Replace -Parameter description +Replace value that you want to substitute your search value with .EXAMPLE -An example +PS C:\> Repair-BacpacModelReplace -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "" -Replace "" + +This will replace the below section from the model file + + .NOTES -General notes +Author: Mötz Jensen (@Splaxi) + +Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended. + +This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech) + + His github profile can be found here: + https://github.com/batetech + +Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file + +The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e -Repair-BacpacModelReplace -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '' -Replace '' +His github profile can be found here: + https://github.com/FH-Inway #> function Repair-BacpacModelReplace { [CmdletBinding()] diff --git a/d365fo.tools/internal/functions/repair-bacpacmodelsimpleremove.ps1 b/d365fo.tools/internal/functions/repair-bacpacmodelsimpleremove.ps1 index fe181b50..e2e23eeb 100644 --- a/d365fo.tools/internal/functions/repair-bacpacmodelsimpleremove.ps1 +++ b/d365fo.tools/internal/functions/repair-bacpacmodelsimpleremove.ps1 @@ -1,32 +1,70 @@ <# -."C:\GIT\GITHUB\d365fo.tools.Workspace\d365fo.tools\d365fo.tools\internal\functions\repair-bacpacmodelsimpleremove.ps1" .SYNOPSIS -Short description +Repair a bacpac model file - using simple logic .DESCRIPTION -Long description +Will use a search pattern, and end pattern, to remove an element from the model file .PARAMETER Path -Parameter description +Path to the bacpac model file that you want to work against .PARAMETER OutputPath -Parameter description +Path to where the repaired model file should be placed .PARAMETER Search -Parameter description +Search pattern that is used to start the removable of the element + +Supports wildcard - as it utilizes the -Like operation that is available directly in powershell + +E.g. "**" .EXAMPLE -An example +PS C:\> Repair-BacpacModelSimpleRemove -Path c:\temp\model.xml -OutputPath c:\temp\repaired_model.xml -Search "**" + +This will remove the below section from the model file + + + + + + + + + + + + + + + + + + + .NOTES -General notes +Author: Mötz Jensen (@Splaxi) + +Json files has to be an array directly in the root of the file. All " (double quotes) has to be escaped with \" - otherwise it will not work as intended. + +This cmdlet is inspired by the work of "Brad Bateman" (github: @batetech) + + His github profile can be found here: + https://github.com/batetech + +Florian Hopfner did a gist implementation, and did all the inital work in terms of finding the fastest way to work against the model file -Repair-BacpacModelSimpleRemove -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '**' -Repair-BacpacModelSimpleRemove -Path "C:\Temp\INOX\Bacpac\Base.xml" -OutputPath "C:\Temp\INOX\Bacpac\Working.xml" -Search '**' +The original gist is: https://gist.github.com/FH-Inway/f485c720b43b72bffaca5fb6c094707e +His github profile can be found here: + https://github.com/FH-Inway #> function Repair-BacpacModelSimpleRemove { [CmdletBinding()]