PowerShell/Web
Aller à la navigation
Aller à la recherche
Web et API...
Postman et PowerShell
Travis Roberts :
- How to use Postman and PowerShell to Query API data
Invoke-RestMethod
JackedProgrammer :
- PowerShell Quick Tips : Invoke-RestMethod vs Invoke-WebRequest (Calling Rest APIs)
Exemple pour GLPI et ticket
<#
Description : Pour GLPI, mise à jour du titre d'un ticket
Usage :
Auteur : fylip22
Version : 1.0
Révisions :
- 1.0 (29/01/2024) : création du script
#>
# Paramètres de connexion à GLPI
$glpiServeur = "http://phpnet1/"
$utilisateur = "compteutilisateur"
$motDePasse = "motdepasse"
$ticketId = 123 # Remplacez par l'ID du ticket que vous souhaitez mettre à jour
$TicketNouveauTitre = "Nouveau titre du ticket"
# Fonction pour obtenir le token d'authentification
function Get-GLPISessionToken {
$url = "$glpiServeur/apirest.php/initSession"
$body = @{
login_name = $utilisateur
login_password = $motDePasse
} | ConvertTo-Json
$response = Invoke-RestMethod -Uri $url -Method Post -Body $body -Headers @{ "Content-Type" = "application/json" }
return $response.session_token
}
# Fonction pour mettre à jour le titre du ticket
function Update-GLPITicketTitle {
param (
[string]$token,
[int]$ticketId,
[string]$TicketNouveauTitre
)
$url = "$glpiServeur/apirest.php/ticket/$ticketId"
$ticketData = Invoke-RestMethod -Uri $url -Method Get -Headers @{ "Session-Token" = $token }
# Modifier le titre du ticket
$ticketData.name = $TicketNouveauTitre
# Mettre à jour le ticket avec les nouvelles données
Invoke-RestMethod -Uri $url -Method Put -Body ($ticketData | ConvertTo-Json) -Headers @{
"Content-Type" = "application/json"
"Session-Token" = $token
}
}
# Obtenir le token d'authentification
$sessionToken = Get-GLPISessionToken
# Mettre à jour le titre du ticket
Update-GLPITicketTitle -token $sessionToken -ticketId $ticketId -newTitle $TicketNouveauTitre