DSCResources/MSFT_xExchAutoMountPoint/MSFT_xExchAutoMountPoint.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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
function Get-TargetResource
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")]
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Identity,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagDatabasesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagVolumesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $DiskToDBMap,

        [parameter(Mandatory = $true)]
        [System.UInt32]
        $SpareVolumeCount,

        [System.Boolean]
        $EnsureExchangeVolumeMountPointIsLast = $false,

        [System.Boolean]
        $CreateSubfolders = $false,

        [ValidateSet("NTFS","REFS")]
        [System.String]
        $FileSystem = "NTFS",

        [System.String]
        $MinDiskSize = "",

        [ValidateSet("MBR","GPT")]
        [System.String]
        $PartitioningScheme = "GPT",

        [System.String]
        $UnitSize = "64K",

        [System.String]
        $VolumePrefix = "EXVOL"
    )

    #Load helper modules
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeCommon.psm1" -Verbose:0
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeDiskPart.psm1" -Verbose:0

    LogFunctionEntry -VerbosePreference $VerbosePreference

    GetDiskInfo

    $dbMap = GetDiskToDBMap -AutoDagDatabasesRootFolderPath $AutoDagDatabasesRootFolderPath

    $returnValue = @{
        Identity = $Identity
        DiskToDBMap = $dbMap
        SpareVolumeCount = $SpareVolumeCount
        AutoDagDatabasesRootFolderPath = $AutoDagDatabasesRootFolderPath
        AutoDagVolumesRootFolderPath = $AutoDagVolumesRootFolderPath
        VolumePrefix = $VolumePrefix
        MinDiskSize = $MinDiskSize
        UnitSize = $UnitSize
        PartitioningScheme = $PartitioningScheme
        FileSystem = $FileSystem
    }

    $returnValue
}

function Set-TargetResource
{
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSDSCUseVerboseMessageInDSCResource", "")]
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Identity,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagDatabasesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagVolumesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $DiskToDBMap,

        [parameter(Mandatory = $true)]
        [System.UInt32]
        $SpareVolumeCount,

        [System.Boolean]
        $EnsureExchangeVolumeMountPointIsLast = $false,

        [System.Boolean]
        $CreateSubfolders = $false,

        [ValidateSet("NTFS","REFS")]
        [System.String]
        $FileSystem = "NTFS",

        [System.String]
        $MinDiskSize = "",

        [ValidateSet("MBR","GPT")]
        [System.String]
        $PartitioningScheme = "GPT",

        [System.String]
        $UnitSize = "64K",

        [System.String]
        $VolumePrefix = "EXVOL"
    )

    #Load helper modules
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeCommon.psm1" -Verbose:0
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeDiskPart.psm1" -Verbose:0

    LogFunctionEntry -VerbosePreference $VerbosePreference

    #First see if we need to assign any disks to ExVol's
    GetDiskInfo
    
    $exVolCount = GetInUseMountPointCount -RootFolder $AutoDagVolumesRootFolderPath
    $requiredVolCount = $DiskToDBMap.Count + $SpareVolumeCount
   
    if ($exVolCount -lt $requiredVolCount)
    {
        CreateMissingExVolumes @PSBoundParameters -CurrentVolCount $exVolCount -RequiredVolCount $requiredVolCount
    }

    #Now see if we need any DB mount points
    GetDiskInfo

    $exDbCount = GetInUseMountPointCount -RootFolder $AutoDagDatabasesRootFolderPath
    $requiredDbCount = GetDesiredDatabaseCount -DiskToDBMap $DiskToDBMap
    
    if ($exDbCount -lt $requiredDbCount)
    {
        CreateMissingExDatabases @PSBoundParameters
    }

    #Now see if any Mount Points are ordered incorrectly. Jetstress wants ExchangeDatabase mount points to be listed before ExchangeVolume mount points
    GetDiskInfo

    if ($EnsureExchangeVolumeMountPointIsLast -eq $true)
    {
        while($true)
        {
            $volNum = VolumeMountPointNotLastInList -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath

            if ($volNum -ne -1)
            {
                SendVolumeMountPointToEndOfList -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath -VolumeNumber $volNum

                #Update DiskInfo for next iteration
                GetDiskInfo
            }
            else
            {
                break
            }
        }
    }
}

