ID Lookup Functionality
The Logic.Monitor PowerShell module includes automatic ID lookup capabilities that make it easier to work with resources using names instead of IDs. While this is convenient for interactive use, there are important considerations for automation and scripting.
How ID Lookup Works
Many commands support both ID and Name parameters. When using names, the module automatically:
- Performs a GET request to find the resource ID
- Uses the retrieved ID for the actual operation
# Using name (performs automatic lookup)Remove-LMDevice -Name "web-server-01"
# Behind the scenes, this is equivalent to:$deviceId = (Get-LMDevice -Name "web-server-01").idRemove-LMDevice -Id $deviceId
Supported Parameters
Different commands support various lookup parameters:
Device Commands
Name
(system.hostname)DisplayName
(displayName)
# These all refer to the same deviceSet-LMDevice -Name "server01.domain.com" -Properties @{location = "NYC"}Set-LMDevice -DisplayName "Production Web Server" -Properties @{location = "NYC"}Set-LMDevice -Id 123 -Properties @{location = "NYC"}
Group Commands
Name
(group name)FullPath
(complete group path)
# Different ways to reference the same groupRemove-LMDeviceGroup -Name "Web Servers"Remove-LMDeviceGroup -FullPath "Production/Web Servers"Remove-LMDeviceGroup -Id 456
Best Practices
Interactive Use
When working interactively, using names is convenient:
# Good for interactive useGet-LMDevice -DisplayName "web*" | Remove-LMDevice
Automation Scripts
For automation, use IDs to avoid extra API calls and potential rate limiting:
# Better for automation - cache IDs$deviceIds = Get-LMDevice -Filter "displayName -contains 'web'" | Select-Object -ExpandProperty id
foreach ($id in $deviceIds) { Remove-LMDevice -Id $id}
Bulk Operations
When performing bulk operations, collect IDs first:
# Less efficient - performs lookup for each deviceGet-LMDevice -Filter "status -eq 'dead'" | ForEach-Object { Remove-LMDevice -Name $_.name }
# More efficient - uses cached IDs$devices = Get-LMDevice -Filter "status -eq 'dead'"$devices | Remove-LMDevice -Id { $_.id }
Rate Limiting Considerations
Each name lookup requires an additional API call, which can impact rate limits:
# May hit rate limits - performs lookup for each deviceforeach ($name in $deviceNames) { Set-LMDevice -Name $name -Properties @{updated = $true}}
# Better approach - single lookup, then use IDs$devices = Get-LMDevice -Filter "name -in $($deviceNames -join ',')"foreach ($device in $devices) { Set-LMDevice -Id $device.id -Properties @{updated = $true}}
Lookup Performance Example
Here’s a comparison of operations with and without ID caching:
# Without ID caching (10 API calls)1..10 | ForEach-Object { Set-LMDevice -Name "server-$_" -Properties @{updated = $true}}
# With ID caching (2 API calls)$devices = Get-LMDevice -Filter "name -contains 'server-'"$devices | ForEach-Object { Set-LMDevice -Id $_.id -Properties @{updated = $true}}
Error Handling
The module includes built-in error handling for lookups:
# Handles non-existent resourcestry { Remove-LMDevice -Name "non-existent-server" -ErrorAction Stop}catch { Write-Error "Device not found: $_"}
# Handle ambiguous namestry { Set-LMDevice -DisplayName "web" -Properties @{updated = $true}}catch { Write-Error "Multiple devices found matching 'web'. Please use a more specific name or ID."}
Remember:
- Use names for interactive work and one-off operations
- Use IDs for scripts and automation
- Cache IDs when performing bulk operations
- Consider rate limits when designing automation scripts