DSCResources/MSFT_SPWebAppPolicy/MSFT_SPWebAppPolicy.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
function Get-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param
    (
        [parameter(Mandatory = $true)]  
        [System.String] 
        $WebAppUrl,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $Members,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToInclude,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToExclude,

        [parameter(Mandatory = $false)] 
        [System.Boolean] 
        $SetCacheAccountsPolicy,

        [parameter(Mandatory = $false)] 
        [System.Management.Automation.PSCredential] 
        $InstallAccount
    )

    Write-Verbose -Message "Getting web app policy for $WebAppUrl"

    if ($Members -and (($MembersToInclude) -or ($MembersToExclude))) 
    {
        Write-Verbose -Message ("Cannot use the Members parameter together with " + `
                               "the MembersToInclude or MembersToExclude parameters")
        return $null
    }

    if (!$Members -and !$MembersToInclude -and !$MembersToExclude) 
    {
        Write-Verbose -Message ("At least one of the following parameters must be specified: " + `
                               "Members, MembersToInclude, MembersToExclude")
        return $null
    }

    foreach ($member in $Members) 
    {
        if (($member.ActAsSystemAccount -eq $true) `
            -and ($member.PermissionLevel -ne "Full Control")) 
        {
            Write-Verbose -Message ("Members Parameter: You cannot specify ActAsSystemAccount " + `
                                   "with any other permission than Full Control")      
            return $null
        }
    }

    foreach ($member in $MembersToInclude) 
    {
        if (($member.ActAsSystemAccount -eq $true) `
            -and ($member.PermissionLevel -ne "Full Control")) 
        {
            Write-Verbose -Message ("MembersToInclude Parameter: You cannot specify " + `
                                    "ActAsSystemAccount with any other permission than Full " + `
                                    "Control")     
            return $null
        }
    }
    
    $result = Invoke-SPDSCCommand -Credential $InstallAccount `
                                  -Arguments $PSBoundParameters `
                                  -ScriptBlock {
        $params = $args[0]

        $wa = Get-SPWebApplication -Identity $params.WebAppUrl `
                                   -ErrorAction SilentlyContinue

        if ($null -eq $wa) 
        { 
            return $null 
        }

        $SetCacheAccountsPolicy = $false
        if ($params.SetCacheAccountsPolicy) 
        {
            if (($wa.Properties.ContainsKey("portalsuperuseraccount") -eq $true) -and `
                ($wa.Properties.ContainsKey("portalsuperreaderaccount") -eq $true)) 
                {

                $correctPSU = $false
                $correctPSR = $false

                $psu = $wa.Policies[$wa.Properties["portalsuperuseraccount"]]
                if ($null -ne $psu) 
                {
                    if ($psu.PolicyRoleBindings.Type -eq 'FullControl') 
                    { 
                        $correctPSU = $true 
                    }
                }

                $psr = $wa.Policies[$wa.Properties["portalsuperreaderaccount"]]
                if ($null -ne $psr) 
                {
                    if ($psr.PolicyRoleBindings.Type -eq 'FullRead') 
                    { 
                        $correctPSR = $true 
                    }
                }

                if ($correctPSU -eq $true -and $correctPSR -eq $true) 
                {
                    $SetCacheAccountsPolicy = $true
                }
            }
        }
           
        $members = @()
        foreach ($policy in $wa.Policies) 
        {
            $member = @{}
            $memberName = $policy.UserName
            $identityType = "Native"
            if ($memberName -like "i:*|*" -or $memberName -like "c:*|*") 
            {
                $identityType = "Claims"
                $convertedClaim = New-SPClaimsPrincipal -Identity $memberName `
                                                        -IdentityType EncodedClaim `
                                                        -ErrorAction SilentlyContinue
                if ($null -ne $convertedClaim) 
                {
                    $memberName = $convertedClaim.Value
                }
            }

            if ($memberName -match "^s-1-[0-59]-\d+-\d+-\d+-\d+-\d+")
            {
                $memberName = Resolve-SPDscSecurityIdentifier -SID $memberName
            }

            switch ($policy.PolicyRoleBindings.Type)
            {
                'DenyAll'
                { 
                    $memberPermissionlevel = 'Deny All'
                }
                'DenyWrite'
                {
                    $memberPermissionlevel = 'Deny Write'
                }
                'FullControl'
                {
                    $memberPermissionlevel = 'Full Control'
                }
                'FullRead'
                {
                    $memberPermissionlevel = 'Full Read'
                }
            }

            $member.Username = $memberName
            $member.PermissionLevel = $memberPermissionlevel
            $member.ActAsSystemAccount = $policy.IsSystemUser
            $member.IdentityType = $identityType
            $members += $member
        }

        $returnval = @{
                WebAppUrl = $params.WebAppUrl
                Members = $members
                MembersToInclude = $params.MembersToInclude
                MembersToExclude = $params.MembersToExclude
                SetCacheAccountsPolicy = $SetCacheAccountsPolicy
                InstallAccount = $params.InstallAccount
        }
        
        return $returnval
    }
    return $result
}

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

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $Members,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToInclude,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToExclude,

        [parameter(Mandatory = $false)] 
        [System.Boolean] 
        $SetCacheAccountsPolicy,

        [parameter(Mandatory = $false)] 
        [System.Management.Automation.PSCredential] 
        $InstallAccount
    )

    Write-Verbose -Message "Setting web app policy for $WebAppUrl"

    if ($Members -and (($MembersToInclude) -or ($MembersToExclude))) 
    {
        throw ("Cannot use the Members parameter together with the " + `
              "MembersToInclude or MembersToExclude parameters")
    }

    if (!$Members -and !$MembersToInclude -and !$MembersToExclude) 
    {
        throw ("At least one of the following parameters must be specified: " + `
              "Members, MembersToInclude, MembersToExclude")
    }

    foreach ($member in $Members) 
    {
        if (($member.ActAsSystemAccount -eq $true) -and `
            ($member.PermissionLevel -ne "Full Control")) 
        {
            throw ("Members Parameter: You cannot specify ActAsSystemAccount " + `
                  "with any other permission than Full Control")        
        }
    }

    foreach ($member in $MembersToInclude) 
    {
        if (($member.ActAsSystemAccount -eq $true) -and `
            ($member.PermissionLevel -ne "Full Control")) 
        {
            throw ("MembersToInclude Parameter: You cannot specify ActAsSystemAccount " + `
                  "with any other permission than Full Control")        
        }
    }

    $CurrentValues = Get-TargetResource @PSBoundParameters


    $modulePath = "..\..\Modules\SharePointDsc.WebAppPolicy\SPWebAppPolicy.psm1"
    Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath $modulePath -Resolve)
    
    if ($null -eq $CurrentValues) 
    {
        throw "Web application does not exist"
    }

    $cacheAccounts = Get-SPDSCCacheAccountConfiguration -InputParameters $WebAppUrl
    
    if ($SetCacheAccountsPolicy) 
    {
        if ($cacheAccounts.SuperUserAccount -eq "" -or $cacheAccounts.SuperReaderAccount -eq "") 
        {
            throw ("Cache accounts not configured properly. PortalSuperUserAccount or " + `
                  "PortalSuperReaderAccount property is not configured.")
        }
    }

    # Determine the default identity type to use for entries that do not have it specified
    $defaultIdentityType = Invoke-SPDSCCommand -Credential $InstallAccount `
                                               -Arguments $PSBoundParameters `
                                               -ScriptBlock {
        $params = $args[0]

        $wa = Get-SPWebApplication -Identity $params.WebAppUrl
        if ($wa.UseClaimsAuthentication -eq $true) 
        {
            return "Claims"
        } 
        else 
        {
            return "Native"
        }
    }

    $changeUsers = @()

    if ($Members -or $MembersToInclude) 
    {
        $allMembers = @()
        if ($Members)
        {
            Write-Verbose -Message "Members property is set - setting full membership list"
            $membersToCheck = $Members
        }
        if ($MembersToInclude)
        {
            Write-Verbose -Message ("MembersToInclude property is set - setting membership " + `
                                    "list to ensure specified members are included")
            $membersToCheck = $MembersToInclude
        }
        foreach ($member in $membersToCheck) 
        {
            $allMembers += $member
        }

        # Determine if cache accounts are to be included users
        if ($SetCacheAccountsPolicy) 
        {
            Write-Verbose -Message "SetCacheAccountsPolicy is True - Adding Cache Accounts to list"
            $psuAccount = @{
                UserName = $cacheAccounts.SuperUserAccount
                PermissionLevel = "Full Control"
                IdentityMode = $cacheAccounts.IdentityMode
            }
            $allMembers += $psuAccount
            
            $psrAccount = @{
                UserName = $cacheAccounts.SuperReaderAccount
                PermissionLevel = "Full Read"
                IdentityMode = $cacheAccounts.IdentityMode
            }
            $allMembers += $psrAccount
        }

        # Get the list of differences from the current configuration
        $differences = Compare-SPDSCWebAppPolicy -WAPolicies $CurrentValues.Members `
                                                 -DSCSettings $allMembers `
                                                 -DefaultIdentityType $defaultIdentityType

        foreach ($difference in $differences) 
        {
            switch ($difference.Status) 
            {
                Additional {
                    # Only remove users if the "Members" property was set
                    # instead of "MembersToInclude"
                    if ($Members) 
                    {
                        $user = @{
                            Type     = "Delete"
                            Username = $difference.Username
                            IdentityMode = $difference.IdentityType
                        }
                    }
                }
                Different {
                    $user = @{
                        Type     = "Change"
                        Username = $difference.Username
                        PermissionLevel    = $difference.DesiredPermissionLevel
                        ActAsSystemAccount = $difference.DesiredActAsSystemSetting
                        IdentityMode = $difference.IdentityType
                    }
                }
                Missing  {
                    $user = @{
                        Type     = "Add"
                        Username = $difference.Username
                        PermissionLevel    = $difference.DesiredPermissionLevel
                        ActAsSystemAccount = $difference.DesiredActAsSystemSetting
                        IdentityMode = $difference.IdentityType 
                    }
                }
            }
            $changeUsers += $user
        }
    }

    if ($MembersToExclude) 
    {
        Write-Verbose -Message ("MembersToExclude property is set - setting membership list " + `
                                "to ensure specified members are not included")

        foreach ($member in $MembersToExclude) 
        {
            $policy = $CurrentValues.Members | Where-Object -FilterScript { 
                $_.UserName -eq $member.UserName -and $_.IdentityType -eq $identityType
            }

            if (($cacheAccounts.SuperUserAccount -eq $member.Username) -or `
                ($cacheAccounts.SuperReaderAccount -eq $member.Username)) 
            {
                throw "You cannot exclude the Cache accounts from the Web Application Policy"
            }

            if ($null -ne $policy) 
            {
                $user = @{
                    Type     = "Delete"
                    Username = $member.UserName
                }
            }
            $changeUsers += $user
        }
    }
    
    ## Perform changes
    Invoke-SPDSCCommand -Credential $InstallAccount `
                        -Arguments @($PSBoundParameters,$PSScriptRoot,$changeUsers) `
                        -ScriptBlock {
        $params      = $args[0]
        $scriptRoot  = $args[1]
        $changeUsers = $args[2]

        $modulePath = "..\..\Modules\SharePointDsc.WebAppPolicy\SPWebAppPolicy.psm1"
        Import-Module -Name (Join-Path -Path $scriptRoot -ChildPath $modulePath -Resolve)

        $wa = Get-SPWebApplication -Identity $params.WebAppUrl -ErrorAction SilentlyContinue

        if ($null -eq $wa) 
        {
            throw "Specified web application could not be found."
        }

        $denyAll     = $wa.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyAll)
        $denyWrite   = $wa.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyWrite)
        $fullControl = $wa.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)
        $fullRead    = $wa.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead)

        Write-Verbose -Message "Processing changes"

        foreach ($user in $changeUsers) 
        {
            switch ($user.Type) 
            {
                "Add" {
                    # User does not exist. Add user
                    Write-Verbose -Message "Adding $($user.Username)"
                    
                    $userToAdd = $user.Username
                    if ($user.IdentityMode -eq "Claims") 
                    {
                        $isUser = Test-SPDSCIsADUser -IdentityName $user.Username
                        if ($isUser -eq $true) 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSamAccountName
                            $userToAdd = $principal.ToEncodedString()
                        } 
                        else 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSecurityGroupName
                            $userToAdd = $principal.ToEncodedString()
                        }    
                    }
                    $newPolicy = $wa.Policies.Add($userToAdd, $user.UserName)
                    foreach ($permissionLevel in $user.PermissionLevel) 
                    {
                        switch ($permissionLevel) 
                        {
                            "Deny All" {
                                $newPolicy.PolicyRoleBindings.Add($denyAll)
                            }
                            "Deny Write" {
                                $newPolicy.PolicyRoleBindings.Add($denyWrite)
                            }
                            "Full Control" {
                                $newPolicy.PolicyRoleBindings.Add($fullControl)
                            }
                            "Full Read" {
                                $newPolicy.PolicyRoleBindings.Add($fullRead)
                            }
                        }
                    }
                    if ($user.ActAsSystemAccount) 
                    {
                        $newPolicy.IsSystemUser = $user.ActAsSystemAccount
                    }
                }
                "Change" {
                    # User exists. Check permissions
                    $userToChange = $user.Username
                    if ($user.IdentityMode -eq "Claims") 
                    {
                        $isUser = Test-SPDSCIsADUser -IdentityName $user.Username
                        if ($isUser -eq $true) 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSamAccountName
                            $userToChange = $principal.ToEncodedString()
                        } 
                        else 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSecurityGroupName
                            $userToChange = $principal.ToEncodedString()
                        }
                    }
                    $policy = $wa.Policies | Where-Object -FilterScript { 
                        $_.UserName -eq $userToChange 
                    }

                    Write-Verbose -Message "User $($user.Username) exists, checking permissions"
                    if ($user.ActAsSystemAccount -ne $policy.IsSystemUser) 
                    { 
                        $policy.IsSystemUser = $user.ActAsSystemAccount 
                    }

                    switch ($policy.PolicyRoleBindings.Type)
                    {
                        'DenyAll'
                        { 
                            $userPermissionlevel = 'Deny All'
                        }
                        'DenyWrite'
                        {
                            $userPermissionlevel = 'Deny Write'
                        }
                        'FullControl'
                        {
                            $userPermissionlevel = 'Full Control'
                        }
                        'FullRead'
                        {
                            $userPermissionlevel = 'Full Read'
                        }
                    }

                    $polbinddiff = Compare-Object -ReferenceObject $userPermissionlevel `
                                                  -DifferenceObject $user.PermissionLevel
                    if ($null -ne $polbinddiff) 
                    {
                        $policy.PolicyRoleBindings.RemoveAll()
                        foreach ($permissionLevel in $user.PermissionLevel) 
                        {
                            switch ($permissionLevel) 
                            {
                                "Deny All" {
                                    $policy.PolicyRoleBindings.Add($denyAll)
                                }
                                "Deny Write" {
                                    $policy.PolicyRoleBindings.Add($denyWrite)
                                }
                                "Full Control" {
                                    $policy.PolicyRoleBindings.Add($fullControl)
                                }
                                "Full Read" {
                                    $policy.PolicyRoleBindings.Add($fullRead)
                                }
                            }
                        }
                    }
                }
                "Delete" 
                {
                    Write-Verbose -Message "Removing $($user.Username)"
                    $userToDrop = $user.Username
                    if ($user.IdentityMode -eq "Claims")  
                    {
                        $isUser = Test-SPDSCIsADUser -IdentityName $user.Username
                        if ($isUser -eq $true) 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSamAccountName
                            $userToDrop = $principal.ToEncodedString()
                        } 
                        else 
                        {
                            $principal = New-SPClaimsPrincipal -Identity $user.Username `
                                                               -IdentityType WindowsSecurityGroupName
                            $userToDrop = $principal.ToEncodedString()
                        }    
                    }
                    Remove-SPDSCGenericObject -SourceCollection $wa.Policies `
                                              -Target $userToDrop `
                                              -ErrorAction SilentlyContinue
                }
            }
        }
        $wa.Update()
    }
}

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

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $Members,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToInclude,

        [parameter(Mandatory = $false)] 
        [Microsoft.Management.Infrastructure.CimInstance[]] 
        $MembersToExclude,

        [parameter(Mandatory = $false)] 
        [System.Boolean] 
        $SetCacheAccountsPolicy,

        [parameter(Mandatory = $false)] 
        [System.Management.Automation.PSCredential] 
        $InstallAccount
    )

    Write-Verbose -Message "Testing web app policy for $WebAppUrl"

    $CurrentValues = Get-TargetResource @PSBoundParameters
    
    $modulePath = "..\..\Modules\SharePointDsc.WebAppPolicy\SPWebAppPolicy.psm1"
    Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath $modulePath -Resolve)

    if ($null -eq $CurrentValues) 
    { 
        return $false 
    }

    $cacheAccounts = Get-SPDSCCacheAccountConfiguration -InputParameters $WebAppUrl
    if ($SetCacheAccountsPolicy) 
    {
        if (($cacheAccounts.SuperUserAccount -eq "") -or `
            ($cacheAccounts.SuperReaderAccount -eq "")) 
        {
            throw "Cache accounts not configured properly. PortalSuperUserAccount or " + `
                  "PortalSuperReaderAccount property is not configured."
        }
    }

    # Determine the default identity type to use for entries that do not have it specified
    $defaultIdentityType = Invoke-SPDSCCommand -Credential $InstallAccount `
                                               -Arguments $PSBoundParameters `
                                               -ScriptBlock {
        $params = $args[0]

        $wa = Get-SPWebApplication -Identity $params.WebAppUrl
        if ($wa.UseClaimsAuthentication -eq $true) 
        {
            return "Claims"
        } 
        else 
        {
            return "Native"
        }
    }
    
    # If checking the full members list, or the list of members to include then build the
    # appropriate members list and check for the output of Compare-SPDSCWebAppPolicy
    if ($Members -or $MembersToInclude) 
    {
        $allMembers = @()
        if ($Members)
        {
            Write-Verbose -Message "Members property is set - testing full membership list"
            $membersToCheck = $Members
        }
        if ($MembersToInclude)
        {
            Write-Verbose -Message ("MembersToInclude property is set - testing membership " + `
                                    "list to ensure specified members are included")
            $membersToCheck = $MembersToInclude
        }
        foreach ($member in $membersToCheck) 
        {
            $allMembers += $member
        }

        # Determine if cache accounts are to be included users
        if ($SetCacheAccountsPolicy) 
        {
            Write-Verbose -Message "SetCacheAccountsPolicy is True - Adding Cache Accounts to list"
            $psuAccount = @{
                UserName = $cacheAccounts.SuperUserAccount
                PermissionLevel = "Full Control"
                IdentityMode = $cacheAccounts.IdentityMode
            }
            $allMembers += $psuAccount
            
            $psrAccount = @{
                UserName = $cacheAccounts.SuperReaderAccount
                PermissionLevel = "Full Read"
                IdentityMode = $cacheAccounts.IdentityMode
            }
            $allMembers += $psrAccount
        }

        # Get the list of differences from the current configuration
        $differences = Compare-SPDSCWebAppPolicy -WAPolicies $CurrentValues.Members `
                                                 -DSCSettings $allMembers `
                                                 -DefaultIdentityType $defaultIdentityType
        
        # If checking members, any difference counts as a fail
        if ($Members)
        {
            if ($differences.Count -eq 0) 
            { 
                return $true 
            } 
            else 
            {
                Write-Verbose -Message "Differences in the policy were found, returning false" 
                return $false 
            }
        }

        # If only checking members to include only differences or missing records count as a fail
        if ($MembersToInclude)
        {
            if (($differences | Where-Object -FilterScript { 
                    $_.Status -eq "Different" -or $_.Status -eq "Missing" 
                }).Count -eq 0) 
            { 
                return $true 
            } 
            else 
            {
                Write-Verbose -Message "Different or Missing policy was found, returning false" 
                return $false 
            }
        }
    }

    # If checking members to exlclude, simply compare the list of user names to the current
    # membership list
    if ($MembersToExclude) 
    {
        Write-Verbose -Message ("MembersToExclude property is set - checking for permissions " + `
                                "that need to be removed")
        foreach ($member in $MembersToExclude) 
        {
            if (($cacheAccounts.SuperUserAccount -eq $member.Username) -or `
                ($cacheAccounts.SuperReaderAccount -eq $member.Username)) 
            {
                throw "You cannot exclude the Cache accounts from the Web Application Policy"
            }
            
            foreach ($policy in $CurrentValues.Members) 
            {
                if ($policy.Username -eq $member.Username) 
                {
                    return $false
                }
            }
        }
        return $true
    }
}

function Get-SPDSCCacheAccountConfiguration() 
{
    [CmdletBinding()]
    [OutputType([System.Collections.Hashtable])]
    param (
        [parameter(Mandatory = $false)]
        [Object[]]  
        $InputParameters
    )
    
    $cacheAccounts = Invoke-SPDSCCommand -Credential $InstallAccount `
                                         -Arguments $InputParameters `
                                         -ScriptBlock {
        Write-Verbose -Message "Retrieving CacheAccounts"
        $params = $args[0]

        $wa = Get-SPWebApplication -Identity $params -ErrorAction SilentlyContinue

        if ($null -eq $wa) 
        {
            throw "Specified web application could not be found."
        }

        $returnval = @{
            SuperUserAccount = ""               
            SuperReaderAccount = ""
        }

        if ($wa.Properties.ContainsKey("portalsuperuseraccount")) 
        {
            $memberName = $wa.Properties["portalsuperuseraccount"]
            if ($wa.UseClaimsAuthentication -eq $true) 
            {
                $convertedClaim = New-SPClaimsPrincipal -Identity $memberName `
                                                        -IdentityType EncodedClaim `
                                                        -ErrorAction SilentlyContinue
                if($null -ne $convertedClaim) 
                {
                    $memberName = $convertedClaim.Value
                }
            }
            $returnval.SuperUserAccount = $memberName
        }
        if ($wa.Properties.ContainsKey("portalsuperreaderaccount")) 
        {
            $memberName = $wa.Properties["portalsuperreaderaccount"]
            if ($wa.UseClaimsAuthentication -eq $true) 
            {
                $convertedClaim = New-SPClaimsPrincipal -Identity $memberName `
                                                        -IdentityType EncodedClaim `
                                                        -ErrorAction SilentlyContinue
                if($null -ne $convertedClaim) 
                {
                    $memberName = $convertedClaim.Value
                }
            }
            $returnval.SuperReaderAccount = $memberName
        }

        if ($wa.UseClaimsAuthentication -eq $true) 
        {
            $returnval.IdentityMode = "Claims"
        } 
        else 
        {
            $returnval.IdentityMode = "Native"
        }
        
        return $returnval
    }

    return $cacheAccounts
}

Export-ModuleMember -Function *-TargetResource