DSCResources/DSC_SmbShare/DSC_SmbShare.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
$modulePath = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'Modules'

# Import the ComputerManagementDsc Common Modules
Import-Module -Name (Join-Path -Path $modulePath `
        -ChildPath (Join-Path -Path 'ComputerManagementDsc.Common' `
            -ChildPath 'ComputerManagementDsc.Common.psm1')) -Force

# Import the ComputerManagementDsc Resource Helper Module
Import-Module -Name (Join-Path -Path $modulePath `
        -ChildPath (Join-Path -Path 'ComputerManagementDsc.Common' `
            -ChildPath 'ComputerManagementDsc.Common.psm1'))

# Import Localization Strings
$script:localizedData = Get-LocalizedData -ResourceName 'DSC_SmbShare'

<#
    .SYNOPSIS
        Returns the current state of the SMB share.
 
    .PARAMETER Name
        Specifies the name of the SMB share.
 
    .PARAMETER Path
        Specifies the path of the SMB share.
 
        Not used in Get-TargetResource.
#>

function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [Parameter(Mandatory = $true)]
        [System.String]
        $Path
    )

    Write-Verbose -Message ($script:localizedData.GetTargetResourceMessage -f $Name)

    $returnValue = @{
        Ensure                = 'Absent'
        Name                  = $Name
        Path                  = [System.String] $null
        Description           = [System.String] $null
        ConcurrentUserLimit   = 0
        EncryptData           = $false
        FolderEnumerationMode = [System.String] $null
        CachingMode           = [System.String] $null
        ContinuouslyAvailable = $false
        ShareState            = [System.String] $null
        ShareType             = [System.String] $null
        ShadowCopy            = $false
        Special               = $false
        ScopeName             = [System.String] $null
    }

    $accountsFullAccess   = [system.string[]] @()
    $accountsChangeAccess = [system.string[]] @()
    $accountsReadAccess   = [system.string[]] @()
    $accountsNoAccess     = [system.string[]] @()

    $smbShare = Get-SmbShare -Name $Name -ErrorAction 'SilentlyContinue'

    if ($smbShare)
    {
        $returnValue['Ensure'] = 'Present'
        $returnValue['Name'] = $smbShare.Name
        $returnValue['Path'] = $smbShare.Path
        $returnValue['Description'] = $smbShare.Description
        $returnValue['ConcurrentUserLimit'] = $smbShare.ConcurrentUserLimit
        $returnValue['EncryptData'] = $smbShare.EncryptData
        $returnValue['FolderEnumerationMode'] = $smbShare.FolderEnumerationMode.ToString()
        $returnValue['CachingMode'] = $smbShare.CachingMode.ToString()
        $returnValue['ContinuouslyAvailable'] = $smbShare.ContinuouslyAvailable
        $returnValue['ShareState'] = $smbShare.ShareState.ToString()
        $returnValue['ShareType'] = $smbShare.ShareType.ToString()
        $returnValue['ShadowCopy'] = $smbShare.ShadowCopy
        $returnValue['Special'] = $smbShare.Special
        $returnValue['ScopeName'] = $smbShare.ScopeName

        $currentSmbShareAccessPermissions = Get-SmbShareAccess -Name $Name

        foreach ($access in $currentSmbShareAccessPermissions)
        {
            switch ($access.AccessRight)
            {
                'Change'
                {
                    if ($access.AccessControlType -eq 'Allow')
                    {
                        $accountsChangeAccess += @($access.AccountName)
                    }
                }

                'Read'
                {
                    if ($access.AccessControlType -eq 'Allow')
                    {
                        $accountsReadAccess += @($access.AccountName)
                    }
                }

                'Full'
                {
                    if ($access.AccessControlType -eq 'Allow')
                    {
                        $accountsFullAccess += @($access.AccountName)
                    }

                    if ($access.AccessControlType -eq 'Deny')
                    {
                        $accountsNoAccess += @($access.AccountName)
                    }
                }
            }
        }
    }
    else
    {
        Write-Verbose -Message ($script:localizedData.ShareNotFound -f $Name)
    }

    <#
        This adds either an empty array, or a populated array depending
        if accounts with the respectively access was found.
    #>

    $returnValue['FullAccess'] = [System.String[]] $accountsFullAccess
    $returnValue['ChangeAccess'] = [System.String[]] $accountsChangeAccess
    $returnValue['ReadAccess'] = [System.String[]] $accountsReadAccess
    $returnValue['NoAccess'] = [System.String[]] $accountsNoAccess

    return $returnValue
}

<#
    .SYNOPSIS
        Creates or removes the SMB share.
 
    .PARAMETER Name
        Specifies the name of the SMB share.
 
    .PARAMETER Path
        Specifies the path of the SMB share.
 
    .PARAMETER Description
        Specifies the description of the SMB share.
 
    .PARAMETER ConcurrentUserLimit
        Specifies the maximum number of concurrently connected users that the
        new SMB share may accommodate. If this parameter is set to zero (0),
        then the number of users is unlimited. The default value is zero (0).
 
    .PARAMETER EncryptData
        Indicates that the SMB share is encrypted.
 
    .PARAMETER FolderEnumerationMode
        Specifies which files and folders in the new SMB share are visible to
        users. { AccessBased | Unrestricted }
 
    .PARAMETER CachingMode
        Specifies the caching mode of the offline files for the SMB share.
        { 'None' | 'Manual' | 'Programs' | 'Documents' | 'BranchCache' }
 
    .PARAMETER ContinuouslyAvailable
        Specifies whether the SMB share should be continuously available.
 
    .PARAMETER FullAccess
        Specifies which accounts are granted full permission to access the
        SMB share.
 
    .PARAMETER ChangeAccess
        Specifies which accounts will be granted modify permission to access the
        SMB share.
 
    .PARAMETER ReadAccess
        Specifies which accounts is granted read permission to access the SMB share.
 
    .PARAMETER NoAccess
        Specifies which accounts are denied access to the SMB share.
 
    .PARAMETER Ensure
        Specifies if the SMB share should be added or removed.
 
    .PARAMETER ScopeName
        Specifies the scope in which the share should be created.
 
    .PARAMETER Force
        Specifies if the SMB share is allowed to be dropped and recreated (required
        when the path changes).
#>

