Private/Format-PASUserObject.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
94
95
96
97
98
99
100
101
102
103
Function Format-PASUserObject {
    <#
    .SYNOPSIS
    Creates object in the expected format for adding or updating PAS Users

    .DESCRIPTION
    From a hashtable provided as input, nests key/value pairs under expected key.
    Returns object structured as required to be converted to json and used as payload to create or update PAS user.
    Designed to be consumed by New-PASUser & Set-PASUser.

    .PARAMETER UserProperties
    A hashtable containing the key/values to create or update a PAS User

    .EXAMPLE
    $ParameterValues | Format-PASUserObject
    #>

    [CmdletBinding()]
    param(
        [parameter(
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [hashtable]$UserProperties
    )

    Begin {
        $businessAddressParams = [Collections.Generic.List[String]]@("workStreet", "workCity", "workState", "workZip", "workCountry")
        $internetParams = [Collections.Generic.List[String]]@("homePage", "homeEmail", "businessEmail", "otherEmail")
        $phonesParams = [Collections.Generic.List[String]]@("homeNumber", "businessNumber", "cellularNumber", "faxNumber", "pagerNumber")
        $personalDetailsParams = [Collections.Generic.List[String]]@("street", "city", "state", "zip", "country", "title", "organization",
            "department", "profession", "FirstName", "middleName", "LastName")
    }

    Process {

        #Clone the input hashtable
        $UserObject = $UserProperties.clone()

        #Process each key of the input hashtable
        switch ($UserProperties.keys) {

            "ExpiryDate" {
                #Include date string in required format
                $UserObject["ExpiryDate"] = $UserProperties["ExpiryDate"] | ConvertTo-UnixTime

            }

            { $businessAddressParams -contains $PSItem } {

                #Create businessAddress key if it does not exist
                if (-not($UserObject.ContainsKey("businessAddress"))) {
                    $UserObject.Add("businessAddress", @{})
                }
                #Add as Key/Value under businessAddress
                $UserObject["businessAddress"].Add($PSItem, $UserObject[$PSItem])

            }

            { $internetParams -contains $PSItem } {

                #Create internet key if it does not exist
                if (-not($UserObject.ContainsKey("internet"))) {
                    $UserObject.Add("internet", @{})
                }
                #Add as Key/Value under internet
                $UserObject["internet"].Add($PSItem, $UserObject[$PSItem])

            }

            { $phonesParams -contains $PSItem } {

                #Create phones key if it does not exist
                if (-not($UserObject.ContainsKey("phones"))) {
                    $UserObject.Add("phones", @{})
                }
                #Add as Key/Value under phones
                $UserObject["phones"].Add($PSItem, $UserObject[$PSItem])

            }

            { $personalDetailsParams -contains $PSItem } {

                #Create personalDetails key if it does not exist
                if (-not($UserObject.ContainsKey("personalDetails"))) {
                    $UserObject.Add("personalDetails", @{})
                }
                #Add as Key/Value under personalDetails
                $UserObject["personalDetails"].Add($PSItem, $UserObject[$PSItem])

            }

        }

    }

    End {

        #Return object with expected structure & format
        $UserObject | Get-PASParameter -ParametersToRemove @($businessAddressParams + $internetParams + $phonesParams + $personalDetailsParams)

    }

}