Forum Discussion

mhashemi's avatar
2 years ago

PATCH website custom property via API

Anyone have some PowerShell that will update the custom properties of a Website (ping check)?

I have been fiddling around and the following code will update properties like name or host (for example), but will not add a custom property. The code runs without error and returns the website as expected, but the change is not reflected in the portal.

[string]$AccessId = ''
[securestring]$AccessKey = ('' | ConvertTo-SecureString -AsPlainText -Force)
[string]$AccountName = ''
[int]$Id = '' #website id
[hashtable]$PropertyTable = @{ 'propertyName' = 'test' }
[string]$httpVerb = 'PATCH'
[string]$resourcePath = "/website/websites"
$AllProtocols = [System.Net.SecurityProtocolType]'Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols

$resourcePath += "/$Id"

$data = $PropertyTable | ConvertTo-Json -Depth 6
$enc = [System.Text.Encoding]::UTF8
$encdata = $enc.GetBytes($data)

$url = "https://$AccountName.logicmonitor.com/santaba/rest$resourcePath"
$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
$requestVars = $httpVerb + $epoch + $data + $resourcePath
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes([System.Runtime.InteropServices.Marshal]::PtrToStringAuto(([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($AccessKey))))
$signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
$signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
$signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))
$headers = @{
    "Authorization" = "LMv1 $accessId`:$signature`:$epoch"
    "Content-Type"  = "application/json"
    "X-Version"     = 3
}

$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers -Body $encdata -ErrorAction Stop

$response

 

4 Replies

  • The UI is API driven.. when in doubt fire up Dev Tools in your browser and modify anything and see the payload it uses.. 

    I have had to do this several times for undocumented API endpoints works a treat!

  • I'd have to look into the documentation, but i think the problem is that you're trying to set 'propertyName' at the root of the device, when it should go under the "customProperties" object. Also, when patching, I think you have to add a query parameter that specifies which fields you are attempting to patch. Don't have access to a LM portal so i can't test at the moment.

  • Any chance you got this working?  Trying to figure out how to add a custom property to a website via API as well.  I’m actually not sure that the API supports it as it isn’t mentioned in the documentation.

    Edit: Nevermind, I just got it working. It looks like it has to be set in the “properties” object, not “customProperties” like you would use on a device record. For example:

    {
    "displayName": "Website Monitor 1",
    "description": "New website",
    "properties": [
    {
    "name": "myProperty",
    "value": "abcdefg"
    },
    {
    "name": "another.property",
    "value": "hijklmnop"
    }
    ]
    }