CrmDataPackager.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
Set-StrictMode -Version Latest

<#
.Synopsis
   Packs and zips a folder of Configuration Migration tool generated files previously created from the Expand-CrmData cmdlet.
.DESCRIPTION
   This function packs and zips the folders and files created from Expand-CrmData processing of a Configuration Migration tool generated zip file.
.EXAMPLE
   Compress-CrmData -Folder 'C:\temp\data\AdventureWorks' -ZipFile 'C:\temp\packed\AdventureWorksData.zip'
 
   This example processes the contents of the specified -Folder with an already exported data set from an AdventureWorks organization and saves the packed zip file to the specified -ZipFile.
#>

function Compress-CrmData {
    param (
        # The folder path of an unpacked Configuation Migration tool generated zip file.
        [Parameter(Mandatory, ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Folder,

        # The zip file to create after packing the configuration data files.
        [Parameter(Mandatory, ValueFromPipelineByPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$ZipFile
    )

    CrmConfigurationPackager -Pack -Path $Folder -DestinationPath $ZipFile
}

<#
.Synopsis
   Extracts and unpacks a Configuration Migration tool generated zip file to individual files.
.DESCRIPTION
   This function extracts a Configuration Migration tool generated zip file and unpacks the .xml files into separate files and folders, where each entity is stored in its own folder and each record is stored in its own .xml file inside the entity folder.
.EXAMPLE
   Expand-CrmData -ZipFile 'C:\temp\export\AdventureWorksData.zip' -Folder 'C:\temp\data\AdventureWorks'
 
   This example extracts the data zip file exported from the AdventureWorks organization and unpacks the contents to the specified -Folder.
#>

function Expand-CrmData {
    param (
        # The path and filename to the Configuration Migration tool generated zip file.
        [Parameter(Mandatory, ValueFromPipelineByPropertyName=$true)]
        [string]$ZipFile,

        # The folder path to store the unpacked records.
        [Parameter(Mandatory, ValueFromPipelineByPropertyName=$true)]
        [string]$Folder,

        # The CrmDataPackager JSON settings file path for specifying per entity unpacking settings.
        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [string]$SettingsFile
    )
    process
    {
        CrmConfigurationPackager -Extract -Path $ZipFile -DestinationPath $Folder -SettingsFile $SettingsFile
    }
}

function Format-Xml {
    [OutputType([string])]
    param (
        [Parameter(Mandatory=$true)]
        [xml]$Xml,

        [Parameter()]
        [int]$Indent=2
    )

    $StringWriter = New-Object System.IO.StringWriter
    $XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter
    $XmlWriter.Formatting = "indented"
    $XmlWriter.Indentation = $Indent
    $Xml.WriteContentTo($XmlWriter)
    $XmlWriter.Flush()
    $StringWriter.Flush()
    Write-Output $StringWriter.ToString()
}

function CreateRootSchema {
    param (
        [Parameter(Mandatory=$true)]
        [xml]$xml,

        [Parameter(Mandatory=$true)]
        [string]$DataPath

    )

    [xml]$rootSchema = $xml.CloneNode($true)

    foreach($entity in $rootSchema.entities.entity) {
        $entityFolder = Get-Item (Join-Path $DataPath $entity.name) -ErrorAction Ignore

        if($entityFolder) {

            # remove extraneous attributes that aren't helpful when viewing the file
            $removeAttributes = $entity.Attributes | Where-Object {$_.name -notin ('name','displayName')}
            foreach ($remove in $removeAttributes){
                $entity.RemoveAttribute($remove.name)
            }

            # remove child elements and make the entity element self-closing tag
            $entity.IsEmpty = $true

        } else {
            Write-Verbose "Removing $($entity.name) from root schema file due to no data for entity"
            $rootSchema.entities.RemoveChild($entity) | Out-Null
        }
    }

    return $rootSchema
}

function ExtractData {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath,

        [Parameter()]
        $settings
    )

    Write-Verbose "Loading assembly System.Web"
    Add-Type -AssemblyName System.Web

    Write-Verbose "Loading assembly Newtonsoft.Json"
    Add-Type -Path (Join-Path -Path $PSScriptRoot -ChildPath Newtonsoft.Json.dll)

    [xml]$xml = Get-Content -Path $Path -Encoding UTF8

    foreach($entity in $xml.entities.entity) {
        $entityFolder = New-Item -ItemType Directory -Path (Join-Path -Path $DestinationPath -ChildPath $entity.name)
        $recordsFolder = New-Item -ItemType Directory -Path (Join-Path -Path $entityFolder -ChildPath records)

        $settingsEntity = $settings.entities | Where-Object {$_.entity -eq $entity.name}

        $entitySettings = LoadEntitySettings -Entity $entity -SettingsEntity $settingsEntity

        foreach($record in $entity.records.record) {
            if($settingsEntity) {
                foreach($field in $settingsEntity.fields) {

                    $fieldSettings = LoadFieldSettings -SettingsField $field

                    if($fieldSettings.field) {
                        WriteFileAndUpdateRecord -Entity $entity -RecordsFolder $recordsFolder -Record $record -FieldSettings $fieldSettings
                    }
                }
            }

            $fileNamePrefix = $record.id
            $fileExtension = $entitySettings.extension
            $fileName = "$($fileNamePrefix)$($fileExtension)"
            
            if($entitySettings.fileNameField -ne "id") {
                $fileNamePrefix = GetFileNamePrefix -FileNameField $fieldSettings.fileNameField -Record $Record 
                $fileNamePrefix = EscapeFileName -FileName $fileNamePrefix
                $fileName = "$($fileNamePrefix)$($fileExtension)"

                $recordPath = Join-Path -Path $recordsFolder -ChildPath $fileName

                if(Test-Path -Path $recordPath) {
                    $existingFileName = $fileName
                    $fileName = "$($record.id)$($fileExtension)"

                    Write-Warning "Naming $($entity.name) file $fileName to avoid conflict with existing file $existingFileName"
                }
            }

            $recordPath = Join-Path -Path $recordsFolder -ChildPath $fileName

            Write-Verbose "Writing file $recordPath"
            Set-Content -Path $recordPath -Value (Format-Xml -xml $record.OuterXml) -Encoding UTF8
        }      

        if($entity.m2mrelationships.GetType() -eq [System.Xml.XmlElement]) {
            $m2mrelationshipsPath = Join-Path -Path $entityFolder -ChildPath m2mrelationships
            Write-Verbose "Creating folder $m2mrelationshipsPath"
            $m2mrelationshipsFolder = New-Item -ItemType Directory -Path $m2mrelationshipsPath

            foreach($m2mrelationship in $entity.m2mrelationships.m2mrelationship) {
                $m2mrelationshipnamePath = Join-Path -Path $m2mrelationshipsFolder -ChildPath $m2mrelationship.m2mrelationshipname
                $m2mrelationshipnameFolder = Get-Item -Path $m2mrelationshipnamePath -ErrorAction Ignore
                if($m2mrelationshipnameFolder -eq $null) {
                    Write-Verbose "Creating folder $m2mrelationshipnamePath"
                    $m2mrelationshipnameFolder = New-Item -ItemType Directory -Path $m2mrelationshipnamePath -ErrorAction Ignore
                }
                $m2mrelationshipPath = Join-Path $m2mrelationshipnameFolder -ChildPath "$($m2mrelationship.sourceid).xml"
                Write-Verbose "Writing file $m2mrelationshipPath"
                Set-Content -Path $m2mrelationshipPath -Value (Format-Xml -Xml $m2mrelationship.OuterXml) -Encoding UTF8
            }
        }
    }

    $rootData = CreateRootSchema -xml $xml -DataPath $DestinationPath

    # write back the condensed version of the data.xml file to the root of the folder
    Set-Content -Path (Join-Path -Path $DestinationPath -ChildPath 'data.xml') -Value (Format-Xml -xml $rootData.OuterXml) -Encoding UTF8
}

function CreateManifest {
    param(
        $Path,

        $Settings
    )

    $manifest = [ordered]@{
        version = $MyInvocation.MyCommand.Module.Version.ToString()
        timestamp = [DateTime]::UtcNow.ToString("o")
        entities = $Settings.entities
    }

    $json = ConvertTo-Json $manifest -Depth 5

    $formatted = [Newtonsoft.Json.Linq.JToken]::Parse($json).ToString()

    $settingsPath = Join-Path -Path $Path -ChildPath settings.json
    Write-Verbose "Writing file $settingsPath"
    Set-Content -Value $formatted -Path $settingsPath -Encoding UTF8
}

function WriteFileAndUpdateRecord {
    param (
        [Parameter(Mandatory=$true)]
        $Entity,

        [Parameter(Mandatory=$true)]
        $RecordsFolder,

        [Parameter(Mandatory=$true)]
        $Record,

        [Parameter(Mandatory=$true)]
        [PSCustomObject]$FieldSettings
    )

    $field = $Record.field | Where-Object {$_.name -eq $FieldSettings.field}

    if($field) {

        if($Entity.name -eq 'annotation' -and $FieldSettings.field -eq 'documentbody' -and $FieldSettings.extension -eq 'auto') {

            $documentbodyFolder = Join-Path -Path $recordsFolder -ChildPath 'documentbody'
            New-Item -ItemType Directory -Path $documentbodyFolder -ErrorAction SilentlyContinue | Out-Null
            
            # unpack the documentbody field from annotations, by converting it from base 64 encoding and saving to the file system
            $annotationFileNameField = $record.field | Where-Object {$_.name -eq 'filename'}

            $documentbody = [Convert]::FromBase64String($field.value)
            
            if($FieldSettings.fileNameField -ne "id") {
                $documentbodyfilename = EscapeFileName -FileName $annotationFileNameField.value
                $documentbodyPath = Join-Path -Path $documentbodyFolder -ChildPath $documentbodyfilename

                if(Test-Path -Path $documentbodyPath) {
                    $existingFileName = $documentbodyfilename
                    $annotationFileExtension = [IO.Path]::GetExtension($annotationFileNameField.value)
                    $documentbodyfilename = "$($record.id)$annotationFileExtension"

                    Write-Warning "Naming $($Entity.name)\$($field.name) file $documentbodyfilename to avoid conflict with existing file $existingFileName"
                }
            } else {
                $annotationFileExtension = [IO.Path]::GetExtension($annotationFileNameField.value)
                $documentbodyfilename = "$($record.id)$annotationFileExtension"
            }

            $documentbodyPath = Join-Path -Path $documentbodyFolder -ChildPath $documentbodyfilename

            Write-Verbose "Writing file $documentbodyPath"
            [IO.File]::WriteAllBytes($documentbodyPath, $documentbody)

            # set the documentbody field value to the generated unpacked filename so the base 64 encoded text isn't written to disk and the file can be easily identified
            $Field.RemoveAttribute("value")
            $path = [string](Join-Path -Path 'documentbody' -ChildPath $documentbodyfilename)
            $field.SetAttribute("path", $path)
            
            if($FieldSettings.hash) {
                $md5 = GetBytesHashMD5 -Bytes $documentbody
                $field.SetAttribute("hash", $md5)
            }
        } else {
            if($Entity.name -eq 'adx_contentsnippet' -and $FieldSettings.field -eq 'adx_value' -and $FieldSettings.extension -eq 'auto') {
                # determine if the type is text or html, set the file extension accordingly
                $type = $record.field | Where-Object {$_.name -eq 'adx_type'}
                $FieldSettings.extension = ".txt"
                if($type -and $type.value -eq "756150001") {
                    $FieldSettings.extension = ".html"
                }
            }

            $fileInfo = WriteTextFile -EntityName $Entity.name -RecordsFolder $RecordsFolder -Record $Record -Field $field -FieldSettings $FieldSettings

            # set the field value to the unpacked filename and hash so the original text isn't written to disk and the file can be easily identified
            $field.RemoveAttribute("value")
            $field.SetAttribute("path", $fileInfo.path)

            if($FieldSettings.hash) {
                $field.SetAttribute("hash", $fileInfo.hash)
            }
        }
    }
}

function WriteTextFile {
    param (
        $EntityName,

        $RecordsFolder,

        [Parameter(Mandatory=$true)]
        $Record,

        [Parameter(Mandatory=$true)]
        $Field,

        [Parameter(Mandatory=$true)]
        [PSCustomObject]$FieldSettings
    )
    
    $value = [System.Web.HttpUtility]::HtmlDecode($Field.value)

    $fileNamePrefix = GetFileNamePrefix -FileNameField $FieldSettings.fileNameField -Record $Record 

    if($FieldSettings.fileNameField -ne "id") {
        $fileNamePrefix = EscapeFileName -FileName $fileNamePrefix
    }

    if($FieldSettings.extension -eq '.json' -and $FieldSettings.format) {
        $value = [Newtonsoft.Json.Linq.JToken]::Parse($value).ToString()
    }

    $fileName = "$($fileNamePrefix)$($FieldSettings.extension)"
    $folder = Join-Path -Path $RecordsFolder -ChildPath $Field.name
    $path = Join-Path -Path $folder -ChildPath $fileName

    if($FieldSettings.fileNameField -ne "id" -and (Test-Path -Path $path)) {
        $existingFileName = $fileName
        $fileName = "$($record.id)$($FieldSettings.extension)"
        $folder = Join-Path -Path $RecordsFolder -ChildPath $Field.name

        Write-Warning "Naming $EntityName\$($Field.name) file $fileName to avoid conflict with existing file $existingFileName"

        $path = Join-Path -Path $folder -ChildPath $fileName
    }

    New-Item -ItemType Directory -Path $folder -ErrorAction SilentlyContinue | Out-Null
    Write-Verbose "Writing file $path"
    Set-Content -Path $path -Value $value -Encoding UTF8 -NoNewline

    # save the relative path to the new file so it can be easily identified in the original record
    $relativePath = [string](Join-Path -Path $Field.name -ChildPath $fileName)

    # save the hash so changes can be identified in the original record
    if($FieldSettings.hash) {
        $hash = GetTextHashMD5 -Text $value

        return @{
            path = $relativePath
            hash = $hash
        }
    } else {
        return @{
            path = $relativePath
            hash = ''
        }
    }
}

function EscapeFileName {
    param (
        [Parameter(Mandatory=$true)]
        $FileName
    )

    $specialCharacters = '\', '/', ':', '*', '?', '"', '<', '>', '|', ` # windows reserved characters
                         ','                                            # field value reserved characters
    
    $fileNameChars = $FileName.ToCharArray()

    $fileNameSb = [System.Text.StringBuilder]::new()

    for ($i = 0; $i -lt $fileNameChars.Length; $i++) { 
        if($fileNameChars[$i] -cin $specialCharacters) {
            $fileNameSb.Append([uri]::EscapeDataString($fileNameChars[$i])) | Out-Null
        } else {
            $fileNameSb.Append($fileNameChars[$i]) | Out-Null
        }
    }

    $escaped = $fileNameSb.ToString()
    return $escaped
}

function GetFileNamePrefix {
    param (
        [Parameter(Mandatory=$true)]
        $Record,

        [Parameter()]
        [string]$FileNameField
    )
    
    if($FileNameField -eq 'id') {
        return $record.id
    }

    if($FileNameField) {
        $field = $record.field | Where-Object {$_.name -eq $FileNameField}
        if($field) {
            return $field.value
        }
    }

    return $record.id
}

function GetTextHashMD5 {
    param (
        [string]$Text
    )

    $stream = [System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($Text))
    $hash = Get-FileHash -InputStream $stream -Algorithm MD5
    return $hash.Hash
}

function GetBytesHashMD5 {
    param (
        [byte[]]$Bytes
    )

    $stream = [System.IO.MemoryStream]::new($Bytes)
    $hash = Get-FileHash -InputStream $stream -Algorithm MD5
    return $hash.Hash
}

function CreateFileId {
    param (
        [string]$Path,

        [string]$MD5
    )
    
    return "$path,$md5"
}


function PackData {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath,

        [Parameter(Mandatory=$true)]
        [PSCustomObject]$Settings
    )

    Write-Verbose "Loading assembly System.Web"
    Add-Type -AssemblyName System.Web

    Write-Verbose "Loading assembly Newtonsoft.Json"
    Add-Type -Path (Join-Path -Path $PSScriptRoot -ChildPath Newtonsoft.Json.dll)

    $rootDataPath = Join-Path -Path $Path -ChildPath data.xml

    Write-Verbose "Loading file $rootDataPath"
    [xml]$xml = Get-Content -Path $rootDataPath -Encoding UTF8

    foreach($entity in $xml.entities.entity) {
        $entityFolder = Get-Item -Path (Join-Path -Path $Path -ChildPath $entity.name) -ErrorAction Ignore

        if($entityFolder -eq $null) {
            Write-Verbose "No data for entity $($entity.name), skipping data pack"
            $xml.entities.RemoveChild($entity) | Out-Null
            continue
        }

        $recordsFolder = Get-Item -Path (Join-Path -Path $entityFolder -ChildPath 'records')

        # load all the xml files that represent each CRM record
        $recordFiles = Get-ChildItem -Path $recordsFolder -Filter '*.xml'

        # create the stub records element where each CRM record will be stored
        $recordsNode = $xml.ImportNode(([xml]"<records />").DocumentElement, $true)

        $entity.AppendChild($recordsNode) | Out-Null

        $settingsEntity = $settings.entities | Where-Object {$_.entity -eq $entity.name}

        $entitySettings = LoadEntitySettings -Entity $entity -SettingsEntity $settingsEntity

        # read the record xml for each CRM record from disk, and add it to the records element
        foreach($recordFile in $recordFiles) {
            Write-Verbose "Loading file $($recordFile.FullName)"
            $recordData = [xml](Get-Content -Path $recordFile.FullName -Encoding UTF8)

            if($settingsEntity) {
                foreach($field in $settingsEntity.fields) {
                    $fieldSettings = LoadFieldSettings -SettingsField $field

                    LoadFileAndUpdateRecord -RecordsFolder $recordsFolder -Record $recordData.record -Entity $entity -FieldSettings $fieldSettings
                }
            }

            $recordDataNode = $xml.ImportNode($recordData.DocumentElement, $true)
            $recordsNode.AppendChild($recordDataNode) | Out-Null
        }

        # create the stub m2mrelationships element that will be replaced with the m2mrelationships.xml file
        $m2mStubNode = $xml.ImportNode(([xml]"<m2mrelationships />").DocumentElement, $true)
        $entity.AppendChild($m2mStubNode)| Out-Null

        # try load the m2mrelationships, if it exists, replace the m2mrelationships node
        $m2mrelationshipsFilePath = Join-Path -Path $entityFolder -ChildPath m2mrelationships.xml
        $m2mrelationshipsFolderPath = Join-Path -Path $entityFolder -ChildPath m2mrelationships

        if(Test-Path -Path $m2mrelationshipsFilePath -PathType Leaf) {
            Write-Verbose "Loading file $m2mrelationshipsFilePath"
            $m2mData = [xml](Get-Content -Path $m2mrelationshipsFilePath -Encoding UTF8)
            $m2mNode = $xml.ImportNode($m2mData.DocumentElement, $true)
            $entity.ReplaceChild($m2mNode, $m2mStubNode) | Out-Null
        } elseif(Test-Path -Path $m2mrelationshipsFolderPath -PathType Container) {
            $m2mrelationshipsFolders = Get-ChildItem -Path $m2mrelationshipsFolderPath

            foreach($m2mrelationshipFolder in $m2mrelationshipsFolders) {
                $m2mrelationshipFiles = Get-ChildItem -Path $m2mrelationshipFolder.FullName -Filter '*.xml'

                foreach($m2mrelationshipFile in $m2mrelationshipFiles) {
                    Write-Verbose "Loading file $($m2mrelationshipFile.FullName)"
                    $m2mrelationshipData = [xml](Get-Content -Path $m2mrelationshipFile.FullName -Encoding UTF8)
                    $m2mrelationshipDataNode = $xml.ImportNode($m2mrelationshipData.DocumentElement, $true)
                    $m2mStubNode.AppendChild($m2mrelationshipDataNode) | Out-Null
                }
            }
        }
    }

    # write the packed version of the data.xml file to the root of the folder
    $dataOutPath = Join-Path -Path $DestinationPath -ChildPath 'data.xml'
    Write-Verbose "Writing file $dataOutPath"
    Set-Content -Path $dataOutPath -Value (Format-Xml -xml $xml.OuterXml) -Encoding UTF8
}

function LoadFileAndUpdateRecord {
    param (
        [Parameter(Mandatory=$true)]
        $RecordsFolder,

        [Parameter(Mandatory=$true)]
        $Record,

        [Parameter(Mandatory=$true)]
        $Entity,
        
        [Parameter(Mandatory=$true)]
        $FieldSettings
    )

    if($Entity.name -eq 'annotation' -and $FieldSettings.field -eq 'documentbody' -and $FieldSettings.extension -eq 'auto') {
        # pack the documentbody field from annotations, by converting it from binary to base 64 encoding
        $documentbodyfield = $recordData.record.field | Where-Object {$_.name -eq 'documentbody'}

        if($documentbodyfield) {
            $relativePath = $documentbodyfield.path
            $documentbodypath = Join-Path -Path $recordsFolder -ChildPath $relativePath
            Write-Verbose "Loading file $documentbodypath"
            
            $documentbodyBytes = Get-Content -Path $documentbodypath -Encoding Byte -Raw
            $documentbodybase64 = [Convert]::ToBase64String($documentbodyBytes)
            $documentbodyfield.SetAttribute("value", $documentbodybase64)

            $documentbodyfield.RemoveAttribute("path")

            if($FieldSettings.hash) {
                $documentbodyfield.RemoveAttribute("hash")
            }
        }
    } else {
        $field = $record.field | Where-Object {$_.name -eq $FieldSettings.field}

        if($field) {
            $relativePath = $field.path
            $filePath = Join-Path -Path $RecordsFolder -ChildPath $relativePath
            Write-Verbose "Loading file $filePath"

            $value = Get-Content -Path $filePath -Encoding UTF8 -Raw

            if($relativePath.EndsWith(".json") -and $FieldSettings.format) {
                $value = [Newtonsoft.Json.Linq.JToken]::Parse($value).ToString([Newtonsoft.Json.Formatting]::None)
            }

            $encoded = [System.Web.HttpUtility]::HtmlEncode($value)
            $field.SetAttribute("value", $encoded)
            
            $field.RemoveAttribute("path")

            if($FieldSettings.hash) {
                $field.RemoveAttribute("hash")
            }
        }
    }
}

function ExtractSchema {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath
    )

    $xml = [xml](Get-Content -Path $Path -Encoding UTF8)

    foreach($entity in $xml.entities.entity) {
        $entityFolder = Get-Item -Path (Join-Path -Path $DestinationPath -ChildPath $entity.name) -ErrorAction Ignore
        if($entityFolder) {
            $schemaPath = Join-Path -Path $entityFolder -ChildPath 'data_schema.xml'
            Write-Verbose "Writing file $schemaPath"
            Set-Content -Path $schemaPath -Value (Format-Xml -xml $entity.OuterXml) -Encoding UTF8
        } else {
            Write-Verbose "No data for entity $($entity.name), skipping schema extract"
        }
    }

    $rootSchema = CreateRootSchema -xml $xml -DataPath $DestinationPath

    Set-Content -Path (Join-Path -Path $DestinationPath -ChildPath 'data_schema.xml') -Value (Format-Xml -xml $rootSchema.OuterXml) -Encoding UTF8
}

function PackSchema {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath
    )

    $rootSchemaPath = Join-Path -Path $Path -ChildPath data_schema.xml
    Write-Verbose "Loading file $rootSchemaPath"
    $xml = [xml](Get-Content -Path $rootSchemaPath -Encoding UTF8)

    foreach($entity in $xml.entities.entity) {
        $entityFolder = Get-Item -Path (Join-Path -Path $Path -ChildPath $entity.name) -ErrorAction Ignore
        if($entityFolder) {
            $schemaPath = Join-Path -Path $entityFolder -ChildPath 'data_schema.xml'
            Write-Verbose "Loading file $schemaPath"
            $entitySchema = [xml](Get-Content -Path $schemaPath -Encoding UTF8)
            $entitySchemaNode = $xml.ImportNode($entitySchema.DocumentElement, $true)
            $xml.entities.ReplaceChild($entitySchemaNode, $entity) | Out-Null
        } else {
            Write-Verbose "No data for entity $($entity.name), skipping schema pack"
            $xml.entities.RemoveChild($entity) | Out-Null
        }
    }

    if($SchemaOnly) {
        $outPath = $DestinationPath
    } else {
        # write the packed version of the data_schema.xml file to the root of the folder
        $outPath = Join-Path -Path $DestinationPath -ChildPath 'data_schema.xml'
    }

    Write-Verbose "Writing file $outPath"
    Set-Content -Path $outPath -Value (Format-Xml -xml $xml.OuterXml) -Encoding UTF8
}

