Last active
April 4, 2025 13:52
-
-
Save henkmeulekamp/ab2cf1d6e9505f66376ac85affa018d1 to your computer and use it in GitHub Desktop.
Powershell Cleanup Old NewRelic Apps
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# walk through all applications and delete the ones that are not reporting, ask per app | |
# | |
# Create this file .credential-config with | |
#{ | |
# "newrelictoken": "your-newrelic-api-key" | |
#} | |
# if in repo, make sure .credential-config is in .gitignore | |
$configFile = ".\.credential-config" | |
$newrelicUrlApplications = "https://api.newrelic.com/v2/applications.json" | |
function LoadConfig { | |
if (Test-Path $configFile) { | |
try { | |
return Get-Content $configFile -Raw -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | ConvertFrom-Json -ErrorAction:SilentlyContinue -WarningAction:SilentlyContinue | |
} catch { | |
Write-Host -ForegroundColor Red "The Base configuration file is missing!" | |
return @{ } | |
} | |
} | |
else { | |
Write-Host -ForegroundColor Red "The Base configuration file is missing! $configFile" | |
return @{ } | |
} | |
} | |
$config = LoadConfig | |
$newrelictoken = $config.newrelictoken | |
$applicationsResponse = Invoke-RestMethod -Uri $newrelicUrlApplications ` | |
-Headers @{"API-KEY" = $newrelictoken} -Method GET | |
Write-Host "Found $($applicationsResponse.applications.Count) apps" | |
if ($applicationsResponse.applications.Count -eq 0){ | |
break | |
} | |
foreach($app in $applicationsResponse.applications) { | |
if($app.reporting -eq $true){ | |
Write-Host " - skipping $($app.name) - $($app.id) - $($app.language) - $($app.health_status) - $($app.reporting) -> reporting ok" | |
continue | |
} | |
Write-Host " - deleting $($app.name) - $($app.id) - $($app.language) - $($app.health_status) - reporting=$($app.reporting) " | |
Read-Host -Prompt "Press Enter to continue or Ctrl-C to cancel" -ErrorAction SilentlyContinue | |
Invoke-RestMethod -Uri $newrelicUrlApplications.Replace("applications.json", "applications/$($app.id).json") ` | |
-Headers @{"API-KEY" = $newrelictoken } -Method Delete | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment