Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.26k stars 3.86k forks source link

Get-AzDiagnosticSetting does not return the diagnostic setting of an Automation account #18334

Open sdg002 opened 2 years ago

sdg002 commented 2 years ago

Description

Steps for reproducing

image

PowerShell command to retrieve the diagnostic setting

Get-AzDiagnosticSetting -ResourceId "/subscriptions/635a2074-cc31-43ac-bebe-2bcd67e1abfe/resourceGroups/rg-demo-automation-account/providers/Microsoft.Automation/automationAccounts/sauautomationaccount001" -debug

Did I try using the Azure CLI?

Yes. It comes up empty

az monitor diagnostic-settings list --resource "/subscriptions/635a2074-cc31-43ac-bebe-2bcd67e1abfe/resourceGroups/rg-demo-automation-account/providers/Microsoft.Automation/automationAccounts/sauautomationaccount001"
{
  "value": []
}
`
``

### Issue script & Debug output

```PowerShell
Get-AzDiagnosticSetting -ResourceId "/subscriptions/635a2074-cc31-43ac-bebe-2bcd67e1abfe/resourceGroups/rg-demo-automation-account/providers/Microsoft.Automation/automationAccounts/sauautomationaccount001" -debug

WARNING: 15:10:23 - *** The namespace for output classes will be uniform for all classes in future releases to make it independent of modifications in the model classes.
DEBUG: [Common.Authentication]: Authenticating using Account: 'saurabh_dasgupta@hotmail.com', environment: 'AzureCloud', tenant: 'e66fc46b-2666-4ea3-984d-bb30c06f2c75'
DEBUG: 15:10:23 - [SilentAuthenticator] Calling SharedTokenCacheCredential.GetTokenAsync - TenantId:'e66fc46b-2666-4ea3-984d-bb30c06f2c75', Scopes:'https://management.core.windows.net//.default', AuthorityHost:'https://login.microsoftonline.com/', UserId:'saurabh_dasgupta@hotmail.com'
DEBUG: SharedTokenCacheCredential.GetToken invoked. Scopes: [ https://management.core.windows.net//.default ] ParentRequestId:
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Azure region was not configured or could not be discovered. Not using a regional authority.
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23] Found 1 cache accounts and 0 broker accounts
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23] Returning 1 accounts
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] MSAL MSAL.NetCore with assembly version '4.30.1.0'. CorrelationId(7dbe9031-3091-425c-9a6b-82f9b5b0b706)
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] === AcquireTokenSilent Parameters ===
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] LoginHint provided: False
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Account provided: True
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] ForceRefresh: False
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ]
=== Request Data ===
Authority Provided? - True
Scopes - https://management.core.windows.net//.default
Extra Query Params Keys (space separated) -
ApiId - AcquireTokenSilent
IsConfidentialClient - False
SendX5C - False
LoginHint ? False
IsBrokerConfigured - False
HomeAccountId - False
CorrelationId - 7dbe9031-3091-425c-9a6b-82f9b5b0b706

DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] === Token Acquisition (SilentRequest) started:

        Authority Host: login.microsoftonline.com
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Azure region was not configured or could not be discovered. Not using a regional authority.
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Access token is not expired. Returning the found cache entry. [Current time (05/29/2022 14:10:23) - Expiration Time (05/29/2022 14:55:15 +00:00) - Extended Expiration Time (05/29/2022 14:55:15 +00:00)]
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Returning access token found in cache. RefreshOn exists ? False
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] Fetched access token from host login.microsoftonline.com.
DEBUG: (False) MSAL 4.30.1.0 MSAL.NetCore Microsoft Windows 10.0.19044 [05/29/2022 14:10:23 - ] === Token Acquisition finished successfully. An access token was returned with Expiration Time: 05/29/2022 14:55:15 +00:00 and Scopes https://management.core.windows.net//user_impersonation https://management.core.windows.net//.default
DEBUG: SharedTokenCacheCredential.GetToken succeeded. Scopes: [ https://management.core.windows.net//.default ] ParentRequestId:  ExpiresOn: 2022-05-29T14:55:15.0000000+00:00
DEBUG: [Common.Authentication]: Received token with LoginType 'User', Tenant: 'e66fc46b-2666-4ea3-984d-bb30c06f2c75', UserId: 'saurabh_dasgupta@hotmail.com'
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method:
GET

Absolute Uri:
https://management.azure.com//subscriptions/635a2074-cc31-43ac-bebe-2bcd67e1abfe/resourceGroups/rg-demo-automation-account/providers/Microsoft.Automation/automationAccounts/sauautomationaccount001/providers/Microsoft.Insights/diagnosticSettings?api-version=2017-05-01-preview

Headers:
x-ms-client-request-id        : 70c82386-915b-4cd0-b4c6-f428bdbe0349
Accept-Language               : en-US

Body:

DEBUG: ============================ HTTP RESPONSE ============================

Status Code:
OK

Headers:
Cache-Control                 : no-cache
Pragma                        : no-cache
Strict-Transport-Security     : max-age=31536000; includeSubDomains
x-ms-request-id               : 3130c77f-2293-459a-95ef-2ff0cefd65cd
Server                        : Microsoft-IIS/10.0
x-ms-ratelimit-remaining-subscription-reads: 11999
x-ms-correlation-request-id   : e5e017b3-8857-4adf-8f9d-e0e9f1224b5d
x-ms-routing-request-id       : UKSOUTH:20220529T141023Z:e5e017b3-8857-4adf-8f9d-e0e9f1224b5d
X-Content-Type-Options        : nosniff
Date                          : Sun, 29 May 2022 14:10:22 GMT

Body:
{
  "value": []
}

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.3
PSEdition                      Core
GitCommitId                    7.1.3
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.7.5                 Az.Accounts                         {Add-AzEnvironment, Clear-AzContext, Clear-AzDefault, Connect-AzAccount…}
Script     3.0.1                 Az.Monitor                          {Add-AzAutoscaleSetting, Add-AzLogProfile, Add-AzMetricAlertRule, Add-AzMetricAlertRuleV2…}
Script     5.3.0                 Az.Resources                        {Export-AzResourceGroup, Export-AzTemplateSpec, Get-AzDenyAssignment, Get-AzDeployment…}

Error output

WARNING: Upcoming breaking changes in the cmdlet 'Resolve-AzError' :
The `Resolve-Error` alias will be removed in a future release.  Please change any scripts that use this alias to use `Resolve-AzError` instead.
Note : Go to https://aka.ms/azps-changewarnings for steps to suppress this breaking change warning, and other information on breaking changes in Azure PowerShell.

   HistoryId: 9

Message        : Exception type: ErrorResponseException, Message: Microsoft.Azure.Management.Monitor.Models.ErrorResponseException: Operation returned an invalid status code 'NotFound'
                    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperations.GetWithHttpMessagesAsync(String resourceUri, String name, Dictionary`2 customHeaders, CancellationToken cancellationToken)
                    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperationsExtensions.GetAsync(IDiagnosticSettingsOperations operations, String resourceUri, String name, CancellationToken
                 cancellationToken)
                    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperationsExtensions.Get(IDiagnosticSettingsOperations operations, String resourceUri, String name)
                    at Microsoft.Azure.Commands.Insights.Diagnostics.GetAzureRmDiagnosticSettingCommand.ProcessRecordInternal()
                    at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet(), Code: Null, Status code:Null, Reason phrase: Null
StackTrace     :    at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet()
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.<>c__3`1.<ExecuteSynchronouslyOrAsJob>b__3_0(T c)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet, Action`1 executor)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.CmdletExtensions.ExecuteSynchronouslyOrAsJob[T](T cmdlet)
                    at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord()
Exception      : System.Management.Automation.PSInvalidOperationException
InvocationInfo : {Get-AzDiagnosticSetting}
Line           : Get-AzDiagnosticSetting -ResourceId $resource.id -Name my-diganostic-001 -debug
Position       : At line:1 char:1
                 + Get-AzDiagnosticSetting -ResourceId $resource.id -Name my-diganostic- …
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 9

Message        : Operation returned an invalid status code 'NotFound'
StackTrace     :    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperations.GetWithHttpMessagesAsync(String resourceUri, String name, Dictionary`2 customHeaders, CancellationToken cancellationToken)
                    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperationsExtensions.GetAsync(IDiagnosticSettingsOperations operations, String resourceUri, String name, CancellationToken
                 cancellationToken)
                    at Microsoft.Azure.Management.Monitor.DiagnosticSettingsOperationsExtensions.Get(IDiagnosticSettingsOperations operations, String resourceUri, String name)
                    at Microsoft.Azure.Commands.Insights.Diagnostics.GetAzureRmDiagnosticSettingCommand.ProcessRecordInternal()
                    at Microsoft.Azure.Commands.Insights.MonitorCmdletBase.ExecuteCmdlet()
Exception      : Microsoft.Azure.Management.Monitor.Models.ErrorResponseException
InvocationInfo : {Get-AzDiagnosticSetting}
Line           : Get-AzDiagnosticSetting -ResourceId $resource.id -Name my-diganostic-001 -debug
Position       : At line:1 char:1
                 + Get-AzDiagnosticSetting -ResourceId $resource.id -Name my-diganostic- …
                 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 9
dingmeng-xue commented 2 years ago

Thanks for reporting. This is due to API version of DiagnosticSetting is too old. We plan to upgrade its API version in upcoming month.

sdg002 commented 2 years ago

@dingmeng-xue Thanks for looking into this. In the mean time, does MSFT have an alternative approach to set/query the Diagnostic information programmatically (IAC) ?

dingmeng-xue commented 2 years ago

Please use below code and send request with specific API version directly

Invoke-AzRestMethod https://management.azure.com/subscriptions/635a2074-cc31-43ac-bebe-2bcd67e1abfe/resourceGroups/rg-demo-automation-account/providers/Microsoft.Automation/automationAccounts/sauautomationaccount001/providers/Microsoft.Insights/diagnosticSettings?api-version=2021-05-01-preview
Kaloszer commented 2 years ago

@dingmeng-xue

Thanks for reporting. This is due to API version of DiagnosticSetting is too old. We plan to upgrade its API version in upcoming month.

That month can't come soon enough 🤣, still facing this issue. Any timeframe for a fix?

x-delfino commented 2 years ago

@Kaloszer - looks like this was fixed in v9.0.1.

[Breaking Change] Upgraded API version for DiagnosticSetting from 2017-05-01-preview to 2021-05-01-preview

Kaloszer commented 2 years ago

Cool! I don't think it's implemented in the current version of ADO yaml inline powershell core pipelines then yet as I was looking at the same issue on Friday, just went with the azrestmethod method to work around the issue.

x-delfino commented 2 years ago

I think the recommended method for Azure PowerShell on ADO pipelines is AzurePowerShell@5 which has the preferredAzurePowerShellVersion property, but for some reason, docs list that as only supporting 1.0.0, 1.6.0, 2.3.2, 2.6.0, and 3.1.0 with the latest being released in 26/11/2019 . So either docs or module support are v outdated. ref: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/azure-powershell-v5?view=azure-pipelines

Kaloszer commented 2 years ago

I think the recommended method for Azure PowerShell on ADO pipelines is AzurePowerShell@5 which has the preferredAzurePowerShellVersion property, but for some reason, docs list that as only supporting 1.0.0, 1.6.0, 2.3.2, 2.6.0, and 3.1.0 with the latest being released in 26/11/2019 . So either docs or module support are v outdated. ref: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/azure-powershell-v5?view=azure-pipelines

It's actually just outdated, I'm using azurePowerShellVersion: LatestVersion

In the pipeline so...