Created
February 14, 2021 01:00
-
-
Save Maelstrom96/6ec7067699b50d6218de07e73de0f1a9 to your computer and use it in GitHub Desktop.
SyncroMSP - Modify asset custom field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.Synopsis | |
Update an asset custom field | |
.DESCRIPTION | |
To use this script, you have to update | |
.NOTES | |
Version: 1.0 | |
Author: Alexandre-Jacques St-Jacques | |
Creation Date: 13-02-2021 | |
Purpose/Change: Initial script development | |
#> | |
# The API token used for the request. Create a API key with permission "Assets - Edit" | |
$ApiToken = "abcdefg-12345678" | |
# Will usually be syncromsp.com | |
$ApiBaseURL = $env:RepairTechApiBaseURL | |
# Your account sub domain will magically be imported. | |
$ApiSubDomain = $env:RepairTechApiSubDomain | |
# The asset that you wish to update the custom field | |
# You can find the ID by looking at the end of the URL when viewing the asset. | |
$AssetID = "09876543" | |
# The custom field name to be updated | |
$CustomField = "Testing Custom Field" | |
# The value to assign to the custom field | |
$CustomFieldValue = "Value" | |
function Asset-Update-Field ($ApiToken,$Id,$CustomField,$CustomFieldValue) { | |
$headers = @{ | |
Content='application/json' | |
Authorization="Bearer $ApiToken" | |
} | |
$payload = @" | |
{ | |
"properties": { | |
"$CustomField": "$CustomFieldValue" | |
} | |
} | |
"@ | |
$ApiPath = "/api/v1/customer_assets" | |
$resp = try { | |
Invoke-RestMethod -Method PUT "https://$($ApiSubDomain).$($ApiBaseURL)$($ApiPath)/$($AssetID)" -Headers $headers -Body "$payload" -ContentType "application/json" | |
} catch { | |
Write-Host "ERROR!" | |
$result = $_.Exception.Response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader($result) | |
$reader.BaseStream.Position = 0 | |
$reader.DiscardBufferedData() | |
$responseBody = $reader.ReadToEnd(); | |
Write-Host $responseBody | |
} | |
} | |
Asset-Update-Field -ApiToken $ApiToken -Id $AssetID -CustomField $CustomField -CustomFieldValue $CustomFieldValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment