You are not logged in.

Announcement

 Téléchargez la dernière version stable de GLPI      -     Et vous, que pouvez vous faire pour le projet GLPI ? :  Contribuer
 Download last stable version of GLPI                      -     What can you do for GLPI ? :  Contribute

#1 2024-09-12 11:31:24

leroyph
Member
Registered: 2024-09-12
Posts: 1

Update item doesn't work as attend

Hi,
When I try to update an item like for example the comment or any other item for a computer, the command creates a new computer instead of updating the original one.

I'm sure of the computer ID.
Do you have any ideas?
By the way, do you know how to update the “tag” plugin field for a computer?

Here is my PowerShell command:

$json =
{
    "input":  [
                  {
                      "id":  16941,
                      "comment":  "test"
                  }
              ]
}

$URLSESSION =
https://xxxxxxxxxxxxxxxxxxxxxxxxxxxx/apirest.php/Computer/16941?session_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&app_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Invoke-WebRequest -Method PATCH -Uri $URLSESSION  -Body $json -ContentType 'application/json' -UseDefaultCredentials


Return:

StatusCode        : 200
StatusDescription : OK
Content           :
RawContent        : HTTP/1.1 200 OK
                    Pragma: no-cache
                    Content-Length: 0
                    Cache-Control: no-store, no-cache, must-revalidate
                    Content-Type: text/html; charset=UTF-8
                    Date: Wed, 11 Sep 2024 15:11:20 GMT
                    Expires: Thu, 19 ...
Forms             : {}
Headers           : {[Pragma, no-cache], [Content-Length, 0], [Cache-Control, no-store, no-cache, must-revalidate], [Content-Type, text/html; charset=UTF-8]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : mshtml.HTMLDocumentClass
RawContentLength  : 0


Thanks in advance for your time and your help !

Offline

#2 2024-10-08 14:03:45

Dead-Red
Member
From: France - Aisne - 02
Registered: 2021-04-10
Posts: 23

Re: Update item doesn't work as attend

If your computer is duplicated when you modify it, it's because you used the “POST” method and not PUT.

Personally, to edit a field after retrieving it via :

Invoke-RestMethod "$AppURL/listSearchOptions/Computer" -Method Get -Headers @{"session-token"=$SessionToken.session_token; "App-Token" = "$AppToken"} -ContentType 'application/json'

I use this command:

$ItemID = 929                                    # Computer ID
$Field_Name="customeridfield"             # Custom Field Name
$Field_NewValue = "Nouvelle Valeur 2"   # Value for that Custom Field Name

$body = @{
    input = @{
        id=  $ItemID                               #Computer ID
        $Field_Name = $Field_NewValue
    }
} | ConvertTo-Json

Invoke-RestMethod -Uri "$($AppURL)/Computer" -Headers @{"session-token"=$SessionToken.session_token; "App-Token" = "$AppToken"} -ContentType 'application/json'  -Method PUT -Body $body

Offline

Board footer

Powered by FluxBB