Forum Discussion

thedietz's avatar
thedietz
Icon for Neophyte rankNeophyte
3 years ago

Powershell API call to POST custom property

Hello.  Trying to get the code right with powershell to use the API to add a custom property to a device.   The custom property is a CityCode that we are using.

I am an Administrator in our instance so i should have access to do everything and i am also using Token Authentication for the API call.   Basing my code off of the example given here REST API v1 Examples | LogicMonitor

The response i am getting back when i run this is ERRMSG - Property name cannot be null or empty ... Status - 1007.  

 

code is as follows.  account info details removed for obvious reasons.   $resourcePath is the device i am attempting to POST to and $data is what i would like to add as a custom Property. 

 

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

<# account info #>
$accessId = 
$accessKey = 
$company = 

<# request details #>
$httpVerb = 'POST'
$resourcePath = '/device/devices/6101/properties'
$data = '{"Type":"customProperties","Name":"CityCode","Value":"CMP"}'


<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath

<# Get current time in milliseconds #>
$epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)

<# Concatenate Request Details #>
$requestVars = $httpVerb + $epoch + $data + $resourcePath

<# Construct Signature #>
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes($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()))

<# Construct Headers #>
$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$auth)
$headers.Add("Content-Type",'application/json')

<# Make Request #>
$response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers 

<# Print status and body of response #>
$status = $response.status
$body = $response.data| ConvertTo-Json -Depth 5

#Write-Host "Status:$status"
#Write-Host "Response:$body"