function Test-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Boolean])]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Identity,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagDatabasesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagVolumesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $DiskToDBMap,

        [parameter(Mandatory = $true)]
        [System.UInt32]
        $SpareVolumeCount,

        [System.Boolean]
        $EnsureExchangeVolumeMountPointIsLast = $false,

        [System.Boolean]
        $CreateSubfolders = $false,

        [ValidateSet("NTFS","REFS")]
        [System.String]
        $FileSystem = "NTFS",

        [System.String]
        $MinDiskSize = "",

        [ValidateSet("MBR","GPT")]
        [System.String]
        $PartitioningScheme = "GPT",

        [System.String]
        $UnitSize = "64K",

        [System.String]
        $VolumePrefix = "EXVOL"
    )

    #Load helper modules
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeCommon.psm1" -Verbose:0
    Import-Module "$((Get-Item -LiteralPath "$($PSScriptRoot)").Parent.Parent.FullName)\Misc\xExchangeDiskPart.psm1" -Verbose:0

    LogFunctionEntry -VerbosePreference $VerbosePreference

    GetDiskInfo

    #Check if the number of assigned EXVOL's is less than the requested number of DB disks plus spares
    $mountPointCount = GetInUseMountPointCount -RootFolder $AutoDagVolumesRootFolderPath

    if ($mountPointCount -lt ($DiskToDBMap.Count + $SpareVolumeCount))
    {
        ReportBadSetting -SettingName "MountPointCount" -ExpectedValue ($DiskToDBMap.Count + $SpareVolumeCount) -ActualValue $mountPointCount -VerbosePreference $VerbosePreference
        return $false
    }
    else #Loop through all requested DB's and see if they have a mount point yet
    {
        foreach ($value in $DiskToDBMap)
        {
            foreach ($db in $value.Split(','))
            {
                if ((DBHasMountPoint -AutoDagDatabasesRootFolderPath $AutoDagDatabasesRootFolderPath -DB $db) -eq $false)
                {
                    ReportBadSetting -SettingName "DB '$($db)' Has Mount Point" -ExpectedValue $true -ActualValue $false -VerbosePreference $VerbosePreference
                    return $false
                }
            }
        }
    }

    #Now check if any ExchangeVolume mount points are higher ordered than ExchangeDatabase mount points. ExchangeDatabase MP's must be listed first for logical disk counters to function properly
    if ($EnsureExchangeVolumeMountPointIsLast -eq $true -and (VolumeMountPointNotLastInList -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath) -ne -1)
    {
        Write-Verbose "One or more volumes have an $($AutoDagVolumesRootFolderPath) mount point ordered before a $($AutoDagDatabasesRootFolderPath) mount point"
        return $false
    }

    return $true
}

