• 0
Amir Montasser

API - Posting widget updates via powershell

Question

Hello,

Trying to get the below script to work, but hitting a snag. I've tried to repurpose the DataSource Upload Example script from this page to get the desired result but no dice.

Param([Parameter(Mandatory=$true)][string]$name,[string]$GetInstance,[string]$GetType,[string]$Computername)
<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  

$accessId = '###'
$accessKey = '###'
$company = '###'

$httpVerb = 'PUT'
$widgetID = "350"
$resourcePath = "/dashboard/widgets/$widgetID"
$queryParams = ''


$boundary = [System.Guid]::NewGuid().ToString()
$LF = "\r\n"

$data = @"
    "items":[{"deviceGroupFullPath":"*","deviceDisplayName":"$computername","dataSourceDisplayName":"$name","instanceName":"$Name","dataPointName":"FolderGT60","groupBy":"instance","name":"$getinstance"}]
"@


$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams


$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($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()))


$auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$auth)
$headers.Add("Content-Type","multipart/form-data; boundary=----$boundary")


$response = Invoke-RestMethod -Uri $url -Method $httpVerb -body $data -Header $headers 


$status = $response.status
$body = $response.data

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

I assume that the '$headers.Add("Content-Type","multipart/form-data; boundary=----$boundary")' section, or '$boundary', is incorrect but have only really been looking into anything Logicmonitor-API-Powershell in the last 24 hours.

I'm getting HTTP 415 error Unsupported Media Typeh1 which I thought originally was from the $data section but I can't figure out any other way to pass the JSON into the variable.

Thanks, Amir.

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

The multipart boundary stuff is more related to sending large amount of data like XML file contents while most API calls including widget updates just need to pass direct JSON content. You might want to look at the examples at https://www.logicmonitor.com/support/rest-api-developers-guide/v1/rest-api-v1-examples/#Example-PowerShell-GET which uses JSON content.

I also think that JSON always starts and ends with "{" or "}" so you might need to add that to your $data string. Also since you are using PowerShell, you can use ConvertTo-JSON to convert PSObjects to JSON if that makes it easier to build and modify.

 

Edited by Mike Moniz

Share this post


Link to post
Share on other sites
  • 0
3 minutes ago, Mike Moniz said:

The multipart boundary stuff is more related to sending large amount of data like XML file contents while most API calls including widget updates just need to pass direct JSON content. You might want to look at the examples at https://www.logicmonitor.com/support/rest-api-developers-guide/v1/rest-api-v1-examples/#Example-PowerShell-GET which uses JSON content.

I also think that JSON always starts and ends with "{" or "}" so you might need to add that to your $data string. Also since you are using PowerShell, you can use ConvertTo-JSON to convert PSObjects to JSON if that makes it easier to build and modify.

 

Hi Mike,

Thanks for the response. My inhouse json guru advised me of the braces before and after my data. Was half way through adding an update when you responded.

The Content-Type was wrong, so you also depicted, so changing this to application/json fixed that error also.

I was also missing specific dashboard/widget information at the start of my json data ("type":"deviceNOC","dashboardId":"##","name":"##").

Ran into an issue now where updating the widget actually requires me to post the actual widget config each time instead of being able to just add a NOC item.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.