Blog Post

Tech Talk
3 MIN READ

Logic.Monitor (PowerShell) module

Steve_Villardi's avatar
Steve_Villardi
Icon for LM Champion rankLM Champion
2 years ago

If you're a LogicMonitor user looking to streamline your workflows and automate repetitive tasks, you'll be pleased to know that there's is a PowerShell module available to help you do just that. As a longtime Windows administrator, I've relied on PowerShell as my go-to tool for automating and managing my infrastructure. I've found that the ability to automate tasks through PowerShell not only saves time, but also reduces errors and ensures consistency across the environment. Developed by myself as a personal side project, this module provides a range of cmdlets that can be used to interact with the LogicMonitor API, making it easier than ever to manage your monitoring setup directly from the command line. Whether you're looking to retrieve information about your monitored devices, update alert thresholds, or perform other administrative tasks, this module has you covered. In this post, we'll take a closer look at the features and capabilities of this module, and show you how to get started with using it in your own automation scripts.

This project is published in the PowerShell Gallery at https://www.powershellgallery.com/packages/Logic.Monitor/.

Installation From PowerShell Gallery:

Install-Module -Name "Logic.Monitor"

Upgrading:

#New releases are published often, to ensure you have the latest version you can run:
Update-Module -Name "Logic.Monitor"


General Usage:

Before you can use on module commands you will need to be connected to a LM portal. To connect your LM portal use the Connect-LMAccount command:

Connect-LMAccount -AccessId "lm_access_id" -AccessKey "lm_access_key" -AccountName "lm_portal_prefix_name"

Once connected you can then run an appropriate command, a full list of commands available can be found using:

Get-Command -Module "Logic.Monitor"

To disconnect from an account simply run the Disconnect-LMAccount command:

Disconnect-LMAccount

Examples:

Most Get commands can pull info by id or name to allow for easier retrieval without needing to know the specific resource id. The name parameters in get commands can also accept wildcard values. 

Get list of devices:

#Get all devices
Get-LMDevice

#Get device via id
Get-LMDevice -Id 1

#Get device via hostname
Get-LMDevice -Name device.example.com

#Get device via displayname/wildcard
Get-LMDevice -DisplayName "corp*"

Modify a device:

#Change device Name,DisplayName,Descrition,Link and set collector assignment
Set-LMDevice -Id 1 -DisplayName "New Device Name" -NewName "device.example.com" -Description "Critical Device" -Link "http://device.example.com" -PreferredCollectorId 1

#Add/Update custom properties to a resource and disable alerting
Set-LMDevice -Id 1 -Properties @{propname1="value1";propname2="value2"} -DisableAlerting $true

***Using the Name parameter to target a resource during a Set/Remove command will perform an initial get request for you automatically to retrieve the required id. When performing a large amount of changes using id is the preferred method to avoid excessive lookups and avoid any potential API throttling.

Remove a device:

#Remove device by hostname
Remove-LMDevice -Name "device.example.com" -HardDelete $false

Send a LM Log Message:

Send-LMLogMessage -Message "Hello World!" -resourceMapping @{"system.displayname"="LM-COLL"} -Metadata @{"extra-data"="value";"extra-data2"="value2"}

Add a new user to LogicMonitor:

New-LMUser -RoleNames @("administrator") -Password "changeme" -FirstName John -LastName Doe -Email jdoe@example.com -Username jdoe@example.com -ForcePasswordChange $true -Phone "5558675309"

There are over ~150 cmdlets exposed as part of this module and more are being added each week as I receive feedback internally and from customers. For more details and other examples/code snippets or to contribute you can visit the github repo where this is hosted.

Source Repository: https://github.com/stevevillardi/Logic.Monitor
Additional Code Examples: https://github.com/stevevillardi/Logic.Monitor/blob/main/EXAMPLES.md

Note: This is very much a personal project and not an official LogicMonitor integration. If the concept of a native PowerShell module interest you, I would recommend putting in a feedback request so that the demand can be tracked.

Published 2 years ago
Version 1.0

28 Comments

  • Hi Steve, is there a way to update the static threshold at the resource/instance level using one of the already available modules?

    Cheers 

    I don’t believe there currently is any cmdlet for threshold modification outside of the datasource global definition. If that is something that would be helpful to have I can certainly look to add it in the next update. If you want to open it as an issue on github as an enhancement I can notify you when it gets put in the next release.

  • I didn’t even have time to write up an issue!

    Thanks Steve. I will test this out tomorrow!

  • Hi,

    I am stringing together a few of your commands to setup Collectors, Resources, Dashboards etc so things are setup consistently.

    Do you have any plans for: “Creating ReportGroup”? (Would be pretty similar to: New-LMDashboardGroup)

    I see Get-LMReportGroup.ps1 but not a new-LMReportGroup yet.

  • Hi,

    I am stringing together a few of your commands to setup Collectors, Resources, Dashboards etc so things are setup consistently.

    Do you have any plans for: “Creating ReportGroup”? (Would be pretty similar to: New-LMDashboardGroup)

    I see Get-LMReportGroup.ps1 but not a new-LMReportGroup yet.

    @ryang I can certainly add it into the next update. If there are other commands that would be of use please feel free to open an enhancement issue on the github repo.

  • @Steve Villardi 

    I was looking at the get-lmalert one and trying to use the customColumns part to get the ##externalticketid## field back.  

    I tried a few different ways to encode it in PowerShell but it did not seem to work properly or I am just doing it wrong.

    Have you done it before to get the ticket id back since that is the field where things like a ServiceNow incident # would appear?

  • @Steve Villardi

    I was looking at the get-lmalert one and trying to use the customColumns part to get the ##externalticketid## field back.  

    I tried a few different ways to encode it in PowerShell but it did not seem to work properly or I am just doing it wrong.

    Have you done it before to get the ticket id back since that is the field where things like a ServiceNow incident # would appear?

    @Jeff Batchelor Should just able able to specify it in double quotes: “##externalticketid##”. How are you building your command. Below is an example for reference:

    Get-LMAlert -Id DS15713155 -CustomColumns "##EXTERNALTICKETID##" | select -ExpandProperty customColumns


    Response:

  • Thanks for creating this.  We’ve been using it for a while and ran into a strange issue with authentication on Linux.  It works perfectly when we run from a Windows machine, but if we run from a Linux machine (pwsh), we get authentication errors even though the credentials aren’t changed.  I’m not sure if this is generally an issue with Linux PowerShell or specific to the module, but thought I’d mention in case it was perhaps a known issue.  Here is the type of error we see (the error shows on the LM logs too… but again, the auth credentials are unchanged from what works when the same script is run from a Windows box):

    Out-PsLogging: [ERROR] 2024-03-02T17:47:29: Unexpected error getting device groups. To prevent errors, Get-LogicMonitorDeviceGroup will
    exit. If present, the following details were returned:                          Error message: Authentication failed 
    Error code: 1401                         Invoke-Request: Response status code does not indicate success: 401 ().     
    Headers:  Name                           Value ----                           ----- X-Version                      3
    Content-Type                   application/json Authorization                  LMv1
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx…                           Body:

  • Thanks for creating this.  We’ve been using it for a while and ran into a strange issue with authentication on Linux.  It works perfectly when we run from a Windows machine, but if we run from a Linux machine (pwsh), we get authentication errors even though the credentials aren’t changed.  I’m not sure if this is generally an issue with Linux PowerShell or specific to the module, but thought I’d mention in case it was perhaps a known issue.  Here is the type of error we see (the error shows on the LM logs too… but again, the auth credentials are unchanged from what works when the same script is run from a Windows box):

    Out-PsLogging: [ERROR] 2024-03-02T17:47:29: Unexpected error getting device groups. To prevent errors, Get-LogicMonitorDeviceGroup will
    exit. If present, the following details were returned:                          Error message: Authentication failed 
    Error code: 1401                         Invoke-Request: Response status code does not indicate success: 401 ().     
    Headers:  Name                           Value ----                           ----- X-Version                      3
    Content-Type                   application/json Authorization                  LMv1
    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx…                           Body:

    @S P are you sure you’re talking about the Logic.Monitor ps module? The cmdlet name is not one that I recognize and the error output is different. The Logic.Monitor module is tested on 5.1 and core 7+ for both linux/win OSes so there should not be an issue specific to linux that does not effect windows users as well.