Public/APICore/Session/Set-GlpiToolsInitSession.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
<#
.SYNOPSIS
    Function which init API session.
.DESCRIPTION
    Function gets nesessery information from Config file, then initialize session, and return object with session_token property.
.EXAMPLE
    PS C:\Users\Wojtek> Set-GlpiToolsInitSession
    Run command like that and you will initiate session with API GLPI
.INPUTS
    None, inside script Function uses Get-GlpiToolsConfig to get data from Config.
.OUTPUTS
    SessionToken parameter
.NOTES
    PSP 12/2018
#>


function Set-GlpiToolsInitSession {
    [CmdletBinding()]
    param (
        
    )
    
    begin {

        $AppToken = Get-GlpiToolsConfig | Select-Object -ExpandProperty AppToken
        $UserToken = Get-GlpiToolsConfig | Select-Object -ExpandProperty UserToken
        $PathToGlpi = Get-GlpiToolsConfig | Select-Object -ExpandProperty PathToGlpi
    }
    
    process {
        $params = @{
            headers = @{
                'Content-Type'  = 'application/json'
                'Authorization' = "user_token $($UserToken)"
                'App-Token'     = $AppToken 
            }
            method  = 'get'
            uri     = "$($PathToGlpi)/initSession/" 
        }
        $InitSession = Invoke-RestMethod @params
        
        $SessionHash = [ordered]@{
            'SessionToken' = $InitSession.session_token
        }
        $object = New-Object -TypeName PSCustomObject -Property $SessionHash
        $SessionToken += $object
        
    }
    
    end {
        $SessionToken
    }
}