function Set-TargetResource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [Parameter(Mandatory = $true)]
        [System.String]
        $Path,

        [Parameter()]
        [System.String]
        $Description,

        [Parameter()]
        [System.UInt32]
        $ConcurrentUserLimit,

        [Parameter()]
        [System.Boolean]
        $EncryptData,

        [Parameter()]
        [ValidateSet('AccessBased', 'Unrestricted')]
        [System.String]
        $FolderEnumerationMode,

        [Parameter()]
        [ValidateSet('None', 'Manual', 'Programs', 'Documents', 'BranchCache')]
        [System.String]
        $CachingMode,

        [Parameter()]
        [System.Boolean]
        $ContinuouslyAvailable,

        [Parameter()]
        [System.String[]]
        $FullAccess,

        [Parameter()]
        [System.String[]]
        $ChangeAccess,

        [Parameter()]
        [System.String[]]
        $ReadAccess,

        [Parameter()]
        [System.String[]]
        $NoAccess,

        [Parameter()]
        [ValidateSet('Present', 'Absent')]
        [System.String]
        $Ensure = 'Present',

        [Parameter()]
        [System.String]
        $ScopeName = '*',

        [Parameter()]
        [System.Boolean]
        $Force
    )

    Assert-AccessPermissionParameters @PSBoundParameters

    <#
        Copy the $PSBoundParameters to a new hash table, so we have the
        original intact.
    #>

    $smbShareParameters = @{} + $PSBoundParameters

    $currentSmbShareConfiguration = Get-TargetResource -Name $Name -Path $Path

    if ($currentSmbShareConfiguration.Ensure -eq 'Present')
    {
        Write-Verbose -Message ($script:localizedData.IsPresent -f $Name)

        if ($Ensure -eq 'Present')
        {
            if (
                ($currentSmbShareConfiguration.Path -ne $Path -or
                $currentSmbShareConfiguration.ScopeName -ne $ScopeName) -and
                $Force
            )
            {
                Write-Verbose -Message ($script:localizedData.RecreateShare -f $Name)

                try
                {
                    Remove-SmbShare -Name $Name -Force -ErrorAction Stop
                    New-SmbShare -Name $Name -Path $Path -ErrorAction Stop
                }
                catch
                {
                    Write-Error -Message ($script:localizedData.RecreateShareError -f $Name, $_)
                }
            }
            else
            {
                Write-Warning -Message (
                    $script:localizedData.NoRecreateShare -f $Name, $currentSmbShareConfiguration.Path, $Path
                )
            }

            Write-Verbose -Message $script:localizedData.UpdatingProperties

            $parametersToRemove = $smbShareParameters.Keys |
                Where-Object -FilterScript {
                    $_ -in ('ChangeAccess','ReadAccess','FullAccess','NoAccess','Ensure','Path','Force')
                }

            $parametersToRemove | ForEach-Object -Process {
                $smbShareParameters.Remove($_)
            }

            # Use Set-SmbShare for performing operations other than changing access
            Set-SmbShare @smbShareParameters -Force -ErrorAction 'Stop'

            $smbShareAccessPermissionParameters = @{
                Name = $Name
            }

            if ($PSBoundParameters.ContainsKey('FullAccess'))
            {
                $smbShareAccessPermissionParameters['FullAccess'] = $FullAccess
            }

            if ($PSBoundParameters.ContainsKey('ChangeAccess'))
            {
                $smbShareAccessPermissionParameters['ChangeAccess'] = $ChangeAccess
            }

            if ($PSBoundParameters.ContainsKey('ReadAccess'))
            {
                $smbShareAccessPermissionParameters['ReadAccess'] = $ReadAccess
            }

            if ($PSBoundParameters.ContainsKey('NoAccess'))
            {
                $smbShareAccessPermissionParameters['NoAccess'] = $NoAccess
            }

            # We should only pass the access collections that the user wants to enforce.
            Remove-SmbShareAccessPermission @smbShareAccessPermissionParameters

            Add-SmbShareAccessPermission @smbShareAccessPermissionParameters
        }
        else
        {
            Write-Verbose -Message ($script:localizedData.RemoveShare -f $Name)

            Remove-SmbShare -name $Name -Force -ErrorAction 'Stop'
        }
    }
    else
    {
        if ($Ensure -eq 'Present')
        {
            $smbShareParameters.Remove('Ensure')
            $smbShareParameters.Remove('Force')

            Write-Verbose -Message ($script:localizedData.CreateShare -f $Name)

            <#
                Remove access collections that are empty, since empty
                collections are not allowed to be provided to the cmdlet
                New-SmbShare.
            #>

            foreach ($accessProperty in ('ChangeAccess','ReadAccess','FullAccess','NoAccess'))
            {
                if ($smbShareParameters.ContainsKey($accessProperty) -and -not $smbShareParameters[$accessProperty])
                {
                    $smbShareParameters.Remove($accessProperty)
                }
            }

            New-SmbShare @smbShareParameters -ErrorAction 'Stop'
        }
    }
}

<#
    .SYNOPSIS
        Determines if the SMB share is in the desired state.
 
    .PARAMETER Name
        Specifies the name of the SMB share.
 
    .PARAMETER Path
        Specifies the path of the SMB share.
 
    .PARAMETER Description
        Specifies the description of the SMB share.
 
    .PARAMETER ConcurrentUserLimit
        Specifies the maximum number of concurrently connected users that the
        new SMB share may accommodate. If this parameter is set to zero (0),
        then the number of users is unlimited. The default value is zero (0).
 
    .PARAMETER EncryptData
        Indicates that the SMB share is encrypted.
 
    .PARAMETER FolderEnumerationMode
        Specifies which files and folders in the new SMB share are visible to
        users. { AccessBased | Unrestricted }
 
    .PARAMETER CachingMode
        Specifies the caching mode of the offline files for the SMB share.
        { 'None' | 'Manual' | 'Programs' | 'Documents' | 'BranchCache' }
 
    .PARAMETER ContinuouslyAvailable
        Specifies whether the SMB share should be continuously available.
 
    .PARAMETER FullAccess
        Specifies which accounts are granted full permission to access the
        SMB share.
 
    .PARAMETER ChangeAccess
        Specifies which accounts will be granted modify permission to access the
        SMB share.
 
    .PARAMETER ReadAccess
        Specifies which accounts is granted read permission to access the SMB share.
 
    .PARAMETER NoAccess
        Specifies which accounts are denied access to the SMB share.
 
    .PARAMETER Ensure
        Specifies if the SMB share should be added or removed.
 
    .PARAMETER ScopeName
        Specifies the scope in which the share should be created.
 
    .PARAMETER Force
        Specifies if the SMB share is allowed to be dropped and recreated (required
        when the path changes).
