Skip to content

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
Terminal window
# Example of using -Debug with Get-LMDevice
Get-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-).

Terminal window
# See what devices would be removed without actually removing them
Get-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 changes
Set-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:

Terminal window
# Using try/catch blocks
try {
New-LMDevice -Name "test-server" -DisplayName "Test Server" -ErrorAction Stop
}
catch {
Write-Error "Failed to create device: $_"
}
# Using -ErrorAction
Get-LMDevice -Name "non-existent" -ErrorAction SilentlyContinue
if (!$?) {
Write-Warning "Device not found"
}

Best Practices

  1. Always Test First

    Terminal window
    # Use -WhatIf before making bulk changes
    Get-LMDevice -Filter "status -eq 'dead'" | Remove-LMDevice -WhatIf
  2. Debug API Issues

    Terminal window
    # Enable debug output for troubleshooting
    $DebugPreference = "Continue"
    Get-LMDevice -Name "problem-device"
    $DebugPreference = "SilentlyContinue" # Reset when done
  3. 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.