Public/APICore/Configuration/Set-GlpiToolsConfig.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
<#
.SYNOPSIS
    Set GLPI Configuration File.
.DESCRIPTION
    This function preparing config file for GLPI API.
.PARAMETER AppToken
    Application Token, you can find Token in API Settings at your GLPI website.
.PARAMETER UserToken
    User Token, you can find inside user account settings.
.PARAMETER PathToGlpi
    Path To GLPI, you can find the path in API Settings at your GLPI website.
.EXAMPLE
    PS C:\Users\Wojtek> Set-GlpiToolsConfig -AppToken 'dsahu2uh2uh32gt43tf434t' -UserToken 'sdasg3123hg3t1ftf21t3' -PathToGlpi 'http://pathtoglpi/glpi'
    This example show how to set GLPI config file
.INPUTS
    None, you cannot pipe objects to Set-GlpiToolsConfig
.OUTPUTS
    None
.NOTES
    PSP 12/2018
#>


function Set-GlpiToolsConfig {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)]
        [string]$AppToken,
        [parameter(Mandatory = $true)]
        [string]$UserToken,
        [parameter(Mandatory = $true)]
        [string]$PathToGlpi
    )
    
    begin {

        if ($IsLinux) {
            $ConfigFile = "Configuration.json"
            $ConfigPath = "$env:HOME/.config/GlpiToolsConfig\"
            $Config = Join-Path -Path $ConfigPath -ChildPath $ConfigFile
        } else {
            $ConfigFile = "Configuration.json"
            $ConfigPath = "$env:LOCALAPPDATA\GlpiToolsConfig\"
            $Config = Join-Path -Path $ConfigPath -ChildPath $ConfigFile
        }

        if (!(Test-Path $ConfigPath)) {
            New-Item -Path $ConfigPath -ItemType Directory | Out-Null
        } else {
            try {
                Remove-Item -Path $Config -ErrorAction Stop    
            } catch {
                
            }
        }
    }

    process {
        $AppTokenSS = ConvertTo-SecureString -String $AppToken -AsPlainText -Force 
        $UserTokenSS = ConvertTo-SecureString -String $UserToken -AsPlainText -Force 
        $PathToGlpiSS = ConvertTo-SecureString -String $PathToGlpi -AsPlainText -Force 
        
        $AppTokenEncrypt = ConvertFrom-SecureString -SecureString $AppTokenSS 
        $UserTokenEncrypt = ConvertFrom-SecureString -SecureString $UserTokenSS 
        $PathToGlpiEncrypt = ConvertFrom-SecureString -SecureString $PathToGlpiSS 
    }
    
    end {
        $ConfigHash = [ordered]@{
            'AppToken'   = $AppTokenEncrypt
            'UserToken'  = $UserTokenEncrypt
            'PathToGlpi' = $PathToGlpiEncrypt
        }
        $ConfigHash | ConvertTo-Json | Out-File -FilePath $Config
    }
}