Serialization/Groups/Deserialize-GPPSectionGroups.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
function Deserialize-GPPSectionGroups {
    Param (
        [Parameter(Mandatory)]
        [System.Xml.XmlElement]$InputObject
    )

    $GroupsMembers = [System.Collections.Generic.List[GPPItemGroupsSection]]::new()

    foreach ($ChildNode in $InputObject.ChildNodes) {
        $GPPItemPropertiesElement = $ChildNode.Properties
        $GPPItemPropertiesElementPropertyDefinitions = (Get-Member -InputObject $GPPItemPropertiesElement | Where-Object { $_.MemberType -eq [System.Management.Automation.PSMemberTypes]::Property }).Name
        foreach ($PropertyDefinition in $GPPItemPropertiesElementPropertyDefinitions) {
            if ($GPPItemPropertiesElement.$PropertyDefinition -eq '') {
                $GPPItemPropertiesElement.RemoveAttribute($PropertyDefinition)
            }
        }

        $Disabled = if ($ChildNode.disabled -eq 1) {
            $true
        }
        else {
            $false
        }

        switch ($ChildNode.LocalName) {
            'Group' {
                $GPPItemGroupMembersElement = $GPPItemPropertiesElement.Members.Member

                $Members = $null
                if ($GPPItemGroupMembersElement) {
                    $Members = [System.Collections.Generic.List[GPPItemGroupMember]]::new()
                    foreach ($Item in $GPPItemGroupMembersElement) {
                        if ($Item.sid) {
                            $Members.Add([GPPItemGroupMember]::new($Item.action, $Item.name, $Item.sid))
                        }
                        else {
                            $Members.Add([GPPItemGroupMember]::new($Item.action, $Item.name))
                        }
                    }
                }

                $DeleteAllUsers = if ($GPPItemPropertiesElement.deleteAllUsers -eq 1) {
                    $true
                }
                else {
                    $false
                }
                $DeleteAllGroups = if ($GPPItemPropertiesElement.deleteAllGroups -eq 1) {
                    $true
                }
                else {
                    $false
                }

                $GPPItemPropertiesGroup = [GPPItemPropertiesGroup]::new($GPPItemPropertiesElement.action, $GPPItemPropertiesElement.groupName, $GPPItemPropertiesElement.groupSid, $GPPItemPropertiesElement.newName, $GPPItemPropertiesElement.description, $Members, $DeleteAllUsers, $DeleteAllGroups)

                $GroupsMembers.Add([GPPItemGroup]::new($GPPItemPropertiesGroup, [guid]$ChildNode.uid, $Disabled, $ChildNode.name))
            }
            'User' {
                $UserMustChangePassword = if ($GPPItemPropertiesElement.changeLogon -eq 1) {
                    $true
                }
                else {
                    $false
                }
                $UserMayNotChangePassword = if ($GPPItemPropertiesElement.noChange -eq 1) {
                    $true
                }
                else {
                    $false
                }
                $PasswordNeverExpires = if ($GPPItemPropertiesElement.neverExpires -eq 1) {
                    $true
                }
                else {
                    $false
                }
                $AccountDisabled = if ($GPPItemPropertiesElement.acctDisabled -eq 1) {
                    $true
                }
                else {
                    $false
                }

                if ($GPPItemPropertiesElement.subAuthority) {
                    [GPPItemUserSubAuthority]$BuiltInUser = $GPPItemPropertiesElement.subAuthority
                }
                else {
                    if ($null -ne $BuiltInUser) {
                        # When in the XML-file, we have a built-in user item and then a regular user item, the $BuiltInUser variable will still have its value when processing the regular user.
                        # This confuses the constructor. And we cannot use $BuiltInUser = $null, because the type of this variable does not allow that.
                        Remove-Variable -Name 'BuiltInUser'
                    }
                }

                $GPPItemPropertiesUser = if ($UserMustChangePassword) {
                    [GPPItemPropertiesUser]::new($GPPItemPropertiesElement.action, $BuiltInUser, $GPPItemPropertiesElement.userName, $GPPItemPropertiesElement.newName, $GPPItemPropertiesElement.fullName, $GPPItemPropertiesElement.description, $UserMustChangePassword, $AccountDisabled, $GPPItemPropertiesElement.expires)
                }
                else {
                    [GPPItemPropertiesUser]::new($GPPItemPropertiesElement.action, $BuiltInUser, $GPPItemPropertiesElement.userName, $GPPItemPropertiesElement.newName, $GPPItemPropertiesElement.fullName, $GPPItemPropertiesElement.description, $UserMayNotChangePassword, $PasswordNeverExpires, $AccountDisabled, $GPPItemPropertiesElement.expires)
                }

                $GroupsMembers.Add([GPPItemUser]::new($GPPItemPropertiesUser, [guid]$ChildNode.uid, $Disabled, $ChildNode.name))
            }
        }
    }

    # The following is as it is because:
    # > $InputObject.disabled.gettype().name
    # String
    # And $true / $false does not play well with string content.
    # But implicit type convertion works well with string -> int
    $SectionDisabled = if ($InputObject.disabled -eq 1) {
        $true
    }
    else {
        $false
    }
    [GPPSectionGroups]::new($GroupsMembers, $SectionDisabled)
}