Azure / Azure/azure-powershell

Az.Monitor Cmdlets Ignoring DefaultProfile Parameter

Open
#20,457 8 comments 0 reactions 0 assignees View on GitHub
act-observability-squad customer-reported feature-request Monitor Tracking
Dominant language
C#
Stars
4.8k
Forks
4.3k
Avg merge
3d 14h
Merged PRs (30d)
54

Description

### Description

After updating the Az.Monitor module from version 3.1.0 to 4.3.0 (versions in between are broken as well), the DefaultProfile parameter is being ignored by the Get-AzActivityLogAlert and New-AzActivityLogAlert cmdlets. There may be others but those are the two I tested. Also, the AzContext alias is now missing as well.

This is causing the cmdlets to run against the wrong tenant when running multiple threads/runspaces in parallel using the Start-ThreadJob cmdlet. This was working fine in version 3.1.0 of the module.

### Issue script & Debug output

Below is the command and output within a background job started with Start-ThreadJob and using Debug-Runspace. Original tenant and subscription IDs have been replaced with random ones.

As you can see, the $context object I'm passing to the Get-AzActivityLogAlert cmdlet is for the c63c19a1-76b7-477f-90e8-a0b5418bf49d tenant ID and the 63659f70-9b68-4cef-9f9f-61a280264d26 subscription ID. However, the object returned by the Get-AzActivityLogAlert cmdlet is for a different subscription ID (a127febc-f62e-4bc6-bec1-1dec6bec9901) in a completely different tenant that's running in a different thread/runspace.