#>

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

        [Parameter(Mandatory = $true)]
        [System.String]
        $Path,

        [Parameter()]
        [System.String]
        $Description,

        [Parameter()]
        [System.UInt32]
        $ConcurrentUserLimit,

        [Parameter()]
        [System.Boolean]
        $EncryptData,

        [Parameter()]
        [ValidateSet('AccessBased', 'Unrestricted')]
        [System.String]
        $FolderEnumerationMode,

        [Parameter()]
        [ValidateSet('None', 'Manual', 'Programs', 'Documents', 'BranchCache')]
        [System.String]
        $CachingMode,

        [Parameter()]
        [System.Boolean]
        $ContinuouslyAvailable,

        [Parameter()]
        [System.String[]]
        $FullAccess,

        [Parameter()]
        [System.String[]]
        $ChangeAccess,

        [Parameter()]
        [System.String[]]
        $ReadAccess,

        [Parameter()]
        [System.String[]]
        $NoAccess,

        [Parameter()]
        [ValidateSet('Present', 'Absent')]
        [System.String]
        $Ensure = 'Present',

        [Parameter()]
        [System.String]
        $ScopeName = '*',

        [Parameter()]
        [System.Boolean]
        $Force
    )

    $null = $PSBoundParameters.Remove('Force')

    Assert-AccessPermissionParameters @PSBoundParameters

    Write-Verbose -Message ($script:localizedData.TestTargetResourceMessage -f $Name)

    $resourceRequiresUpdate = $false

    $currentSmbShareConfiguration = Get-TargetResource -Name $Name -Path $Path

    if ($currentSmbShareConfiguration.Ensure -eq $Ensure)
    {
        if ($Ensure -eq 'Present')
        {
            Write-Verbose -Message (
                '{0} {1}' -f `
                    ($script:localizedData.IsPresent -f $Name),
                    $script:localizedData.EvaluatingProperties
            )

            <#
                Using $VerbosePreference so that the verbose messages in
                Test-DscParameterState is outputted, if the user requested
                verbose messages.
            #>

            $resourceRequiresUpdate = Test-DscParameterState `
                -CurrentValues $currentSmbShareConfiguration `
                -DesiredValues $PSBoundParameters `
                -Verbose:$VerbosePreference
        }
        else
        {
            Write-Verbose -Message ($script:localizedData.IsAbsent -f $Name)

            $resourceRequiresUpdate = $true
        }
    }

    return $resourceRequiresUpdate
}

<#
    .SYNOPSIS
        Removes the access permission for accounts that are no longer part
        of the respectively access collections (FullAccess, ChangeAccess,
        ReadAccess, and NoAccess).
 
    .PARAMETER Name
        The name of the SMB share for which to remove access permission.
 
    .PARAMETER FullAccess
        A string collection of account names that _should have_ full access
        permission. The accounts not in this collection will be removed from
        the SMB share.
 
    .PARAMETER ChangeAccess
        A string collection of account names that _should have_ change access
        permission. The accounts not in this collection will be removed from
        the SMB share.
 
    .PARAMETER ReadAccess
        A string collection of account names that _should have_ read access
        permission. The accounts not in this collection will be removed from
        the SMB share.
 
    .PARAMETER NoAccess
        A string collection of account names that _should be_ denied access
        to the SMB share. The accounts not in this collection will be removed
        from the SMB share.
 
    .NOTES
        The access permission is only removed if the parameter was passed
        into the function.
