Public/Connect-IdoIt.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
195
196
197
198
199
200
function Connect-IdoIt {
<#
    .SYNOPSIS
    Connect-Cmdb initalize the session to the idoit cmdb.

    .DESCRIPTION
    Connect-Cmdb initalize the session to the idoit cmdb.

    .PARAMETER Credential
    User with appropiate permissions to access the cmdb.

    .PARAMETER ApiKey
    This is the apikey you define in idoit unter Settings-> Interface-> JSON-RPC API to access the api

    .PARAMETER Uri
    This is the Uri to the idoit JSON-RPC API. It is always in the format http[s]://your.i-doit.host/src/jsonrpc.php

    .PARAMETER Settings
    This parameter will be removed - for hashtable you can use paramter splatting

    .PARAMETER ConfigFile
    You can provide a path to a settings file in json format. It must containt username, password, apikey and
    uri as key-value pairs

    .PARAMETER NoTLS
    If you provide this switch parameter, you can connect to unsecure http endpoints without TLS encryption.

    Be aware, that the username and password transferd plaintext in the header.

    .PARAMETER ForceCacheRebuild
    ForceCacheRebuild instructs Connect-IdoIt to re-create all the cache files regardless of their age. This can be usefull
    if there are a lot of changes going on in your i-doit installation and you wan't to be sure you have the latest configuration

    .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 a Microsoft.PowerShell.Commands.HtmlWebResponseObject

    .EXAMPLE
    PS> Connect-Cmdb -Username 'admin' -Password 'admin' -Uri 'https://demo.i-doit.com/src/jsonrpc.php' -ApiKey 'asdaur'

    This will esatblish a session to demo.i-doit.com with api key asdaur for user admin

    .NOTES
    Version
    0.1.0 29.12.2017 CB initial release
    0.2.0 31.12.2017 CB some major redesign for the parameter sets
    0.3.0 02.01.2018 CB Using Credential object instead of username & password (https://github.com/PowerShell/PSScriptAnalyzer/issues/363)
    0.3.1 03.01.2018 CB Added Verbose/Debug output, ParameterSplatting to the Invoke and RawOuput
    0.4.0 15.01.2018 CB Beginning to include caching of some static env constants etc. to the user dir
#>

    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "")]
    [Cmdletbinding()]

    Param(
        [Parameter (
            Mandatory = $True,
            ParameterSetName = "SetA"
        )]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,

        [Parameter (
            Mandatory = $True,
            ParameterSetName = "SetA",
            Position = 3
        )]
        [String]$ApiKey,

        [Parameter (
            Mandatory = $True,
            ParameterSetName = "SetA",
            Position = 4
        )]
        [String]$Uri,

        [Parameter (
            Mandatory = $True,
            ParameterSetName = "SetB",
            Position = 0
        )]
        [ValidateScript ({
            $RequiredKeys= @("Username","Password","ApiKey","Uri")
            $ProvidedKeys = @($_.Keys)
            -Not @($RequiredKeys| Where-Object {$ProvidedKeys -notcontains $_}).Count
        })]
        [Hashtable]$Settings,

        [Parameter (
            Mandatory = $True,
            ParameterSetName = "SetC",
            Position = 0
        )]
        [ValidateNotNullOrEmpty()]
        [String]$ConfigFile,

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

        [Parameter (
            Mandatory = $False
        )]
        [Switch]$NoTls,

        [Parameter (
            Mandatory = $False
        )]
        [Switch]$ForceCacheRebuild


    )

    # When –Debug is used, we will not get a prompt each time it is used :-) Thanks to Boe Prox
    # https://learn-powershell.net/2014/06/01/prevent-write-debug-from-bugging-you/
    If ($PSBoundParameters['Debug']) {
        $DebugPreference = 'Continue'
    }

    If ($PSCmdlet.ParameterSetName -eq "SetA") {
        $SettingsParams = @{
            Username = $Credential.Username
            Password = $Credential.GetNetworkCredential().Password #$Credential.Password
            ApiKey = $ApiKey
            Uri = $Uri
        }

    } ElseIf ($PSCmdlet.ParameterSetName -eq "SetC") {
        $SettingsParams = Get-Content $ConfigFile -Raw | ConvertFrom-Json
        $SecurePassword = ConvertTo-SecureString $SettingsParams.Password
        $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $SettingsParams.Username, $SecurePassword

        $SettingsParams.Password = $Credential.GetNetworkCredential().Password

    }

    Else {
        $SettingsParams = $Settings
    }

    #Test the uri uses https because we are sending credentials
    If ( ( -Not (Test-IdoitHttpSSL -Uri $SettingsParams.Uri)) -And (-Not ($NoTls))) {
        Throw "Please use https or provide -NoTls paramter to lower security"
    }


    New-Variable -Name CmdbApiKey -Scope Global -Value $SettingsParams.ApiKey -Force:$True
    New-Variable -Name CmdbUri -Scope Global -Value $SettingsParams.Uri -Force:$True


    $Params = @{}
    $Headers = @{"Content-Type" = "application/json"; "X-RPC-Auth-Username" = $SettingsParams.Username; "X-RPC-Auth-Password" = $SettingsParams.Password}

    $SplattingParameter = @{
        Method = "idoit.login"
        Params = $Params
        Headers = $Headers
        Uri = $SettingsParams.Uri
    }

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

    $ResultObj = Invoke-IdoIt @SplattingParameter


    $LoginResult = [pscustomobject]@{
        Account  = $ResultObj.name
        Tenant   = $ResultObj.'client-name'
        TenantId = $ResultObj.'client-id'
    }

    New-Variable -Name CmdbSession -Scope Global -Value $ResultObj.'session-id' -Force:$True

    #Before we begin, we check that we are running against correct version. Also we know that everything
    #is working.
    $CmdbVer = [Version](Get-IdoItVersion).Version
    If (!$CmdbVer) {
        Throw "There was an unkown problem getting the i-doit version."
    }
    Elseif (($CmdbVer.Major -lt 1) -or ($CmdbVer.Minor -lt 7)) {
        Throw "PSCmdb needs minimum Version 1.7 to work. You are running i-doit $($CmdbVer.Major).$($CmdbVer.Minor)"
    }

    If ( -Not (Test-IdoitCacheFile -CacheType Constant -Expiry (New-Timespan -Days 30))) {

        Write-Verbose "Updating idoit constant cache file"
        New-IdoItCacheFile -CacheType Constant -CacheData (Get-IdoItConstant)

    }

    Return $LoginResult
}