Public/APICore/Search/Search-GlpiToolsItems.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
<#
.SYNOPSIS
    Function is using GLPI Search Engine to get informations.
.DESCRIPTION
    Function Search for specific component in GLPI
    Parameters are the names of options in GLPI
    Remember that, names used in cmdlet coming from glpi URL, and can be hard to understand, but most of them are intuitional.
    To get name you always have to look at the URL in GLPI, for example "http://glpi/front/computer.php" where "computer" is the name to use in parameter.
.PARAMETER SearchFor
    You can use this function with -SearchFor parameter.
    Using TAB button you can choose desired option.
    You can add your custom parameter options to Parameters.json file located in Private folder
.PARAMETER SearchType
    You can use this function with -SearchType parameter.
    Using TAB button you can choose desired option.
.PARAMETER SearchField
    You can use this function with -SearchField parameter.
    This is an optional parameter, default value is 1 which is called Name in GLPI.
    This parameter can take pipeline input, even from Get-GlpiToolsListSearchOptions cmdlet.
.PARAMETER SearchValue
    You can use this function with -SearchValue parameter.
    This parameter can take pipeline input.
    Provide value to the function, which is used to search for.
.PARAMETER SearchTrash
    You can use this function with -SearchTrash parameter.
    This is an optional switch parameter.
.EXAMPLE
    PS C:\> Search-GlpiToolsItems -SearchFor Computer -SearchType contains -SearchValue DC
    Example will show every asset which contains value "DC" in the Name from Asset->Computers.
.EXAMPLE
    PS C:\> Search-GlpiToolsItems -SearchFor Computer -SearchType contains -SearchValue DC -SearchField 1
    Example will show every asset which contains value "DC" in the Name from Asset->Computers.
    SearchFiled can be retrieved from Get-GlpiToolsListSearchOptions cmdlet, you can provide it throught pipeline.
.EXAMPLE
    PS C:\> Search-GlpiToolsItems -SearchFor Computer -SearchType contains -SearchValue DC -SearchField 1 -SearchInTrash
    Example will show every asset which contains value "DC" in the Name from Asset->Computers.
    SearchFiled can be retrieved from Get-GlpiToolsListSearchOptions cmdlet, you can provide it throught pipeline.
    SearchInTrash will allow you to search for assets from trash.
.INPUTS
    Only for -SearchValue, and -SearchField.
.OUTPUTS
    Function returns PSCustomObject with property's of Search results from GLPI
.NOTES
    PSP 02/2019
#>


function Search-GlpiToolsItems {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)]
        [String]$SearchFor,

        [parameter(Mandatory = $true)]
        [ValidateSet("contains",
            "equals",
            "notequals",
            "lessthan",
            "morethan",
            "under",
            "notunder")]
        [String]$SearchType,

        [parameter(Mandatory = $false,
            ValueFromPipeline = $true)]
        [String]$SearchField = 1,

        [parameter(Mandatory = $true,
            ValueFromPipeline = $true)]
        [String]$SearchValue,

        [parameter(Mandatory = $false)]
        [ValidateSet("Yes", "No")]
        [String]$SearchInTrash = "No"
    )
    
    begin {
        $SearchArray = [System.Collections.Generic.List[PSObject]]::New()

        $AppToken = $Script:AppToken
        $PathToGlpi = $Script:PathToGlpi
        $SessionToken = $Script:SessionToken

        $AppToken = Get-GlpiToolsConfig | Select-Object -ExpandProperty AppToken
        $PathToGlpi = Get-GlpiToolsConfig | Select-Object -ExpandProperty PathToGlpi
        $SessionToken = Set-GlpiToolsInitSession | Select-Object -ExpandProperty SessionToken

        if ($SearchInTrash -eq "Yes") {
            $Trash = 1
        }
        else {
            $Trash = 0
        }
        
        $ListSearchOptions = Get-GlpiToolsListSearchOptions -ListOptionsFor $SearchFor
    }
    
    process {
        
        $params = @{
            headers = @{
                'Content-Type'  = 'application/json'
                'App-Token'     = $AppToken
                'Session-Token' = $SessionToken
            }
            method  = 'get'
            uri     = "$($PathToGlpi)/search/$($SearchFor)?is_deleted=$($Trash)&as_map=0&criteria[0][field]=$($SearchField)&criteria[0][searchtype]=$($SearchType)&criteria[0][value]=$($SearchValue)&search=Search&itemtype=$($SearchFor)&range=0-9999999999999"
        }
            
        $SearchResult = Invoke-RestMethod @params
        
        try {
            $SearchResults = $SearchResult | Select-Object -ExpandProperty data -ErrorAction Stop
        } catch {

        }
        
        foreach ($SearchItem in $SearchResults) {
            $SearchHash = [ordered]@{}
            $DataResult = $SearchItem.PSObject.Properties | Select-Object -Property Name, Value 

            foreach ($Data in $DataResult) {
                    
                $Property = $ListSearchOptions | Where-Object {$_.Id -eq $Data.Name } | Select-Object -ExpandProperty Name
                $Table = $ListSearchOptions | Where-Object {$_.Id -eq $Data.Name } | Select-Object -ExpandProperty Table
                $Value = $Data.Value

                if ($SearchHash.Keys -contains $Property) {
                    $NewProperty = $Property + "_" + $Table
                    $SearchHash.Add($NewProperty, $Value)
                } else {
                    $SearchHash.Add($Property, $Value)
                }
                
            }

            $object = [pscustomobject]$SearchHash
            $SearchArray.Add($object)
        }

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

$SearchForValidate = {
    param ($commandName, $parameterName, $stringMatch)
    $ModulePath = Split-Path (Get-Module -Name GlpiTools).Path -Parent
    (Get-Content "$($ModulePath)\Private\Parameters.json" | ConvertFrom-Json).GlpiComponents | Where-Object {$_ -match "$stringMatch"}
}
$SearchTypeValidate = {
    param ($commandName, $parameterName, $stringMatch)
    $ModulePath = Split-Path (Get-Module -Name GlpiTools).Path -Parent
    (Get-Content "$($ModulePath)\Private\Parameters.json" | ConvertFrom-Json).SearchType | Where-Object {$_ -match "$stringMatch"}
}
Register-ArgumentCompleter -CommandName Search-GlpiToolsItems -ParameterName SearchFor -ScriptBlock $SearchForValidate
Register-ArgumentCompleter -CommandName Search-GlpiToolsItems -ParameterName SearchType -ScriptBlock $SearchTypeValidate