#Creates mount points for any Exchange Volumes we are missing
function CreateMissingExVolumes
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Identity,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagDatabasesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagVolumesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $DiskToDBMap,

        [parameter(Mandatory = $true)]
        [System.UInt32]
        $SpareVolumeCount,

        [System.Boolean]
        $CreateSubfolders = $false,

        [ValidateSet("NTFS","REFS")]
        [System.String]
        $FileSystem = "NTFS",

        [System.String]
        $MinDiskSize = "",

        [ValidateSet("MBR","GPT")]
        [System.String]
        $PartitioningScheme = "GPT",

        [System.String]
        $UnitSize = "64K",

        [System.String]
        $VolumePrefix = "EXVOL",

        [System.Int32]
        $CurrentVolCount,

        [System.Int32]
        $RequiredVolCount
    )

    for ($i = $CurrentVolCount; $i -lt $RequiredVolCount; $i++)
    {
        if ($i -ne $CurrentVolCount) #Need to update disk info if we've gone through the loop already
        {
            GetDiskInfo
        }

        $firstDisk = FindFirstAvailableDisk -MinDiskSize $MinDiskSize

        if ($firstDisk -ne -1)
        {
            $firstVolume = FindFirstAvailableVolumeNumber -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath -VolumePrefix $VolumePrefix

            if ($firstVolume -ne -1)
            {
                $volPath = Join-Path -Path "$($AutoDagVolumesRootFolderPath)" -ChildPath "$($VolumePrefix)$($firstVolume)"

                PrepareVolume -DiskNumber $firstDisk -Folder $volPath -FileSystem $FileSystem -UnitSize $UnitSize -PartitioningScheme $PartitioningScheme -Label "$($VolumePrefix)$($firstVolume)"
            }
            else
            {
                throw "Unable to find a free volume number to use when naming the volume folder"
            }
        }
        else
        {
            throw "No available disks to assign an Exchange Volume mount point to"
        }
    }
}

#Looks for databases that have never had a mount point created, and gives them a mount point
function CreateMissingExDatabases
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory = $true)]
        [System.String]
        $Identity,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagDatabasesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String]
        $AutoDagVolumesRootFolderPath,

        [parameter(Mandatory = $true)]
        [System.String[]]
        $DiskToDBMap,

        [parameter(Mandatory = $true)]
        [System.UInt32]
        $SpareVolumeCount,

        [System.Boolean]
        $CreateSubfolders = $false,

        [ValidateSet("NTFS","REFS")]
        [System.String]
        $FileSystem = "NTFS",

        [System.String]
        $MinDiskSize = "",

        [ValidateSet("MBR","GPT")]
        [System.String]
        $PartitioningScheme = "GPT",

        [System.String]
        $UnitSize = "64K",

        [System.String]
        $VolumePrefix = "EXVOL"
    )

    for ($i = 0; $i -lt $DiskToDBMap.Count; $i++)
    {
        if ($i -gt 0) #Need to refresh current disk info
        {
            GetDiskInfo
        }

        [string[]]$dbsNeedingMountPoints = @()

        [string[]]$allDBsRequestedForDisk = $DiskToDBMap[$i].Split(',')

        for ($j = 0; $j -lt $allDBsRequestedForDisk.Count; $j++)
        {
            $current = $allDBsRequestedForDisk[$j]

            $path = Join-Path -Path "$($AutoDagDatabasesRootFolderPath)" -ChildPath "$($current)"

            #We only want to touch datases who have never had a mount point created. After that, AutoReseed will handle it.
            if ((Test-Path -Path "$($path)") -eq $false)
            {
                $dbsNeedingMountPoints += $current
            }
            else #Since the folder already exists, need to check and error if the mount point doesn't
            {
                if ((MountPointExists -Path $path) -eq -1)
                {
                    throw "Database '$($current)' already has a folder on disk at '$($path)', but does not have a mount point. This must be manually corrected for xAutoMountPoint to proceed."
                }
            }
        }

        if ($dbsNeedingMountPoints.Count -eq $allDBsRequestedForDisk.Count) #No DB mount points for this disk have been created yet
        {
            $targetVolume = GetExchangeVolume -AutoDagDatabasesRootFolderPath $AutoDagDatabasesRootFolderPath -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath -DBsPerDisk $allDBsRequestedForDisk.Count -VolumePrefix $VolumePrefix
        }
        elseif ($dbsNeedingMountPoints.Count -gt 0) #We just need to create some mount points
        {
            $existingDB = ""

            #Find a DB that's already had its mount point created
            foreach ($db in $allDBsRequestedForDisk)
            {
                if (($dbsNeedingMountPoints.Contains($db) -eq $false))
                {
                    $existingDB = $db
                    break
                }
            }

            if ($existingDB -ne "")
            {
                $targetVolume = GetExchangeVolume -AutoDagDatabasesRootFolderPath $AutoDagDatabasesRootFolderPath -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath -ExistingDB $existingDB -DBsPerDisk $allDBsRequestedForDisk.Count -DBsToCreate $dbsNeedingMountPoints.Count -VolumePrefix $VolumePrefix
            }
        }
        else #All DB's requested for this disk are good. Just continue on in the loop
        {
            continue
        }

        if ($null -ne $targetVolume)
        {
            if ($targetVolume -ne -1)
            {
                foreach ($db in $dbsNeedingMountPoints)
                {
                    $path = Join-Path -Path "$($AutoDagDatabasesRootFolderPath)" -ChildPath "$($db)"

                    AddMountPoint -VolumeNumber $targetVolume -Folder $path

                    if ($CreateSubfolders -eq $true)
                    {
                        $dbFolder = Join-Path -Path "$($path)" -ChildPath "$($db).db"
                        $logFolder = Join-Path -Path "$($path)" -ChildPath "$($db).log"

                        if ((Test-Path -LiteralPath "$($dbFolder)") -eq $false)
                        {
                            mkdir -Path "$($dbFolder)"
                        }

                        if ((Test-Path -LiteralPath "$($logFolder)") -eq $false)
                        {
                            mkdir -Path "$($logFolder)"
                        }
                    }
                }
            }
            else
            {
                throw "Unable to find a volume to place mount points for the following databases: '$($dbsNeedingMountPoints)'"
            }
        }
    }
}

