Public/New-IdoItObject.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
function New-IdoItObject {
    <#
        .SYNOPSIS
        New-IdoItObject

        .DESCRIPTION
        New-IdoItObject lets you create a new object.

        DYNAMIC PARAMETERS
        -Category <String>
            This parameter defines the dialog category on the general category. It is a dynamic parameter
            that will pull the available values in real time.

        -Purpose <String>
            This parameter defines the dialog purpose on the general category. It is a dynamic parameter
            that will pull the available values in real time.

        .PARAMETER Type
        This parameter either the type id or the the type name of the object you wan't to create.

        .PARAMETER Title
        This the title of the object you are creating.


        .EXAMPLE
        PS> New-IdoItObject -Type 5 -Title "srv12345.domain.de" -Purpose "Production"

        This will create a new Object of type 5 (C__OBJTYPE__SERVER) with the name srv12345.domain.de
        and the purpose "Production"

        .NOTES
        Version
        0.1.0 29.12.2017 CB initial release
    #>

        [CmdletBinding(SupportsShouldProcess = $True)]
        Param (
            [Parameter( Mandatory = $True,
                        Position = 0 )]
            [Alias("TypeId")]
            $Type,

            [Parameter( Mandatory = $True )]
            [string]$Title
        )

        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 = $false

            # 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
            $arrSet = (Get-IdoItDialog -Category "C__CATG__GLOBAL" -Property "category").title
            $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)

            $ParamName_Purpose = "Purpose"
            # 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 = $false
            #$ParameterAttribute.Position = 1
            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)
            # Generate and set the ValidateSet
            $arrSet = (Get-IdoItDialog -Category "C__CATG__GLOBAL" -Property "purpose").title
            $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_Purpose, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParamName_Purpose, $RuntimeParameter)

            Return $RuntimeParameterDictionary
        }

        Begin {
            $Category = $PsBoundParameters[$ParamName_Category]
            $Purpose =  $PsBoundParameters[$ParamName_Purpose]

            If ($Category) {
                $CategoryElementId = (Get-IdoItDialog -Category "C__CATG__GLOBAL" -Property "category" | Where-Object {$_.title -eq $Category }).id
            }

            If ($Purpose) {
                $PurposeElementId = (Get-IdoItDialog -Category "C__CATG__GLOBAL" -Property "purpose" | Where-Object {$_.title -eq $Purpose }).id
            }


        }

        Process {

            $Params = @{}
            $Params.Add("type", $Type)
            $Params.Add("title", $Title)

            If ($Category) {
                $Params.Add("category", $CategoryElementId)
            }

            If ($Purpose) {
                $Params.Add("purpose", $PurposeElementId)
            }
            If ($PSCmdlet.ShouldProcess("Creating new object $Title of type $Type")) {
                $ResultObj = Invoke-IdoIt -Method "cmdb.object.create" -Params $Params


                If ($ResultObj.Success -eq $True) {
                    $ResultNewObj = Get-IdoItObject -Id $ResultObj.id
                    Return $ResultNewObj
                }
                Else {
                    Return $ResultObj
                }
            }
        }
    }