diff --git a/Public/Get-AzRetirementRecommendation.ps1 b/Public/Get-AzRetirementRecommendation.ps1 index 29bddf7..0f4874d 100644 --- a/Public/Get-AzRetirementRecommendation.ps1 +++ b/Public/Get-AzRetirementRecommendation.ps1 @@ -169,9 +169,45 @@ Gets recommendations using the REST API method $recommendations = if ($SubscriptionId) { # Query specific subscriptions + # Store the current context to restore later + $originalContext = Get-AzContext + foreach ($subId in $SubscriptionId) { Write-Verbose "Querying subscription via Az.Advisor: $subId" - Get-AzAdvisorRecommendation -Filter $filter | Where-Object $subcategoryFilter + + # Set context to the specific subscription + try { + $context = Set-AzContext -SubscriptionId $subId -ErrorAction Stop + + # Verify that the context was actually set to the intended subscription + if (-not $context -or -not $context.Subscription -or $context.Subscription.Id -ne $subId) { + Write-Warning "Azure context for subscription $($subId) could not be verified. Skipping recommendation query for this subscription." + continue + } + + } + catch { + Write-Warning "Failed to set Azure context for subscription $($subId): $_" + continue + } + + # Query Advisor recommendations for the current subscription + try { + Get-AzAdvisorRecommendation -Filter $filter | Where-Object $subcategoryFilter + } + catch { + Write-Warning "Failed to query Advisor recommendations for subscription $($subId): $_" + } + } + + # Restore the original context + if ($originalContext) { + try { + $null = Set-AzContext -Context $originalContext -ErrorAction Stop + } + catch { + Write-Warning "Failed to restore original Azure context: $_" + } } } else { diff --git a/Tests/AzRetirementMonitor.Tests.ps1 b/Tests/AzRetirementMonitor.Tests.ps1 index 77efa1a..878415c 100644 --- a/Tests/AzRetirementMonitor.Tests.ps1 +++ b/Tests/AzRetirementMonitor.Tests.ps1 @@ -201,6 +201,62 @@ Describe "Get-AzRetirementRecommendation" { } } +Describe "Get-AzRetirementRecommendation Context Switching Logic" { + It "Should have context management code in the function" { + # Verify that the function source contains the necessary context management logic + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check for Get-AzContext call to save original context + $functionDef | Should -Match 'Get-AzContext' + + # Check for Set-AzContext with SubscriptionId parameter + $functionDef | Should -Match 'Set-AzContext\s+-SubscriptionId' + + # Check for context verification logic + $functionDef | Should -Match 'could not be verified' + + # Check for context restoration with Context parameter + $functionDef | Should -Match 'Set-AzContext\s+-Context' + } + + It "Should have error handling for Set-AzContext failures" { + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check for try-catch around Set-AzContext + $functionDef | Should -Match 'try\s*\{[^}]*Set-AzContext' + $functionDef | Should -Match 'Failed to set Azure context for subscription' + } + + It "Should have error handling for context restoration" { + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check for error handling around context restoration + $functionDef | Should -Match 'Failed to restore original Azure context' + } + + It "Should have error handling for Get-AzAdvisorRecommendation failures" { + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check for error handling around Get-AzAdvisorRecommendation + $functionDef | Should -Match 'Failed to query Advisor recommendations' + } + + It "Should verify subscription context after setting it" { + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check that the function verifies the context was set correctly + $functionDef | Should -Match 'context\.Subscription\.Id' + $functionDef | Should -Match '\$subId' + } + + It "Should use continue statement to skip failed subscriptions" { + $functionDef = (Get-Command Get-AzRetirementRecommendation).Definition + + # Check for continue statements in error handling + $functionDef | Should -Match 'continue' + } +} + Describe "Get-AzRetirementMetadataItem" { It "Should have no parameters" { $cmd = Get-Command Get-AzRetirementMetadataItem