```PowerShell

PS WD:\> Debug-Runspace 6
Debugging Runspace: Runspace6
To end the debugging session type the 'Detach' command at the debugger prompt, or type 'Ctrl+C' otherwise.

At line:27 char:9
+ if (!$alert) {
+ ~~~~~~~
[DBG]: [Process:7352]: [Runspace6]: PS C:\Users\corywo>> $context.Context

Name :
Account : f388e098-255d-452d-9924-9053cced978e
Environment : AzureCloud
Subscription : 63659f70-9b68-4cef-9f9f-61a280264d26
Tenant : c63c19a1-76b7-477f-90e8-a0b5418bf49d
TokenCache :
VersionProfile :
ExtendedProperties : {}

[DBG]: [Process:7352]: [Runspace6]: PS C:\Users\corywo>> $DebugPreference = 'Continue'
[DBG]: [Process:7352]: [Runspace6]: PS C:\Users\corywo>> Get-AzActivityLogAlert -ResourceGroupName $resourceGroupName -Name $alertName -DefaultProfile $context -Debug
DEBUG: 2:06:00 PM - GetAzureRMContextCommand end processing.
DEBUG: [CmdletBeginProcessing]: Starting command
DEBUG: CmdletBeginProcessing:
DEBUG: CmdletProcessRecordStart:
DEBUG: CmdletGetPipeline:
DEBUG: CmdletBeforeAPICall:
DEBUG: URLCreated:
/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/providers/Microso
DEBUG: ft.Insights/activityLogAlerts/ResourceHealth?api-version=2020-10-01
DEBUG: RequestCreated:
/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/providers/Mic
DEBUG: rosoft.Insights/activityLogAlerts/ResourceHealth?api-version=2020-10-01
DEBUG: HeaderParametersAdded:
DEBUG: ============================ HTTP REQUEST ============================
DEBUG:
DEBUG: HTTP Method:
DEBUG: GET
DEBUG:
DEBUG: Absolute Uri:
DEBUG:
https://management.azure.com/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/p
DEBUG: roviders/Microsoft.Insights/activityLogAlerts/ResourceHealth?api-version=2020-10-01
DEBUG:
DEBUG: Headers:
DEBUG: x-ms-unique-id : 59,60,61,62,63,64,65,66
DEBUG: x-ms-client-request-id : 31b03334-0269-45db-bafc-7881cb1402fb
DEBUG: CommandName : &
DEBUG: FullCommandName : Get-AzActivityLogAlert_Get
DEBUG: ParameterSetName : __AllParameterSets
DEBUG: User-Agent : AzurePowershell/v0.0.0,PSVersion/v3.0.0.0,Az.ActivityLogAlert/4.3.0
DEBUG:
DEBUG: Body:
DEBUG:
DEBUG:
DEBUG:
DEBUG: BeforeCall:
DEBUG: ============================ HTTP RESPONSE ============================
DEBUG:
DEBUG: Status Code:
DEBUG: OK
DEBUG:
DEBUG: Headers:
DEBUG: Pragma : no-cache
DEBUG: Vary : Accept-Encoding
DEBUG: x-ms-request-id : 798f8fe0-da86-4dee-91d0-8e341a05f6e1,798f8fe0-da86-4dee-91d0-8e341a05f6e1
DEBUG: api-supported-versions : 2017-03-01-preview, 2017-04-01, 2020-10-01
DEBUG: Strict-Transport-Security : max-age=31536000; includeSubDomains
DEBUG: Arr-Disable-Session-Affinity : true
DEBUG: X-Content-Type-Options : nosniff
DEBUG: x-ms-ratelimit-remaining-subscription-reads: 11997
DEBUG: x-ms-correlation-request-id : 798f8fe0-da86-4dee-91d0-8e341a05f6e1
DEBUG: x-ms-routing-request-id : CENTRALUS:20221215T200558Z:798f8fe0-da86-4dee-91d0-8e341a05f6e1
DEBUG: Cache-Control : no-cache
DEBUG: Date : Thu, 15 Dec 2022 20:05:58 GMT
DEBUG: Server : Microsoft-IIS/10.0
DEBUG: X-AspNet-Version : 4.0.30319
DEBUG: X-Powered-By : ASP.NET
DEBUG:
DEBUG: Body:
DEBUG: {
DEBUG: "id":
"/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/providers/microsoft.
DEBUG: insights/activityLogAlerts/ResourceHealth",
DEBUG: "name": "ResourceHealth",
DEBUG: "type": "Microsoft.Insights/ActivityLogAlerts",
DEBUG: "location": "Global",
DEBUG: "properties": {
DEBUG: "scopes": [
DEBUG: "/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901"
DEBUG: ],
DEBUG: "condition": {
DEBUG: "allOf": [
DEBUG: {
DEBUG: "field": "category",
DEBUG: "equals": "ResourceHealth"
DEBUG: },
DEBUG: {
DEBUG: "field": "status",
DEBUG: "equals": "Active"
DEBUG: },
DEBUG: {
DEBUG: "field": "resourceType",
DEBUG: "equals": "microsoft.compute/virtualmachines"
DEBUG: },
DEBUG: {
DEBUG: "anyOf": [
DEBUG: {
DEBUG: "field": "properties.cause",
DEBUG: "equals": "PlatformInitiated"
DEBUG: },
DEBUG: {
DEBUG: "field": "properties.cause",
DEBUG: "equals": "Unknown"
DEBUG: }
DEBUG: ]
DEBUG: },
DEBUG: {
DEBUG: "anyOf": [
DEBUG: {
DEBUG: "field": "properties.currentHealthStatus",
DEBUG: "equals": "Degraded"
DEBUG: },
DEBUG: {
DEBUG: "field": "properties.currentHealthStatus",
DEBUG: "equals": "Unavailable"
DEBUG: }
DEBUG: ]
DEBUG: }
DEBUG: ]
DEBUG: },
DEBUG: "actions": {
DEBUG: "actionGroups": [
DEBUG: {
DEBUG: "actionGroupId":
"/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/p
DEBUG: roviders/microsoft.insights/actionGroups/TechnicalTeamsAzureNotifications"
DEBUG: }
DEBUG: ]
DEBUG: },
DEBUG: "enabled": false
DEBUG: }
DEBUG: }
DEBUG:
DEBUG:
DEBUG: ResponseCreated:
DEBUG: BeforeResponseDispatch:

ActionGroup : {{
"actionGroupId": "/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealt
hAlerts-rg/providers/microsoft.insights/actionGroups/TechnicalTeamsAzureNotifications"
}}
ConditionAllOf : {{
"field": "category",
"equals": "ResourceHealth"
}, {
"field": "status",
"equals": "Active"
}, {
"field": "resourceType",
"equals": "microsoft.compute/virtualmachines"
}, {
"anyOf": [
{
"field": "properties.cause",
"equals": "PlatformInitiated"
},
{
"field": "properties.cause",
"equals": "Unknown"
}
]
}...}
Description :
Enabled : False
Id : /subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901/resourceGroups/ServiceHealthAlerts-rg/providers
/microsoft.insights/activityLogAlerts/ResourceHealth
Location : Global
Name : ResourceHealth
ResourceGroupName : ServiceHealthAlerts-rg
Scope : {/subscriptions/a127febc-f62e-4bc6-bec1-1dec6bec9901}
Tag : {
}
Type : Microsoft.Insights/ActivityLogAlerts

DEBUG: Finally:
DEBUG: CmdletAfterAPICall:
DEBUG: [CmdletProcessRecordAsyncEnd]: Finish HTTP process
DEBUG: CmdletProcessRecordAsyncEnd:
DEBUG: CmdletProcessRecordEnd:
DEBUG: AzureQoSEvent: Module: Az.Monitor:4.3.0; CommandName: Get-AzActivityLogAlert; PSVersion: 3.0.0.0; IsSuccess:
True;
DEBUG: Duration: 00:00:00.1030225
```

### Environment data

```PowerShell
Name Value
---- -----
PSVersion 5.1.20348.859
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.20348.859
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
```

### Module versions

```PowerShell
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 2.10.4 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzContext, Clear-AzDefault...}
Script 4.3.0 Az.Monitor {Add-AzLogProfile, Add-AzMetricAlertRule, Add-AzMetricAlertRuleV2, Add-AzWebtestAler...
Script 6.5.0 Az.Resources {Export-AzResourceGroup, Export-AzTemplateSpec, Get-AzDenyAssignment, Get-AzDeployme...
```

### Error output

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with Get-AzActivityLogAlert and New-AzActivityLogAlert in parallel Start-ThreadJob runspaces, using the supplied DefaultProfile and debug output. Trace how these cmdlets resolve the profile and AzContext alias; done means both use the requested tenant and subscription rather than another runspace's context.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, powershell
Domain
cli, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.