在很多情况下,当我们使用完Azure资源组之后总是忘记删除它们,所以我创建了下面的PowerShell脚本来帮助清理它们。它适用于所有的账户订阅。如果您的账户中有很多人在管理不同的Azure Resource但其并而不是自己清理空的资源组,那么这个脚本就很有用
#LogintoAzureaccount Login-AzureRmAccount#Global Login-AzureRmAccount-EnvironmentAzureChinaCloud#21V #GetlistofAzureSubscriptionID's $Subs=(get-AzureRMSubscription).ID #LoopthroughthesubscriptionstofindallemptyResourceGroupsandstorethemin$EmptyRGs ForEach($subin$Subs){ Select-AzureRmSubscription-SubscriptionId$Sub $AllRGs=(Get-AzureRmResourceGroup).ResourceGroupName $UsedRGs=(Get-AzureRMResource|Group-ObjectResourceGroupName).Name $EmptyRGs=$AllRGs|Where-Object{$_-notin$UsedRGs} #LoopthroughtheemptyResorceGroupsaskingifyouwouldliketodeletethem.Andthendeletesthem. foreach($EmptyRGin$EmptyRGs){ $Confirmation=Read-Host"Wouldyouliketodelete$EmptyRG'(Y)es'or'(N)o'" IF($Confirmation-eq"y"-or$Confirmation-eq"Yes"){ Write-Host"Deleting"$EmptyRG"ResourceGroup" Remove-AzureRmResourceGroup-Name$EmptyRG-Force } } }