#>

function Remove-SmbShareAccessPermission
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [Parameter()]
        [System.String[]]
        $FullAccess,

        [Parameter()]
        [System.String[]]
        $ChangeAccess,

        [Parameter()]
        [System.String[]]
        $ReadAccess,

        [Parameter()]
        [System.String[]]
        $NoAccess
    )

    $currentSmbShareAccessPermissions = Get-SmbShareAccess -Name $Name

    <#
        First all access must be removed for accounts that should not
        have permission, or should be unblocked (those that was denied
        access). After that we can add new accounts using the function
        Add-SmbShareAccessPermission.
    #>

    foreach ($smbShareAccess in $currentSmbShareAccessPermissions)
    {
        switch ($smbShareAccess.AccessControlType)
        {
            'Allow'
            {
                $shouldRevokeAccess = $false

                foreach ($accessRight in 'Change','Read','Full')
                {
                    $accessRightVariableName = '{0}Access' -f $accessRight
                    $shouldRevokeAccess = $shouldRevokeAccess `
                        -or (
                            $smbShareAccess.AccessRight -eq $accessRight `
                            -and $PSBoundParameters.ContainsKey($accessRightVariableName) `
                            -and $smbShareAccess.AccountName -notin $PSBoundParameters[$accessRightVariableName]
                        )
                }

                if ($shouldRevokeAccess)
                {
                    Write-Verbose -Message ($script:localizedData.RevokeAccess -f $smbShareAccess.AccountName, $Name)

                    Revoke-SmbShareAccess -Name $Name -AccountName $smbShareAccess.AccountName -Force -ErrorAction 'Stop'
                }
            }

            'Deny'
            {
                if ($smbShareAccess.AccessRight -eq 'Full')
                {
                    if ($PSBoundParameters.ContainsKey('NoAccess') -and $smbShareAccess.AccountName -notin $NoAccess)
                    {
                        Write-Verbose -Message ($script:localizedData.UnblockAccess -f $smbShareAccess.AccountName, $Name)

                        Unblock-SmbShareAccess -Name $Name -AccountName $smbShareAccess.AccountName -Force -ErrorAction 'Stop'
                    }
                }
            }
        }
    }
}

<#
    .SYNOPSIS
        Add the access permission to the SMB share for accounts, in the
        respectively access collections (FullAccess, ChangeAccess,
        ReadAccess, and NoAccess), that do not yet have access.
 
    .PARAMETER Name
        The name of the SMB share to add access permission to.
 
    .PARAMETER FullAccess
        A string collection of account names that should have full access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER ChangeAccess
        A string collection of account names that should have change access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER ReadAccess
        A string collection of account names that should have read access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER NoAccess
        A string collection of account names that should be denied access
        to the SMB share. The accounts in this collection will be added to
        the SMB share.
#>

function Add-SmbShareAccessPermission
{
    param
    (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Name,

        [Parameter()]
        [System.String[]]
        $FullAccess,

        [Parameter()]
        [System.String[]]
        $ChangeAccess,

        [Parameter()]
        [System.String[]]
        $ReadAccess,

        [Parameter()]
        [System.String[]]
        $NoAccess
    )

    $currentSmbShareAccessPermissions = Get-SmbShareAccess -Name $Name

    $accessRights = @{
        ReadAccess = 'Read'
        ChangeAccess = 'Change'
        FullAccess = 'Full'
    }

    foreach ($accessRight in $accessRights.GetEnumerator())
    {
        if ($PSBoundParameters.ContainsKey($accessRight.Key))
        {
            # Get already added account names.
            $smbShareAccessObjects = $currentSmbShareAccessPermissions | Where-Object -FilterScript {
                $_.AccessControlType -eq 'Allow' -and
                $_.AccessRight -eq $accessRight.Value
            }

            # Get a collection of just the account names.
            $accessAccountNames = @($smbShareAccessObjects.AccountName)

            $newAccountsToHaveAccess = $PSBoundParameters[$accessRight.Key] | Where-Object -FilterScript {
                $_ -notin $accessAccountNames
            }

            # Add new accounts that should have permission.
            $newAccountsToHaveAccess | ForEach-Object -Process {
                Write-Verbose -Message ($script:localizedData.GrantAccess -f $accessRight.Value, $_, $Name)

                Grant-SmbShareAccess -Name $Name -AccountName $_ -AccessRight $accessRight.Value -Force -ErrorAction 'Stop'
            }
        }
    }

    if ($PSBoundParameters.ContainsKey('NoAccess'))
    {
        # Get already added account names.
        $smbShareNoAccessObjects = $currentSmbShareAccessPermissions | Where-Object -FilterScript {
            $_.AccessControlType -eq 'Deny' -and
            $_.AccessRight -eq 'Full'
        }

        # Get a collection of just the account names.
        $noAccessAccountNames = @($smbShareNoAccessObjects.AccountName)

        $newAccountsToHaveNoAccess = $NoAccess | Where-Object -FilterScript {
            $_ -notin $noAccessAccountNames
        }

        # Add new accounts that should be denied permission.
        $newAccountsToHaveNoAccess | ForEach-Object -Process {
            Write-Verbose -Message ($script:localizedData.DenyAccess -f $_, $Name)

            Block-SmbShareAccess -Name $Name -AccountName  $_ -Force -ErrorAction 'Stop'
        }
    }
}

<#
    .SYNOPSIS
        Assert that not only empty collections are passed in the
        respectively access permission collections (FullAccess,
        ChangeAccess, ReadAccess, and NoAccess).
 
    .PARAMETER Name
        The name of the SMB share to add access permission to.
 
    .PARAMETER FullAccess
        A string collection of account names that should have full access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER ChangeAccess
        A string collection of account names that should have change access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER ReadAccess
        A string collection of account names that should have read access
        permission. The accounts in this collection will be added to the
        SMB share.
 
    .PARAMETER NoAccess
        A string collection of account names that should be denied access
        to the SMB share. The accounts in this collection will be added to
        the SMB share.
 
    .PARAMETER RemainingParameters
        Container for the rest of the potentially splatted parameters from
        the $PSBoundParameters object.
 
    .NOTES
        The group 'Everyone' is automatically given read access by
        the cmdlet New-SmbShare if all access permission parameters
        (FullAccess, ChangeAccess, ReadAccess, NoAccess) is set to @().
        For that reason we need neither of the parameters, or at least
        one to specify an account.
#>

function Assert-AccessPermissionParameters
{
    param
    (
        [Parameter()]
        [System.String[]]
        $FullAccess,

        [Parameter()]
        [System.String[]]
        $ChangeAccess,

        [Parameter()]
        [System.String[]]
        $ReadAccess,

        [Parameter()]
        [System.String[]]
        $NoAccess,

        [Parameter(ValueFromRemainingArguments)]
        [System.Collections.Generic.List`1[System.Object]]
        $RemainingParameters
    )

    <#
        First check if ReadAccess is monitored (part of the configuration).
        If it is not monitored, then we don't need to worry if Everyone is
        added.
    #>

    if ($PSBoundParameters.ContainsKey('ReadAccess') -and -not $ReadAccess)
    {
        $fullAccessIsEmpty = $PSBoundParameters.ContainsKey('FullAccess') -and -not $FullAccess
        $changeAccessIsEmpty = $PSBoundParameters.ContainsKey('ChangeAccess') -and -not $ChangeAccess
        $noAccessIsEmpty = $PSBoundParameters.ContainsKey('NoAccess') -and -not $NoAccess

        <#
            If ReadAccess should have no members, then we need at least one
            member in one of the other access permission collections.
        #>

        if ($fullAccessIsEmpty -and $changeAccessIsEmpty -and $noAccessIsEmpty)
        {
            New-InvalidArgumentException -Message $script:localizedData.InvalidAccessParametersCombination -ArgumentName 'FullAccess, ChangeAccess, ReadAccess, NoAccess'
        }
    }
}