Public/Setup/Plugins/Get-GlpiToolsPlugins.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
<#
.SYNOPSIS
    Function to get GLPI Plugin list.
.DESCRIPTION
    Function is getting Plugin list from GLPI.
    You can choose for Raw list, or list with translated status.
.PARAMETER Raw
    Switch parameter, you can use this parameter to get raw Plugin list.
    Raw I mean, as that as it is from original object from GLPI.
.EXAMPLE
    PS C:\> Get-GlpiToolsPlugins
    Gets Plugin list from GLPI, with translated status from number to Enabled etc...
.EXAMPLE
    PS C:\> Get-GlpiToolsPlugins -Raw
    Gets Plugin list from GLPI, object isn't translated. Original as it comes from GLPI.
.INPUTS
    None
.OUTPUTS
    Function returns PSCustomObject with property's of Plugins from GLPI
.NOTES
    PSP 04/2019
#>


function Get-GlpiToolsPlugins {
    [CmdletBinding()]
    param (
        [Switch]$Raw
    )
    
    begin {
        $SessionToken = $Script:SessionToken
        $AppToken = $Script:AppToken
        $PathToGlpi = $Script:PathToGlpi
    
        $SessionToken = Set-GlpiToolsInitSession | Select-Object -ExpandProperty SessionToken
        $AppToken = Get-GlpiToolsConfig | Select-Object -ExpandProperty AppToken
        $PathToGlpi = Get-GlpiToolsConfig | Select-Object -ExpandProperty PathToGlpi

        $PluginsArray = [System.Collections.Generic.List[PSObject]]::New()

    }
    
    process {
        $params = @{
            headers = @{
                'Content-Type'  = 'application/json'
                'App-Token'     = $AppToken
                'Session-Token' = $SessionToken
            }
            method  = 'get'
            uri     = "$($PathToGlpi)/Plugin/" 
        }
        $Plugins = Invoke-RestMethod @params

        if ($Raw) {
            foreach ($Plugin in $Plugins) {
                $PluginHash = [ordered]@{ }
                $PluginProperties = $Plugin.PSObject.Properties | Select-Object -Property Name, Value 
                
                foreach ($PluginProp in $PluginProperties) {
                    $PluginHash.Add($PluginProp.Name, $PluginProp.Value)
                }
                $object = [pscustomobject]$PluginHash
                $PluginsArray.Add($object)
            }
        } else {
            foreach ($Plugin in $Plugins) {
                $PluginHash = [ordered]@{ }
                $PluginProperties = $Plugin.PSObject.Properties | Select-Object -Property Name, Value 
                
                foreach ($PluginProp in $PluginProperties) {
                
                    if (($PluginProp.Name -eq "state") -and ($PluginProp.Value -eq "1")) {
                        $StateResolved = "Enabled"
                        $PluginHash.Add($PluginProp.Name, $StateResolved) 
                    } elseif (($PluginProp.Name -eq "state") -and ($PluginProp.Value -eq "4")) {
                        $StateResolved = "Installed / not activated"
                        $PluginHash.Add($PluginProp.Name, $StateResolved)
                    } elseif (($PluginProp.Name -eq "state") -and ($PluginProp.Value -eq "2")) {
                        $StateResolved = "Not installed"
                        $PluginHash.Add($PluginProp.Name, $StateResolved)
                    } else {
                        $PluginHash.Add($PluginProp.Name, $PluginProp.Value)
                    }
                    
                }
                $object = [pscustomobject]$PluginHash
                $PluginsArray.Add($object)
            }
        }

        $PluginsArray
        $PluginsArray = [System.Collections.Generic.List[PSObject]]::New()
    }
    
    end {
        Set-GlpiToolsKillSession -SessionToken $SessionToken
    }
}