如何使用 PowerShell 检查 Azure 资源组是否为空?

powershellmicrosoft technologiessoftware & coding

要检查资源组是否为空,我们需要检查该资源组是否包含任何资源。

在本例中,我们有一个名为 TestRG 的资源组,我们需要检查它是否为空。

示例

$resources = Get-AzResource -ResourceGroupName TestRG
if($resources){"Resource group is not empty"}
else{"Resource group is empty"}

输出

Resource group is empty

要检查特定订阅中的资源组是否为空,请使用以下代码。

输出

Connect-AZAccount
Set-AzContext -SubscriptionName 'Your Subscription Name'
$rgs = Get-AzResourceGroup

Write-Output "Empty Resource Groups"
foreach($rg in $rgs.ResourceGroupName){
    $resources = Get-AzResource -ResourceGroupName $rg
    if(!($resources)){ $rg }
}

相关文章