大規模地將 Azure 原則部署至委派的訂用帳戶

身為服務提供者,您可能已將多個客戶租用戶上線至 Azure Lighthouse。 Azure Lighthouse 可讓服務提供者一次在多個租用戶之間執行大規模作業,讓管理工作更有效率。

本主題說明如何在 Azure 原則使用 PowerShell 命令對多個租用戶部署原則定義和原則指派。 在此範例中,原則定義可確保只允許 HTTPS 流量來保護儲存體帳戶。

提示

雖然我們在這個主題中所述的是服務提供者和客戶,但管理多個租用戶的企業可以使用相同的程序。

使用 Azure Resource Graph 在客戶租用戶之間進行查詢

您可以使用 Azure Resource Graph 在您所管理的客戶租用戶中的所有訂閱內進行查詢。 在此範例中,我們要在這些訂閱中找出目前不需要 HTTPS 流量的所有儲存體帳戶。

$MspTenant = "insert your managing tenantId here"

$subs = Get-AzSubscription

$ManagedSubscriptions = Search-AzGraph -Query "ResourceContainers | where type == 'microsoft.resources/subscriptions' | where tenantId != '$($mspTenant)' | project name, subscriptionId, tenantId" -subscription $subs.subscriptionId

Search-AzGraph -Query "Resources | where type =~ 'Microsoft.Storage/storageAccounts' | project name, location, subscriptionId, tenantId, properties.supportsHttpsTrafficOnly" -subscription $ManagedSubscriptions.subscriptionId | convertto-json

在多個客戶租用戶之間部署原則

下列範例顯示如何使用 Azure Resource Manager 範本,在多個客戶租用戶中的委派訂用帳戶之間,部署原則定義和原則指派。 此原則定義需要所有儲存體帳戶才能使用 HTTPS 流量。 它可防止建立任何不符合規範的新儲存體帳戶。 任何沒有設定的現有儲存體帳戶都會標示為不符合規範。

Write-Output "In total, there are $($ManagedSubscriptions.Count) delegated customer subscriptions to be managed"

foreach ($ManagedSub in $ManagedSubscriptions)
{
    Select-AzSubscription -SubscriptionId $ManagedSub.subscriptionId

    New-AzSubscriptionDeployment -Name mgmt `
                     -Location eastus `
                     -TemplateUri "https://raw.githubusercontent.com/Azure/Azure-Lighthouse-samples/master/templates/policy-enforce-https-storage/enforceHttpsStorage.json" `
                     -AsJob
}

注意

雖然您可以對多個租用戶部署原則,但目前無法在這些租用戶中檢視合規性詳細資料以找出不合規範的資源。

驗證原則部署

部署 Azure Resource Manager範本之後,嘗試在其中一個委派的訂用帳戶中,嘗試建立EnableHttpsTrafficOnly設定為false的儲存體帳戶,以確認已成功套用原則定義。 因為原則指派,所以您應該無法建立此儲存體帳戶。

New-AzStorageAccount -ResourceGroupName (New-AzResourceGroup -name policy-test -Location eastus -Force).ResourceGroupName `
                     -Name (get-random) `
                     -Location eastus `
                     -EnableHttpsTrafficOnly $false `
                     -SkuName Standard_LRS `
                     -Verbose                  

清除資源

完成後請移除部署所建立的原則定義和指派。

foreach ($ManagedSub in $ManagedSubscriptions)
{
    select-azsubscription -subscriptionId $ManagedSub.subscriptionId

    Remove-AzSubscriptionDeployment -Name mgmt -AsJob

    $Assignment = Get-AzPolicyAssignment | where-object {$_.Name -like "enforce-https-storage-assignment"}

    if ([string]::IsNullOrEmpty($Assignment))
    {
        Write-Output "Nothing to clean up - we're done"
    }
    else
    {

    Remove-AzPolicyAssignment -Name 'enforce-https-storage-assignment' -Scope "/subscriptions/$($ManagedSub.subscriptionId)" -Verbose

    Write-Output "Deployment has been deleted - we're done"
    }
}

下一步