Public/Get-IdoItCategory.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Function Get-IdoItCategory {
    <#
        .SYNOPSIS
        Get category information from the i-doit cmdb for a specific object id.

        .DESCRIPTION
        Get detailed information for a category for a given object. The resulting information depends mainly on the category you
        are requesting.

        DYNAMIC PARAMETERS
        -Category <String>
            This parameter takes the constant of a global or specific category you want to get information for this object. This is a dynamic parameter
            that will pull the available values in from the users constant cache.

            To get an overview of the available constants you can call
            PS> Get-IdoItConstant | Where-Object {(($_.Type -eq "Global") -or ($_.Type -eq "Specific"))}

        .PARAMETER Id
        This is the id of the object you want to query a category for. This parameter takes an integer and it should be greater than 0.

        You can also provide a value by pipline.


        .PARAMETER CatgId
        Instead of providing a constant name for a category you can also pass an category id
        There are two types of categories available:
        - Global categories
        - Specific categories

        CatgId is for global category ids


        .PARAMETER CatsId
        Instead of providing a constant name for a category you can also pass an category id
        There are two types of categories available:
        - Global categories
        - Specific categories

        CatsId is for specific category ids

        .PARAMETER RawOutput
        You can provide a [Ref] parameter to the function to get back the raw response from the invoke to the I-doIt API.

        You have to put the parameter in parantheses like this:
        -RawOutput ([Ref]$Output)

        The return value is [Microsoft.PowerShell.Commands.HtmlWebResponseObject

        .EXAMPLE
        PS> Get-IdoItCategory -Id 3411 -Category "C__CATG__ACCOUNTING"

        This command will return the accounting category for object 3411.

        .EXAMPLE
        PS> Get-IdoItCategory -Id 55 -CatsId 1

        This command will return the specific category with id 1 for object with id 55.

        .NOTES
        Version
        0.1.0 29.12.2017 CB initial release
        0.1.1 06.01.2018 CB Updated inline help; Added RawOuput parameter
        0.1.2 10.01.2018 CB Fixed pipline behavoir for the Id parameter
        0.2.0 15.01.2018 CB Added dynamic paramter category that pulls constant cache in the validate set
    #>

        [CmdletBinding()]
        Param (
            [Parameter (
                Mandatory = $True,
                ValueFromPipeline = $True,
                Position=0,
                ParameterSetName = "Category"
            )]
            [Parameter (
                Mandatory = $True,
                ValueFromPipeline = $True,
                Position=0,
                ParameterSetName = "CatgId"
            )]
            [Parameter (
                Mandatory = $True,
                ValueFromPipeline = $True,
                Position=0,
                ParameterSetName = "CatsId"
            )]
            [Int]$Id,

            [Parameter (
                Mandatory = $True,
                ParameterSetName = "CatgId"
            )]
            [Int]$CatgId,

            [Parameter (
                Mandatory = $True,
                ParameterSetName = "CatsId"
            )]
            [int]$CatsId,

            [Parameter (
                Mandatory = $False
            )]
            [Ref]$RawOutput
        )

        DynamicParam {
            $ParamName_Category = "Category"

            # Create the collection of attributes
            $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

            # Create and set the parameters' attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.Mandatory = $True
            $ParameterAttribute.ParameterSetName = "Category"


            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)
            # Create the dictionary
            $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
            # Generate and set the ValidateSet

            $Category = Get-IdoitCacheFile -CacheType Constant | Where-Object {(($_.type -eq "Global") -or ($_.type -eq "Specific"))}

            $arrSet = $Category.Const
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
            # Add the ValidateSet to the attributes collection
            $AttributeCollection.Add($ValidateSetAttribute)
            # Create and return the dynamic parameter
            $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParamName_Category, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParamName_Category, $RuntimeParameter)

            Return $RuntimeParameterDictionary
        }

        Begin {
            $Category = $PsBoundParameters[$ParamName_Category]
        }

        Process {

            $Params  = @{}
            $Params.Add("objID", $Id)

            Switch ($PSCmdlet.ParameterSetName) {
                "Category" { $Params.Add("category",$Category); Break }
                "CatgId" { $Params.Add("catgID",$CatgId); Break }
                "CatsId" { $Params.Add("catsID",$CatsId); Break }
            }

            $SplattingParameter = @{
                Method = "cmdb.category.read"
                Params = $Params
            }

            If ($PSBoundParameters.ContainsKey("RawOutput")) {
                $SplattingParameter.Add("RawOutput", $RawOutput)
            }

            Try {
                $ResultObj = Invoke-IdoIt @SplattingParameter
            }
            Catch {
                Throw $_
            }

            #We remove the original property id and rename objID to id to be consistant and be able to pipe results to
            #other Cmdlets

            ForEach ($O in $ResultObj) {

                If (@("CatgId", "CatsId") -contains $PSCmdlet.ParameterSetName) {

                    $O | Add-Member -MemberType NoteProperty -Name $PSCmdlet.ParameterSetName.ToLower() -Value $O.Id

                }
                Else {

                    $O | Add-Member -MemberType NoteProperty -Name "RefCategory" -Value $Category

                }

                $O.Id = $O.ObjID
                $O.PSObject.Properties.Remove("objID")

            }

            $ResultObj = $ResultObj | Add-ObjectTypeName -TypeName 'Idoit.Category'

            Return $ResultObj | ConvertFrom-IdoItResultObject
        }
    }