#Builds a map of the DBs that already exist on disk
function GetDiskToDBMap
{
    param([string]$AutoDagDatabasesRootFolderPath)

    #Get the DB path to a point where we know there will be a trailing \
    $dbpath = Join-Path -Path "$($AutoDagDatabasesRootFolderPath)" -ChildPath ""

    #Keep track of a disk number for putting in the map
    $i = 0

    #Will be the return value for DiskToDBMap
    [string[]]$dbMap = @()

    #Loop through all existing mount points and figure out which ones are for DB's
    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        [string]$mountPoints = ""

        foreach ($mountPoint in $global:VolumeToMountPointMap[$key])
        {
            if ($mountPoint.StartsWith($dbpath))
            {
                $startIndex = $dbpath.Length
                $endIndex = $mountPoint.IndexOf("\", $startIndex)
                $dbName = $mountPoint.Substring($startIndex, $endIndex - $startIndex)

                if ($mountPoints -eq "")
                {
                    $mountPoints = $dbName
                }
                else
                {
                    $mountPoints += ",$($dbName)"
                }
            }
        }

        if ($mountPoints.Length -gt 0)
        {
            $dbMap += $mountPoints
        }
    }

    return $dbMap
}

#Looks for a volume where an Exchange Volume or Database mount point can be added.
#If ExistingDB is not specified, looks for a spare volume that has no mount points yet.
#If ExistingDB is specified, finds the volume number where that DB exists, only if there is room to
#create the requested database mount points.
function GetExchangeVolume
{
    param([string]$AutoDagDatabasesRootFolderPath, [string]$AutoDagVolumesRootFolderPath, [string]$ExistingDB = "", [Uint32]$DBsPerDisk, [Uint32]$DBsToCreate, [string]$VolumePrefix = "EXVOL")

    $targetVol = -1 #Our return variable

    [object[]]$keysSorted = GetSortedExchangeVolumeKeys -AutoDagDatabasesRootFolderPath $AutoDagDatabasesRootFolderPath -AutoDagVolumesRootFolderPath $AutoDagVolumesRootFolderPath -VolumePrefix $VolumePrefix
    
    #Loop through every volume
    foreach ($key in $keysSorted)
    {
        [int]$intKey = $key

        #Get mount points for this volume
        [string[]]$mountPoints = $global:VolumeToMountPointMap[$intKey]

        $hasExVol = $false #Whether any ExVol mount points exist on this disk
        $hasExDb = $false #Whether any ExDB mount points exist on this disk
        $hasExistingDB = $false #Whether $ExistingDB exists as a mount point on this disk

        #Inspect each individual mount point
        foreach($mountPoint in $mountPoints)
        {
            if ($mountPoint.StartsWith($AutoDagVolumesRootFolderPath))
            {
                $hasExVol = $true
            }
            elseif ($mountPoint.StartsWith($AutoDagDatabasesRootFolderPath))
            {
                $hasExDb = $true

                $path = Join-Path -Path "$($AutoDagDatabasesRootFolderPath)" -ChildPath "$($ExistingDB)"

                if ($mountPoint.StartsWith($path))
                {
                    $hasExistingDB = $true
                }
            }
        }

        if ($ExistingDB -eq "")
        {
            if ($hasExVol -eq $true -and $hasExDb -eq $false)
            {
                $targetVol = $intKey
                break
            }
        }
        else
        {
            if ($hasExVol -eq $true -and $hasExistingDB -eq $true)
            {
                if (($mountPoints.Count + $DBsToCreate) -le ($DBsPerDisk + 1))
                {
                    $targetVol = $intKey
                }

                break
            }
        }
    }

    return $targetVol
}

