Private/ConvertFrom-IdoItResultObject.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
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
Function ConvertFrom-IdoItResultObject {
    <#
   .SYNOPSIS
   This helper function does some formatting and flattening to the original idoit result-

   .DESCRIPTION
   This helper function does some formatting and flattening to the original idoit result.

   It will Pascal-Case the property names and also removes '_' from the names.
   Idoit will also return relations within the answers. We flatten these results a little bit to make it more easy to use results without
   to much knowledge of the idoit relation model.

   .PARAMETER InputObject
   Takes one or more PSObjects and makes property names nicely and flattens the result if parameter NoFlatten is not provided

   .PARAMETER NoFlatten
   This switch will not change the original object structure of the object.

   .EXAMPLE
   PS> ConvertFrom-IdoItResultObject -InputObject (Get-IdoItCategory -Id 3411 -Category C__CATG__IP)

   This will the cache for idoit constants

   .NOTES
   Version
   0.1.0 20.01.2018 CB initial release
   #>

    [CmdletBinding ()]
    Param (
        [Parameter (
            Mandatory = $True,
            Position = 0,
            ValueFromPipeline = $True,
            ValueFromPipelineByPropertyName = $True
        )]
        [PSObject[]]$InputObject,

        [Switch]$NoFlattening
    )

    Process {

        $InputObjectProperties = $_.PSObject.Properties
        $HelperObject = [PSCustomObject]@{}


        Foreach ($Property in $InputObjectProperties)
        {

            #Idoi
            $PropertyName = ''
            $NameParts = $Property.Name.Split('_')
            ForEach ($Part in $NameParts)
            {

                $PropertyName += $Part.Substring(0,1).ToUpper() + $Part.Substring(1).ToLower()

            }



            If ($Property.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject')
            {

                If ( -not $NoFlattening)
                {

                    If ($Property.Value.PSObject.Properties.Name -contains 'ref_title')
                    {

                        $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $Property.Value.ref_title

                    }
                    ElseIf ($Property.Value.PSObject.Properties.Name -contains 'title')
                    {

                        $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $Property.Value.title

                    }
                    Else
                    {

                        $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $Property.Value

                    }

                    If ($Property.Value.PSObject.Properties.Name -contains 'ref_id')
                    {

                        $HelperObject | Add-Member -MemberType $Property.MemberType -Name ($PropertyName+'Id') -Value $Property.Value.ref_id

                    }
                    ElseIf ($Property.Value.PSObject.Properties.Name -contains 'id')
                    {

                        $HelperObject | Add-Member -MemberType $Property.MemberType -Name ($PropertyName+'Id') -Value $Property.Value.ref_id

                    }

                }
                Else
                {
                    $tempObject = [PSCustomObject]@{}
                    ForEach ($P in $Property.Value.PSObject.Properties) {
                        $PropertyNameTemp = ''
                        $NameParts = $P.Name.Split('_')
                        ForEach ($Part in $NameParts)
                        {

                            $PropertyNameTemp += $Part.Substring(0,1).ToUpper() + $Part.Substring(1).ToLower()

                        }
                        $tempObject | Add-Member -MemberType $P.MemberType -Name $PropertyNameTemp -Value $P.Value
                    }
                    $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $tempObject

                }

            }
            ElseIf ($Property.TypeNameOfValue -eq 'System.Object[]')
            {

                $tempArray = @()
                If ( -not $NoFlattening)
                {

                    ForEach ($Value in $Property.Value)
                    {


                        If ( ($Value.GetType()).Name -eq 'PSCustomObject')
                        {

                            If ($Value.PSObject.Properties.Name -contains 'title')
                            {

                                $tempArray += $Value.Title

                            }

                        }

                    }

                    $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $tempArray

                }
                Else
                {
                    $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $Property.Value
                }
            }

            Else {

                $HelperObject | Add-Member -MemberType $Property.MemberType -Name $PropertyName -Value $Property.Value
            }




        }

        Return $HelperObject
    }

}