Module/Common/Functions.Helper.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# Header

<#
    .SYNOPSIS
        Converts OrgSetting Xml object to a hashtable
 
    .DESCRIPTION
        Converts OrgSetting Xml object to a hashtable. The hashtable can be used to merge
        settings from the default xml and user specified settings passed from the configuration
 
    .PARAMETER XmlOrgSetting
        Xml object OrgSettings from the org.default.xml file
 
    .EXAMPLE
        PS C:\> [xml]$xmlOrgSettings = (Get-Content -Path ($this.RuleFile -replace '.xml', '.org.default.xml'))
        PS C:\> $settings = ConvertTo-OrgSettingHashtable -XmlOrgSetting $xmlOrgSettings
 
        This example will set the xmlOrgSettings variable as an xml object, representing the contents of
        the technology specific org.default.xml file. Pass the xmlOrgSettings object to the XmlOrgSetting parameter
        in order to convert the xml object to a hashtable.
#>


function ConvertTo-OrgSettingHashtable
{
    [CmdletBinding()]
    [OutputType([hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [xml]
        $XmlOrgSetting
    )

    $result = @{}
    foreach ($ruleId in $XmlOrgSetting.OrganizationalSettings.OrganizationalSetting)
    {
        $ruleIdValues = @{}
        $ruleIdProperty = $ruleId.Attributes.Name | Where-Object -FilterScript {$PSItem -ne 'id'}
        foreach ($property in $ruleIdProperty)
        {
            $ruleIdValues.Add($property, ($ruleId.GetAttribute($property)))
        }
        $result.Add($ruleId.id, $ruleIdValues)
    }

    return $result
}

<#
    .SYNOPSIS
        Merges Default OrgSettings with user specified OrgSettings at configuration run time.
 
    .DESCRIPTION
        Merges Default OrgSettings with user specified OrgSettings at configuration run time.
 
    .PARAMETER DefaultOrgSetting
        Hashtable representing the contents of the org.default.xml file.
 
    .PARAMETER UserSpecifiedOrgSetting
        Hashtable representing data passed from the configuration by the user.
 
    .EXAMPLE
        PS C:\> [xml]$xmlOrgSettings = (Get-Content -Path ($this.RuleFile -replace '.xml', '.org.default.xml'))
        PS C:\> $settings = ConvertTo-OrgSettingHashtable -XmlOrgSetting $xmlOrgSettings
        PS C:\> $orgSettings = Merge-OrgSettingValue -DefaultOrgSetting $settings -UserSpecifiedOrgSetting $OrgSettings
 
        This example will take an xml OrgSettings file, convert it to a hashtable, then merge the contents to user
        specified OrgSettings passed during configuration.
#>


function Merge-OrgSettingValue
{
    [CmdletBinding()]
    [OutputType([hashtable])]
    param
    (
        [Parameter(Mandatory = $true)]
        [hashtable]
        $DefaultOrgSetting,

        [Parameter(Mandatory = $true)]
        [hashtable]
        $UserSpecifiedOrgSetting
    )

    foreach ($ruleId in $UserSpecifiedOrgSetting.Keys)
    {
        $DefaultOrgSetting[$ruleId] = $UserSpecifiedOrgSetting[$ruleId]
    }

    return $DefaultOrgSetting
}