function GetSortedExchangeVolumeKeys
{
    param([string]$AutoDagDatabasesRootFolderPath, [string]$AutoDagVolumesRootFolderPath, [string]$VolumePrefix = "EXVOL")

    [string[]]$sortedKeys = @() #The return value

    [string]$pathBeforeVolumeNumber = Join-Path -Path $AutoDagVolumesRootFolderPath -ChildPath $VolumePrefix

    #First extract the actual volume number as an Int from the volume path, then add it to a new hashtable with the same key value
    [Hashtable]$tempVolumeToMountPointMap = @{}

    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        $volPath = ""

        #Loop through each mount point on this volume and find the EXVOL mount point
        foreach ($value in $VolumeToMountPointMap[$key])
        {
            if ($value.StartsWith($pathBeforeVolumeNumber))
            {
                $volPath = $value
                break
            }
        }

        if ($volPath.StartsWith($pathBeforeVolumeNumber))
        {
            if ($volPath.EndsWith("\") -or $volPath.EndsWith("/"))
            {
                [string]$exVolNumberStr = $volPath.Substring($pathBeforeVolumeNumber.Length, ($volPath.Length - $pathBeforeVolumeNumber.Length - 1))
            }
            else
            {
                [string]$exVolNumberStr = $volPath.Substring($pathBeforeVolumeNumber.Length, ($volPath.Length - $pathBeforeVolumeNumber.Length))
            }
            
            [int]$exVolNumber = [int]::Parse($exVolNumberStr)
            $tempVolumeToMountPointMap.Add($key, $exVolNumber)
        }
    }

    #Now go through the volume numbers, and add the keys to the return array in sorted value order
    while ($tempVolumeToMountPointMap.Count -gt 0)
    {
        [object[]]$keys = $tempVolumeToMountPointMap.Keys
        [int]$lowestKey = $keys[0]
        [int]$lowestValue = $tempVolumeToMountPointMap[$keys[0]]

        for ($i = 1; $i -lt $tempVolumeToMountPointMap.Count; $i++)
        {
            [int]$currentValue = $tempVolumeToMountPointMap[$keys[$i]]

            if ($currentValue -lt $lowestValue)
            {
                $lowestKey = $keys[$i]
                $lowestValue = $currentValue
            }
        }

        $sortedKeys += $lowestKey
        $tempVolumeToMountPointMap.Remove($lowestKey)
    }

    return $sortedKeys
}

#Finds the lowest disk number that doesn't have any volumes associated, and is larger than the requested size
function FindFirstAvailableDisk
{
    param([string]$MinDiskSize = "")

    $diskNum = -1

    foreach ($key in $global:DiskToVolumeMap.Keys)
    {
        if ($global:DiskToVolumeMap[$key].Count -eq 0 -and ($key -lt $diskNum -or $diskNum -eq -1))
        {
            if ($MinDiskSize -ne "")
            {
                [Uint64]$minSize = 0 + $MinDiskSize.Replace(" ", "")
                [Uint64]$actualSize = 0 + $global:DiskSizeMap[$key].Replace(" ", "")

                if ($actualSize -gt $minSize)
                {
                    $diskNum = $key
                }
            }
            else
            {
                $diskNum = $key
            }
        }
    }

    return $diskNum
}

#Looks in the volumes root folder and finds the first number we can give to a volume folder
#based off of what folders have already been created
function FindFirstAvailableVolumeNumber
{
    param([string]$AutoDagVolumesRootFolderPath, [string]$VolumePrefix)

    if((Test-Path -LiteralPath "$($AutoDagVolumesRootFolderPath)") -eq $false) #If the ExVol folder doesn't already exist, then we can start with 1
    {
        return 1
    }

    $currentFolders = Get-ChildItem -LiteralPath "$($AutoDagVolumesRootFolderPath)" | where {$_.GetType().Name -eq "DirectoryInfo"} | Sort-Object

    for ($i = 1; $i -lt 999; $i++)
    {
        $existing = $null
        $existing = $currentFolders | where {$_.Name -eq "$($VolumePrefix)$($i)"}

        if ($null -eq $existing)
        {
            return $i
        }
    }

    return -1
}

#Counts and returns the number of DB's in the disk to db map
function GetDesiredDatabaseCount
{
    param([string[]]$DiskToDBMap)

    $count = 0

    foreach ($value in $DiskToDBMap)
    {
        $count += $value.Split(',').Count
    }

    return $count
}

#Checks if a database already has a mountpoint created
function DBHasMountPoint
{
    param([string]$AutoDagDatabasesRootFolderPath, [string]$DB)

    $dbPath = Join-Path -Path "$($AutoDagDatabasesRootFolderPath)" -ChildPath "$($DB)"

    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        foreach ($mountPoint in $global:VolumeToMountPointMap[$key])
        {
            if ($mountPoint.StartsWith($dbPath))
            {
                return $true
            }
        }
    }

    return $false
}

