Categories: PowerShellScripts

Powershell: Connect to LogicMonitor’s REST API

It’s been a while since I’ve posted. Way too long. I’ve had this script for quite a while that I wanted to share with the world. LogicMonitor is releasing a new REST API which requires some session based login. This script helps you obtain that session and download the audit log for the last hour. You’ll have to modify your timezone settings (in the AddHours lines, currently set for EST).

Here’s the script:

$user = "username"
$pass= "P@ssw0rd"

#get epoch time for current and x hours before
$date1 = Get-Date -Date "01/01/1970"
#get start time
$date2 = (Get-Date).AddHours(4)
$epochStart= (New-TimeSpan -Start $date1 -End $date2).TotalSeconds
#get end time
$date2 = (Get-Date).AddHours(5)
$epochEnd= (New-TimeSpan -Start $date1 -End $date2).TotalSeconds
#round the time to not have decimals
$epochStart= [math]::Round($epochStart)
$epochEnd= [math]::Round($epochEnd)

$filter = "_all~update" #check LM documentation on filters
$fields = "username,happenedOnLocal,description"
#build uri for access logs
$uri = "https://{account}.logicmonitor.com/santaba/rest/setting/accesslogs?sort=-happenedOn&filter=$filter,happenedOn>:$epochStart&fields=$fields"
#build base64Auth for the header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$pass)))
#get the events
$events = Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri $uri
$events #display events that were gathered
TomLasswell

Share
Published by
TomLasswell

Recent Posts

Autotask: PowerShell: Enable Client Portal for all users

This is a quick one, it's been forever since I've posted here. After moving back…

2 years ago

PowerShell :: Get Exchange Mailboxes Over XXGB

Simple command turned crazy. I ended up coming up with this due to the fact…

3 years ago

PowerShell: ConnectWise Documents API, Uploading a document or attachment to a ticket

Phew, this one took a minute to figure out. ConnectWise has a form based documents…

5 years ago

PowerShell: ConnectWise REST API Query Contacts by Email Address

I've found myself at a new job, recreating many of the processes that I spent…

5 years ago

First post in a long time — changing hosting providers

Wow, it's been a while since I've done a real post on this site. I've…

6 years ago

Powershell: AutoTask – Get Picklist Values

When using AutoTask's API it's required to lookup a various amount of picklist values that…

9 years ago