function PackContentTypes {
    param (
        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath
    )

    $sourceContentTypesPath = Join-Path -Path $Path -ChildPath "[Content_Types].xml"
    $destinationContentTypesPath = Join-Path -Path $DestinationPath -ChildPath "[Content_Types].xml"
    Write-Verbose "Writing file $destinationContentTypesPath"
    Copy-Item -LiteralPath $sourceContentTypesPath -Destination $destinationContentTypesPath
}

function CrmConfigurationPackager {
    param (
        [Parameter(Mandatory=$true,ParameterSetName="Extract")]
        [Switch]$Extract,

        [Parameter(Mandatory=$true,ParameterSetName="Pack")]
        [Switch]$Pack,

        [Parameter(Mandatory=$true)]
        [string]$Path,

        [Parameter(Mandatory=$true)]
        [string]$DestinationPath,

        [Parameter(ValueFromPipelineByPropertyName=$true)]
        [string]$SettingsFile,

        # convert the packed version of the data to a zip file
        [Parameter(ParameterSetName="Pack")]
        [switch]$Compress = $true,

        # only pack the schema
        [Parameter(ParameterSetName="Pack")]
        [switch]$SchemaOnly
    )

    if($Extract) {

        if(Get-Item -Path $DestinationPath -OutVariable DestinationFullPath -ErrorAction Ignore) {
            Write-Verbose "Deleting folder $DestinationPath"

            Remove-Item -Path $DestinationFullPath -Recurse -Force
        }

        if([IO.Path]::GetExtension($Path) -eq '.zip') {
            Expand-Archive -Path $Path -DestinationPath $DestinationPath
        } else {
            Copy-Item -Path $Path -Destination $DestinationPath -Recurse
        }

        $dataPath = Join-Path -Path $DestinationPath -ChildPath data.xml
        $schemaPath = Join-Path -Path $DestinationPath -ChildPath data_schema.xml

        if(-not $SettingsFile) {
            $SettingsFile = Join-Path -Path $PSScriptRoot -ChildPath settings.json
        }

        Write-Verbose "Loading file $SettingsFile"
        $settings = LoadSettingsFile -Path $SettingsFile

        ExtractData -Path $dataPath -DestinationPath $DestinationPath -Settings $settings
        ExtractSchema -Path $schemaPath -DestinationPath $DestinationPath
        CreateManifest -Path $DestinationPath -Settings $settings
    } elseif($Pack)  {

        $settingsPath = Join-Path -Path $Path -ChildPath "settings.json"

        if((Test-Path -Path $settingsPath) -eq $false) {
            Write-Error -Message "Could not find a settings.json file at $settingsPath. If the unpacked folder at $Path was created with Adoxio.Dynamics.DevOps, first run Compress-Data from Adoxio.Dynamics.DevOps on the unpacked folder to create the zip file, and then run Expand-Data with this version of CrmDataPackager to generate a compatible unpacked folder." -ErrorAction Stop
        }

        Write-Verbose "Loading file $settingsPath"
        $settings = ConvertFrom-Json -InputObject (Get-Content -Path $settingsPath -Raw)

        if($settings.version.StartsWith("1.0")) {
            Write-Error -Message "The unpacked folder at $Path was created with an incompatible version of CrmConfigurationPackager ($($settings.version)). To use Expand-CrmData from this version of CrmConfigurationPackager ($($MyInvocation.MyCommand.Module.Version)), first run Compress-Data from CrmConfigurationPackager v1.0 on the unpacked folder to create the zip file, and then run Expand-Data with this version of CrmDataPackager to generate a compatible unpacked folder." -ErrorAction Stop
        }

        # when target is a zip file, pack the contents to a temporary folder, then delete the temporary folder when done
        if([IO.Path]::GetExtension($DestinationPath) -eq '.zip') {
            $extractPath = Join-Path -Path ([IO.Path]::GetDirectoryName($DestinationPath)) -ChildPath (([IO.Path]::GetFileNameWithoutExtension(($DestinationPath)) + (Get-Date -Format '-yyyy-mm-dd-HHmmss')))
        } else {
            $extractPath = $DestinationPath
        }

        if(!$SchemaOnly -and !(Test-Path -Path $extractPath)) {
            Write-Verbose "Creating temporary folder $extractPath"
            New-Item -Path $extractPath -ItemType Directory | Out-Null
        }

        PackSchema -Path $Path -DestinationPath $extractPath

        if(!$SchemaOnly) {
            PackData -Path $Path -DestinationPath $extractPath -Settings $settings
            PackContentTypes -Path $Path -DestinationPath $extractPath

            # when target is a zip file, create the zip file, then delete the temporary folder
            if([IO.Path]::GetExtension($DestinationPath) -eq '.zip') {
                Write-Verbose "Writing file $DestinationPath"
                Compress-Archive -Path (Join-Path $extractPath '*') -DestinationPath $DestinationPath -Force
                Write-Verbose "Deleting temporary folder $extractPath"
                Remove-Item -Path $extractPath -Recurse -Force
            }
        }
    }
}

