Private/Utils/Get-ScopePath.ps1
|
function Get-ScopePath { <# .SYNOPSIS Construct Azure resource scope path from Management Group or Subscription ID. .DESCRIPTION Builds proper Azure scope path format for policy operations. Falls back to current subscription context if neither parameter is provided. .PARAMETER ManagementGroupId Management Group ID to construct scope for. .PARAMETER SubscriptionId Subscription ID to construct scope for. .EXAMPLE Get-ScopePath -ManagementGroupId "MyMG" Returns: "/providers/Microsoft.Management/managementGroups/MyMG" .EXAMPLE Get-ScopePath -SubscriptionId "12345678-1234-1234-1234-123456789abc" Returns: "/subscriptions/12345678-1234-1234-1234-123456789abc" .OUTPUTS String - Azure scope path #> [CmdletBinding()] param( [string]$ManagementGroupId, [string]$SubscriptionId ) if ($ManagementGroupId) { return "/providers/Microsoft.Management/managementGroups/$ManagementGroupId" } if ($SubscriptionId) { return "/subscriptions/$SubscriptionId" } # Fallback to current context $ctx = Get-AzContext if (-not $ctx.Subscription) { throw "No active subscription in context. Specify -SubscriptionId or -ManagementGroupId." } return "/subscriptions/$($ctx.Subscription.Id)" } |