stevevillardi / Logic.Monitor

LogicMonitor PowerShell Module for automating LM Portal tasks using APIv3
https://www.powershellgallery.com/packages/Logic.Monitor
Mozilla Public License 2.0
23 stars 3 forks source link

Custom Properties and checking for updates #7

Closed jmossuk closed 2 years ago

jmossuk commented 2 years ago

Hi Steve,

I trying to write a Powershell script to check for a list of custom properties against a device in LogicMonitor and then to check the value of that property and if it has changed to just update that property, do you happen to have an easy way to do that.

I'm trying to avoid having to do a request for each device property just to get the status or to see if that property exists as this will cause alot of requests in a very short period of time and I'm already coming up against the 200 limit within 60 seconds.

Do you happen to know if there is a way to query and retrieve a list of all the custom properties in one go and then to be able to just update the ones which have changed or are missing.

Many thanks

Jamie

stevevillardi commented 2 years ago

Instead of using get-lmdeviceproperty which retrieves a specific property I would just use get-lmdevice which includes a customproperties object in the response which is all of the applied custom properties for that device. There are also systemproperties and inheritedproperties objects returned as well depending on what type of properties you are validating

stevevillardi commented 2 years ago

You can do the same with Set-LMDevice using the -Properties parameter which is a hash table of properties to create or update for the specified device.

stevevillardi commented 2 years ago

As an example you can do something like this using get-lmdevicegroupdevices to return all devices in a specific device group along with their assigned properties, then just loop through and change/add the prop if needed:

example code test before executing

#Array to store returned objects
$processedDeviceList = @()

#Get list of devices in a device group for device group id 23
$devices = Get-LMDeviceGroupDevices -Id "23"

#Loop through each device and check for presence of property
foreach ($dev in $devices) {
    $propName = "test.prop"
    $propValue = "1234"
    $currentPropValue = ($dev.customProperties[$dev.customProperties.name.IndexOf($propName)].value)
    if($currentPropValue -and $dev.customProperties.name.IndexOf($propName) -ne -1){

        If($currentPropValue -ne $propValue){
            #Update the property value since it does not match desired value
            $processedDeviceList += Set-LMDevice -Id $dev.id -Properties @{$propName=$propValue}
            Write-Host "Successfully processed $($dev.displayName), updated property $propName value from $currentPropValue -> $propValue"
        }
        Else{
            #Skip device since it already has desired value:
            Write-Host "Skipped processing $($dev.displayName) since property $propName is already present with the desired value $propValue"
        }
    }
    else{
        #Add property and value since it does not exist on device currently
        $processedDeviceList += Set-LMDevice -Id $dev.id -Properties @{$propName=$propValue}
        Write-Host "Successfully processed $($dev.displayName), added property $propName with value $propValue"
    }
}

#Print out results
$processedDeviceList | Select-Object id,displayname,customproperties | Format-List