#Gets the count of in use mount points matching the given critera
function GetInUseMountPointCount
{
    param([string]$RootFolder)

    $count = 0

    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        foreach ($mountPoint in $global:VolumeToMountPointMap[$key])
        {
            if ($mountPoint.StartsWith($RootFolder))
            {
                $count++
            }
        }
    }

    return $count
}

#Checks all volumes, and sees if any of them have ExchangeVolume mount points that show up before other (like ExchangeDatabase) mount points.
#If so, it returns the volume number. If not, it returns -1
function VolumeMountPointNotLastInList
{
    param([string]$AutoDagVolumesRootFolderPath)

    foreach ($key in $global:VolumeToMountPointMap.Keys)
    {
        $values = $global:VolumeToMountPointMap[$key]

        if ($null -ne $values)
        {
            for ($i = 0; $i -lt $values.Count; $i++)
            {
                if ($values[$i].StartsWith($AutoDagVolumesRootFolderPath) -eq $true -and $i -lt ($values.Count - 1))
                {
                    return $key
                }
            }
        }
    }

    return -1
}

#For volumes that have multiple mount points including an ExchangeVolume mount point, sends removes and re-adds the ExchangeVolume
#mount point so that it is at the end of the list of mount points
function SendVolumeMountPointToEndOfList
{
    [CmdletBinding()]
    param([string]$AutoDagVolumesRootFolderPath, [Int32]$VolumeNumber)

    $values = $global:VolumeToMountPointMap[$VolumeNumber]

    foreach ($folderName in $values)
    {
        if ($folderName.StartsWith($AutoDagVolumesRootFolderPath))
        {
            if ($folderName.EndsWith("\"))
            {
                $folderName = $folderName.Substring(0, $folderName.Length - 1)
            }

            StartDiskpart -Commands "select volume $($VolumeNumber)","remove mount=`"$($folderName)`"","assign mount=`"$($folderName)`"" -VerbosePreference $VerbosePreference | Out-Null
            break
        }
    }
}

#Takes an empty disk, initalizes and formats it, and gives it an ExchangeVolume mount point
function PrepareVolume
{
    [CmdletBinding()]
    param([int]$DiskNumber, [string]$Folder, [ValidateSet("NTFS","REFS")][string]$FileSystem = "NTFS", [string]$UnitSize, [string]$PartitioningScheme, [string]$Label)
    
    #Initialize the disk and put in MBR format
    StartDiskpart -Commands "select disk $($DiskNumber)","clean" -VerbosePreference $VerbosePreference | Out-Null
    StartDiskpart -Commands "select disk $($DiskNumber)","online disk" -VerbosePreference $VerbosePreference | Out-Null
    StartDiskpart -Commands "select disk $($DiskNumber)","attributes disk clear readonly","convert MBR" -VerbosePreference $VerbosePreference | Out-Null
    StartDiskpart -Commands "select disk $($DiskNumber)","offline disk" -VerbosePreference $VerbosePreference | Out-Null
 
    #Online the disk
    StartDiskpart -Commands "select disk $($DiskNumber)","attributes disk clear readonly","online disk" -VerbosePreference $VerbosePreference | Out-Null

    #Convert to GPT if requested
    if ($PartitioningScheme -eq "GPT")
    {
        StartDiskpart -Commands "select disk $($DiskNumber)","convert GPT noerr" -VerbosePreference $VerbosePreference | Out-Null
    }

    #Create the directory if it doesn't exist
    if ((Test-Path $Folder) -eq $False)
    {
        mkdir -Path "$($Folder)" | Out-Null
    }    

    #Create the partition and format the drive
    if ($FileSystem -eq "NTFS")
    {
        $formatString = "Format FS=$($FileSystem) UNIT=$($UnitSize) Label=$($Label) QUICK"

        StartDiskpart -Commands "select disk $($DiskNumber)","create partition primary","$($formatString)","assign mount=`"$($Folder)`"" -VerbosePreference $VerbosePreference | Out-Null
    }
    else #if ($FileSystem -eq "REFS")
    {
        StartDiskpart -Commands "select disk $($DiskNumber)","create partition primary" -VerbosePreference $VerbosePreference | Out-Null
        
        if ($UnitSize.ToLower().EndsWith("k"))
        {
            $UnitSizeBytes = [UInt64]::Parse($UnitSize.Substring(0, $UnitSize.Length - 1)) * 1024
        }
        else
        {
            $UnitSizeBytes = $UnitSize
        }

        Write-Verbose "Sleeping for 15 seconds after partition creation."

        Start-Sleep -Seconds 15

        Get-Partition -DiskNumber $DiskNumber -PartitionNumber 2| Format-Volume -AllocationUnitSize $UnitSizeBytes -FileSystem REFS -NewFileSystemLabel $Label -SetIntegrityStreams:$false -Confirm:$false
        Add-PartitionAccessPath -DiskNumber $DiskNumber -PartitionNumber 2 -AccessPath $Folder -PassThru | Set-Partition -NoDefaultDriveLetter $true
    }
}

#Adds a mount point to an existing volume
function AddMountPoint
{
    [CmdletBinding()]
    param([int]$VolumeNumber, [string]$Folder)

    #Create the directory if it doesn't exist
    if ((Test-Path $Folder) -eq $False)
    {
        mkdir -Path "$($Folder)" | Out-Null
    }

    StartDiskpart -Commands "select volume $($VolumeNumber)","assign mount=`"$($Folder)`"" -VerbosePreference $VerbosePreference | Out-Null
}


Export-ModuleMember -Function *-TargetResource