Debugging and Testing
The Logic.Monitor PowerShell module includes several built-in features to help you debug and test commands before executing them. This guide covers the main debugging tools available.
Using -Debug Parameter
The -Debug
parameter allows you to see detailed information about API requests, including:
- Request URLs
- Headers
- Request bodies
- Response data
# Example of using -Debug with Get-LMDeviceGet-LMDevice -Name "prod-web01" -Debug
# Output will show:# DEBUG: Request URL: https://company.logicmonitor.com/santaba/rest/device/devices...# DEBUG: Request Headers: {...}# DEBUG: Request Method: GET# DEBUG: Response Status: 200
This is particularly useful when:
- Troubleshooting API errors
- Understanding what data is being sent/received
- Developing new scripts
- Validating request formatting
Using -WhatIf Parameter
The -WhatIf
parameter shows what would happen if you ran the command without actually executing it. This is available on all commands that modify resources (New-, Set-, Remove-).
# See what devices would be removed without actually removing themGet-LMDevice -Filter "name -contains 'test'" | Remove-LMDevice -WhatIf
# Output:# What if: Would remove device 'test-server-01' (ID: 123)# What if: Would remove device 'test-server-02' (ID: 124)
# Preview property changesSet-LMDevice -Id 123 -Properties @{ "environment" = "production" "team" = "platform"} -WhatIf
# Output:# What if: Would update properties for device 'prod-web01' (ID: 123)
This is especially useful for:
- Validating changes before executing them
- Testing filters to ensure they target the correct resources
- Documenting planned changes
- Training and learning new commands
Error Handling Examples
The module includes built-in error handling. Here’s how to properly handle errors in your scripts:
# Using try/catch blockstry { New-LMDevice -Name "test-server" -DisplayName "Test Server" -ErrorAction Stop}catch { Write-Error "Failed to create device: $_"}
# Using -ErrorActionGet-LMDevice -Name "non-existent" -ErrorAction SilentlyContinueif (!$?) { Write-Warning "Device not found"}
Best Practices
-
Always Test First
Terminal window # Use -WhatIf before making bulk changesGet-LMDevice -Filter "status -eq 'dead'" | Remove-LMDevice -WhatIf -
Debug API Issues
Terminal window # Enable debug output for troubleshooting$DebugPreference = "Continue"Get-LMDevice -Name "problem-device"$DebugPreference = "SilentlyContinue" # Reset when done -
Validate Filters
Terminal window # Test filters before using in bulk operations$filter = "displayName -contains 'prod'"Get-LMDevice -Filter $filter | Select-Object id, name, displayName
These debugging and testing features help ensure your scripts work as intended before making changes to your LogicMonitor environment.