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 provides detailed information about API requests to help troubleshoot issues.

What Debug Shows

Request Details

  • HTTP method (GET, POST, PATCH, DELETE)
  • Complete endpoint URL
  • Query parameters
  • Portal information

Headers

  • Authentication type
  • Content-Type
  • API version
  • User-Agent (partially redacted)

Payload

  • Complete JSON request body
  • Formatted for readability
  • Sensitive fields redacted
  • Nested object structure

Debug Output Example

Terminal window
# Debug a GET request
Get-LMDevice -Name "prod-web01" -Debug
# Output:
# DEBUG: ============ LogicMonitor API Debug Info ==============
# DEBUG: Command: Get-LMDevice | Method: GET | Timestamp: 2024-01-15 10:30:45.123
# DEBUG: Parameters: Name: prod-web01
# DEBUG: Endpoint: /device/devices?filter=name:"prod-web01"
# DEBUG: Portal: company.logicmonitor.com
# DEBUG: Headers: Authorization: LMv1... | Content-Type: application/json | X-Version: 3
# DEBUG: Request Payload: None (GET request)
# DEBUG: ========================================================

When to Use Debug

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"
}

Advanced Debugging Techniques

Enable Debug for Multiple Commands

Terminal window
# Set debug preference for entire session
$DebugPreference = "Continue"
# All commands will now show debug output
Get-LMDevice -Name "server01"
Set-LMDevice -Id 123 -Properties @{test="value"}
# Reset when done
$DebugPreference = "SilentlyContinue"

Debug Custom API Requests

Terminal window
# Debug shows complete request structure
Invoke-LMAPIRequest `
-ResourcePath "/device/devices" `
-Method POST `
-Data @{
name = "test-server"
displayName = "Test"
preferredCollectorId = 5
} `
-Debug

Learning Payload Formats

Troubleshooting Common Issues

Authentication Errors

Terminal window
# Check if you're connected
if (-not $Script:LMAuth.Valid) {
Write-Warning "Not connected to LogicMonitor"
Connect-LMAccount -AccessId $id -AccessKey $key -AccountName "portal"
}
# Verify portal connection
$Script:LMAuth
# Debug authentication issues
Connect-LMAccount -AccessId $id -AccessKey $key -AccountName "portal" -Debug

Best Practices

✅ DO

  • Use -WhatIf before bulk operations
  • Enable -Debug when troubleshooting
  • Test filters on small datasets first
  • Add delays in bulk operations
  • Capture debug output when reporting issues

❌ DON'T

  • Don’t skip -WhatIf for destructive operations
  • Don’t leave debug enabled in production scripts
  • Don’t ignore verbose error messages
  • Don’t run untested filters at scale

Testing Workflow

Terminal window
# 1. Test filter first
$filter = "status -eq 'dead'"
Get-LMDevice -Filter $filter | Select-Object id, name | Format-Table
# 2. Preview changes with -WhatIf
Get-LMDevice -Filter $filter | Remove-LMDevice -WhatIf
# 3. Test on single item with -Debug
Get-LMDevice -Filter $filter | Select-Object -First 1 | Remove-LMDevice -Debug
# 4. If all looks good, proceed with actual operation
Get-LMDevice -Filter $filter | Remove-LMDevice -Confirm:$false