Private/ConvertTo-SortedPermission.ps1

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
function ConvertTo-SortedPermission {
    <#
    .SYNOPSIS
    Correctly sorts PAS Safe Permissions

    .DESCRIPTION
    Safe permissions should be sent to the API in a specific order.
    When given a hashtable of parameters as input, safe permission key
    value pairs are returned in the correct order and format for
    inclusion in request body.

    .PARAMETER Parameters
    The input parameters to filter for safe permissons

    .EXAMPLE
    $PSBoundParameters | ConvertTo-SortedPermission

    Returns key value pairs fro $PSBoundParameters which relate to safe permissions

    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'Permissions', Justification = "False Positive")]
    [CmdletBinding()]
    param (
        [parameter(
            Mandatory = $false,
            ValueFromPipeline = $true
        )]
        [hashtable]$Parameters
    )

    begin {

        $OrderedPermissions = [ordered]@{
            UseAccounts                            = $false
            RetrieveAccounts                       = $false
            ListAccounts                           = $false
            AddAccounts                            = $false
            UpdateAccountContent                   = $false
            UpdateAccountProperties                = $false
            InitiateCPMAccountManagementOperations = $false
            SpecifyNextAccountContent              = $false
            RenameAccounts                         = $false
            DeleteAccounts                         = $false
            UnlockAccounts                         = $false
            ManageSafe                             = $false
            ManageSafeMembers                      = $false
            BackupSafe                             = $false
            ViewAuditLog                           = $false
            ViewSafeMembers                        = $false
            RequestsAuthorizationLevel             = 0
            AccessWithoutConfirmation              = $false
            CreateFolders                          = $false
            DeleteFolders                          = $false
            MoveAccountsAndFolders                 = $false
        }

    }

    process {

        #Input parameters have been provided
        If ($null -ne $Parameters.Keys) {

            #For each Ordered Safe Member Permission
            $OrderedPermissions.keys | ForEach-Object {

                $Permissions = [ordered]@{ }

            } {

                #Parameter match
                If ($Parameters.ContainsKey($PSItem)) {

                    #Add to hash table in key/value pair
                    $Permissions.Add($PSItem, $Parameters[$PSItem])

                }

            } {

                #return permissions, in order
                $Permissions.getenumerator() | ForEach-Object { $PSItem }

            }
        }

    }

    end {

    }

}