function GetFileNameField {
    param (
        $DefaultFileNameField,
        $EntityField
    )

    $fileNameField = if($EntityField -and 'fileNameField' -in $EntityField.PSObject.Properties.Name) { $EntityField.fileNameField } else { $DefaultFileNameField }

    return $fileNameField
}

function LoadFieldSettings {
    param (
        $SettingsField
    )

    # the default settings for an entity
    $fieldSettings = [PSCustomObject]@{
        field = ""
        fileNameField = "id"
        extension = ".txt"
        format = $false
        hash = $true
    }

    if($Field) {
        foreach($property in $Field.PSObject.Properties) {
            if($property.Name -in $fieldSettings.PSObject.Properties.Name) {
                $fieldSettings."$($property.Name)" = $property.Value
            }
        }
    }

    return $fieldSettings
}

function LoadEntitySettings {
    param (
        $Entity,
        $SettingsEntity
    )

    # the default settings for an entity
    $entitySettings = [PSCustomObject]@{
        entity = $Entity.name
        fileNameField = "id"
        extension = ".xml"
    }

    if($SettingsEntity) {
        # overwrite the default settings based on any matching provided values
        foreach($property in $SettingsEntity.PSObject.Properties) {
            if($property.Name -in $entitySettings.PSObject.Properties.Name) {
                $entitySettings."$($property.Name)" = $property.Value
            }
        }
    }

    return $entitySettings
}

function LoadSettingsFile {
    param (
        $Path
    )

    # the default settings
    $settings = [PSCustomObject]@{
        entities = @()
    }

    $fileSettings = ConvertFrom-Json (Get-Content -Raw -Path $Path)

    # overwrite the default settings based on any matching provided values
    foreach($property in $settings.PSObject.Properties) {
        if(Get-Member -InputObject $fileSettings -Name $property.Name) {
            $settings."$($property.Name)" = $fileSettings."$($property.Name)"
        }
    }

    return $settings
}