GLPI/Configuration/Générale/API/Exemple 4
Aller à la navigation
Aller à la recherche
Le site It-Connect propose un exemple d'utilisation de l'API de GLPI pour récupérer des informations de l'Active Directory et mettre à jour GLPI.
Généralité
It-Connect
GLPI : comment utiliser l’API avec un script PowerShell ? Voici un exemple pratique !
Problème connu
Conflit entre authentification SSO et jeton d'API
Si l'authentification se fait via SSO, il peut y avoir un conflit avec l'authentification via le jeton.
Voir le forum GLPI, "Erreur 401 - Utilisation de l'API en PowerShell avec GLPITools" :
Problème résolu... Nous utilisons du SSO en production pour s'authentifier sur GLPI avec nos comptes LDAP. Ceci faisait conflit avec la méthode d'authentification par jetons utilisée par l'API. Modifier la configuration d'Apache... <Location /glpi/apirest.php> Require all granted </Location>
Autre
[RESOLU] Glpi_agent - erreur connexion agent
Script complet
# Description: This script retrieves a list of computers from Active Directory and updates their descriptions in GLPI using the GLPI REST API. # Variables $GLPIServer = "https://support.it-connect.tech" $GLPIAppToken = "Cbnpe8EuH4N3giGgdX4xxxxxxxxxxxxxxxxx" $GLPIUserToken = "7T1DfhhOLPl9duAaExlbbxxxxxxxxxxxxx" $ADSearchBase = "OU=PC,OU=IT-Connect,DC=it-connect,DC=local" # Function to invoke GLPI REST API with error handling. function Invoke-GLPIRequest { param( [Parameter(Mandatory)] [string]$Method, [Parameter(Mandatory)] [string]$Uri, [Parameter(Mandatory)] [hashtable]$Headers, $Body = $null ) $ContentType = "application/json" try { if ($Body) { $BodyJson = $Body | ConvertTo-Json -Depth 5 } else { $BodyJson = $null } $result = Invoke-RestMethod -Method $Method -Headers $Headers -Uri $Uri -Body $BodyJson ` -ContentType $ContentType -SkipCertificateCheck -ErrorAction Stop return $result } catch { Write-Host "ERROR - GLPI API : Request [$Method] $Uri failed. $_" -ForegroundColor Red return $null } } if ($GLPIServer) { Write-Host "INFO - GLPI API : Initializing a session with the remote server..." -ForegroundColor Green # Open a session and get the session token $initHeaders = @{ "Authorization" = "user_token $GLPIUserToken" "App-Token" = $GLPIAppToken } $initUri = "$GLPIServer/apirest.php/initSession" $initResult = Invoke-GLPIRequest -Method "GET" -Uri $initUri -Headers $initHeaders if ($initResult -and $initResult.session_token) { $session_token = $initResult.session_token Write-Host "INFO - GLPI API : Session token is $session_token" -ForegroundColor Green Write-Host "INFO - Retrieving computer list from Active Directory..." -ForegroundColor Green $ComputerList = Get-ADComputer -Filter * -SearchBase $ADSearchBase -Properties Description | Select-Object Name, Description foreach ($Computer in $ComputerList) { $ComputerName = $Computer.Name $ComputerDesc = $Computer.Description if ($ComputerDesc) { Write-Host "INFO - GLPI API : Computer '$ComputerName' has a description to update in GLPI." -ForegroundColor Green Write-Host "INFO - Searching for the computer '$ComputerName' in GLPI..." -ForegroundColor Green $searchHeaders = @{ "Session-Token" = $session_token "App-Token" = $GLPIAppToken } $searchUri = "$GLPIServer/apirest.php/search/Computer?criteria[0][link]=AND&criteria[0][itemtype]=Computer&criteria[0][field]=1&criteria[0][searchtype]=contains&criteria[0][value]=$ComputerName&forcedisplay[0]=2" $searchResult = Invoke-GLPIRequest -Method "GET" -Uri $searchUri -Headers $searchHeaders if ($searchResult) { $NumberOfResult = $searchResult.totalcount if ($NumberOfResult -eq 1) { Write-Host "INFO - GLPI API : One computer found for '$ComputerName'." -ForegroundColor Green # Retrieve the computer ID (assumed to be contained in the first element of data) $ComputerID = $searchResult.data.2 if (-not $ComputerID) { Write-Host "ERROR - GLPI API : Computer ID not found for '$ComputerName'." -ForegroundColor Red continue } $updateHeaders = @{ "Session-Token" = $session_token "App-Token" = $GLPIAppToken } $updateUri = "$GLPIServer/apirest.php/Computer/$ComputerID" $body = @{ input = @{ contact_num = $ComputerDesc } } $updateResult = Invoke-GLPIRequest -Method "PUT" -Uri $updateUri -Headers $updateHeaders -Body $body if ($updateResult) { Write-Host "INFO - GLPI API : Updated computer '$ComputerName' in GLPI." -ForegroundColor Green } else { Write-Host "ERROR - GLPI API : Failed to update computer '$ComputerName'." -ForegroundColor Red } } elseif ($NumberOfResult -gt 1) { Write-Host "ERROR - GLPI API : Multiple ($NumberOfResult) computers found for '$ComputerName'." -ForegroundColor Red } else { Write-Host "ERROR - GLPI API : No computer found for '$ComputerName'." -ForegroundColor Red } } else { Write-Host "ERROR - GLPI API : An error occurred during the search for '$ComputerName'." -ForegroundColor Red } } } # Kill the GLPI session $killHeaders = @{ "Session-Token" = $session_token "App-Token" = $GLPIAppToken } $killUri = "$GLPIServer/apirest.php/killSession" Invoke-GLPIRequest -Method "GET" -Uri $killUri -Headers $killHeaders | Out-Null Write-Host "INFO - GLPI API : Session ($session_token) terminated." -ForegroundColor Green } else { Write-Host "ERROR - GLPI API : Unable to open a session and retrieve a token." -ForegroundColor Red } }