Skip to content

Set-AzDiagnosticSetting - impossible to set RentionInDays for individual categories #11589

@AdrianSanguineti

Description

@AdrianSanguineti

Description

I'm trying to set diagnostic settings via the Set-AzDiagnosticSetting commandlet for an Azure Front Door resource.

I've tried a number a ways, but I have not been able to set a separate Retention policy per category. I can easily do this via the Azure portal, but I want to automate it with PowerShell. No matter how I call/chain the calls to Set-AzDiagnosticSetting command together, the last value given for RetentionInDays is applied to all Categories.

Steps to reproduce

The following are the values I am trying to set. Metrics should be stored for 180 days, the logs indefinitely.

$diagnosticMetricSettings = @{
  Name = 'FrontDoor-Diagnostics'
  ResourceId = '<enter front door resource id>'
  StorageAccountId = '<enter storage account resource id>'
  Enabled = $true
  MetricCategory = @('AllMetrics')
  RetentionInDays = 180
  RetentionEnabled = $true
}

$diagnosticLogSettings = @{
  Name = 'FrontDoor-Diagnostics'
  ResourceId = '<enter front door resource id>'
  StorageAccountId = '<enter storage account resource id>'
  Enabled = $true
  Category = @('FrontDoorAccessLog', 'FrontdoorWebApplicationFireallLog')
  RetentionInDays = 0
  RetentionEnabled = $true
}

Executing Set-AzDiagnosticSetting with these parameters in the following order:

Set-AzDiagnosticSetting @diagnosticMetricSettings 
Set-AzDiagnosticSetting @diagnosticLogSettings 

Results in all categories and metrics having a retention policy of 0 days

Executing Set-AzDiagnosticSetting with these parameters in the following order:

Set-AzDiagnosticSetting @diagnosticLogSettings
Set-AzDiagnosticSetting @diagnosticMetricSettings 

Results in all categories and metrics having a retention policy of 180 days

Piping the commands together in any order results in the same behaviour.

I've taken a quick look at the source for Set-AzDiagnosticSetting and it looks like the SetRetention function is executed against ALL categories enabled on the resource, instead of just the categories that are passed in to the commandlet. This would confirm the behaviour that I'm experiencing.

Environment data

(Sorry hand to write this out by hand - unable to cut'n paste from target environment to GitHub)

Name                                               Value
-----                                                 -----
PSVersion                                         5.1.17134.858
PSEdition                                          Desktop
PSCompatibleVersions                     {1.0, 2.0, 3.0, 4.0...}
BuildVersion                                     10.0.17134.858
CLRVersion                                       4.0.30319.42000
WSManStackVersion                        3.0
PSRemotingProtocolVersion            2.3
SerializationVersion                          1.1.0.1

Module versions

Sorry unable provide this as unable to cut'n paste from target environment to GitHub.

Debug output

Sorry unable provide this as unable to cut'n paste from target environment to GitHub.

Error output

There's no errors executing the command.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions