Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Created June 17, 2020 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/a35cff1be8c3989ab2ec0a1ba8e0e0c4 to your computer and use it in GitHub Desktop.
Save chrismckelt/a35cff1be8c3989ab2ec0a1ba8e0e0c4 to your computer and use it in GitHub Desktop.
Turn on log analytics diagnostics settings for specified resource type
# Turn on log analytics diagnostics settings for specified resource type
$name = 'your subscription name goes here'
$subscription = Get-AzSubscription -SubscriptionName $name
Select-AzSubscription -SubscriptionName $name
$log_analytics_resource_group = 'log analytics resource group name'
$target_resource_group = 'resource group you want to setup'
$workspaces = Get-AzOperationalInsightsWorkspace -ResourceGroupName $log_analytics_resource_group
if (@($workspaces).Length -eq 0)
{
throw "No log analytics workspace in resource group: $log_analytics_resource_group"
}
else
{
$workspacesResourceId = $workspaces[0].ResourceId
}
# resource types to add diagnostic settings flow to log analytics
$resourceTypes = @(
"Microsoft.Logic/workflows",
"Microsoft.ApiManagement/service",
"Microsoft.KeyVault/vaults",
"Microsoft.ServiceBus/namespaces",
"Microsoft.Web/sites",
"Microsoft.Web/serverfarms",
"Microsoft.Sql/servers/databases",
"Microsoft.Network/applicationGateways",
"Microsoft.Network/networkSecurityGroups",
"Microsoft.ContainerService/managedClusters",
"Microsoft.EventGrid/topics",
"Microsoft.ContainerService/managedClusters/agentPools",
"Microsoft.EventGrid/systemTopics",
"Microsoft.Cache/Redis",
"Microsoft.Sql/servers/databases"
)
$rg = Get-AzResourceGroup -Name $target_resource_group
if($null -ne $rg){
foreach ($resourceType in $resourceTypes)
{
# $resourceType = 'Microsoft.Logic/workflows';
# https://www.codeisahighway.com/how-to-safely-replace-find-Azresource-resourcetype-calls-in-azure-powershell-6-x/
Write-Host "Lookup for $($resourceType) in $($rg.ResourceGroupName) ..."
$rgname = $rg.ResourceGroupName
$resources = Get-AzResource -ODataQuery "`$filter=resourcetype eq '$resourceType' and resourcegroup eq '$rgname'" -WarningAction Ignore
Write-Host $resources
foreach ($resource in $resources)
{
# $resource = $resources[0]
Write-Host "Check Diagnostic Settings for $($resource.Name)..." -NoNewline
$setting = Get-AzDiagnosticSetting -ResourceId $resource.ResourceId -WarningAction Ignore -ErrorAction Ignore
if(($null -eq $setting) -or $setting.WorkspaceId -eq '' -or $setting.WorkspaceId -ne $workspacesResourceId -or @($setting.Metrics).Length -eq 0 -or (-not $setting.Metrics[0].Enabled))
{
Write-Host
Write-Host "Set Diagnostic Settings for $($resource.Name)..." -NoNewline
try {
$setting = Set-AzDiagnosticSetting -ResourceId $resource.ResourceId -Enabled $True -WorkspaceId $workspacesResourceId -WarningAction Continue -ErrorAction Continue
Write-Host "done!" -ForegroundColor DarkGreen
}
catch {
Write-Host
Write-Warning "FAILED to add diagnostic for $($resource.Name) of type $($resourceType) in $($rg.ResourceGroupName)"
}
}
else
{
Write-Host "enabled!" -ForegroundColor DarkGreen
}
}
}
Write-Host "Finished Lookup Resource Group $($rg.ResourceGroupName)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment