forked from Azure/Mission-Critical-Online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClean-StaleResources.ps1
60 lines (48 loc) · 2.29 KB
/
Clean-StaleResources.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#
# Cleans up stale resources for a give deployment (i.e. resource prefix)
# These usually occur when a deployment is manually deleted and not through the proper CI/CD pipeline
#
# Requires Azure CLI being installed and authenticated
#
function Remove-SavedLogAnalyticsQueries {
[CmdletBinding()] # indicate that this is advanced function (with additional params automatically added)
param (
$resourcePrefix = "ace2e122e"
)
Write-Host "Using Azure Account:"
az account show
# List all Log Analytics Workspaces by a given Prefix tag
$laWorkspaces = az monitor log-analytics workspace list --query "[?tags.Prefix == '$resourcePrefix']" | ConvertFrom-Json
foreach($workspace in $laWorkspaces)
{
Write-Host "*** Looking for saved searches in workspace $($workspace.Name) in category 'HealthModel'"
# List all saved searches in the workspace of category "HealthModel" (those are our saved queries)
$savedSearches = az monitor log-analytics workspace saved-search list --resource-group $workspace.resourceGroup --workspace-name $workspace.name --query "value[?category=='HealthModel']" | ConvertFrom-Json
foreach($search in $savedSearches)
{
Write-Host "Deleting saved search: $($search.name)"
az monitor log-analytics workspace saved-search delete --resource-group $workspace.resourceGroup --workspace-name $workspace.name --name $search.name --yes
}
}
}
function Remove-DiagnosticSettings {
[CmdletBinding()] # indicate that this is advanced function (with additional params automatically added)
param (
$resourcePrefix = "ace2e122e"
)
Write-Host "Using Azure Account:"
az account show
# List all resources for a given Prefix tag
$allResources = az resource list --tag Prefix=$resourcePrefix | ConvertFrom-Json
foreach($resource in $allResources)
{
Write-Host "*** Looking for diagnostic settings in resource $($resource.Name)"
# List all diagnostic settings for a given resource
$diagnosticSettings = $(az monitor diagnostic-settings list --resource $resource.Id | ConvertFrom-Json).value
foreach($setting in $diagnosticSettings)
{
Write-Host "Deleting diagnostic setting: $($setting.name)"
az monitor diagnostic-settings delete --resource $resource.Id --name $setting.name
}
}
}