Update contact via PowerShell

Does anyone have any sample code that I could steal for updating an existing contact via the API using Powershell?
I don’t have access to my docs at the minute so I cannot provide any sample code but I can create a new contact via PowerShell using Invoke-RestMethod -Method POST but that makes multiple contacts, I want to update a contact if it exists using PUT but it keeps failing.
I think I’m not processing the nested JSON properly or something. Would anyone have some working PS code?

Thanks

1 Like

Any attributes you want to update just add them as a separate line in the payload. I included a couple example attributes. Hope this helps.

###Script Variables####
$SyncroSubdomain = "insert syncro subdomain"
$SyncroAPIKey = "insert syncro api key"
$SyncroContactID = 1092301 <#Replace with the contact ID you want to update/#>
$Payload=@{
    phone = "1234567890"
    mobile = "" <#Use empty quotes to clear a field in Syncro/#>
    email = "test@example.com"
    api_key=$SyncroAPIKey
}

###Update Contact###
Invoke-RestMethod -Uri "https://$($SyncroSubdomain).syncromsp.com/api/v1/contacts/$($SyncroContactID)" -Method PUT -body (ConvertTo-Json $Payload) -ContentType application/json
3 Likes

@matthew5
Thanks very much that did the trick.

I haven’t had time to implement it into my script but it looks like I wasn’t adding the API key in the body.

Thanks again for that, it’s been very helpful and greatly appreciated!!
Luke

1 Like

Your original Syntax you were using -Method POST when you should be using -Method PUT or PATCH :smiley:

Here are some tips on the different command flags of REST: ( think basic programming: CRUD or DAVE)
DELETE | ADD | VIEW | EDIT or CREATE | REVIEW | UPDATE | DELETE

  • PUT replaces a resource (even if that resource doesn’t yet exist), while POST usually adds a new resource. They can both be used to Create new resources, but PUT is mainly used to Update existing resources.
  • PATCH is used to Update part of a resource, whereas PUT is only used to Update an entire resource (by replacing it).
  • POST is processed “according to the resource’s own specific semantics” (per the RFC), making it something of a catch-all. It can even have Update functionality, like when sent to an /updates/ subresource.