使用PowerShell查找和刪除所有Azure訂閱中的空資源組

來源: 51CTO
作者:wuyvzhang
時間:2021-04-21
17497
在很多情況下,當(dāng)我們使用完Azure資源組之后總是忘記刪除它們,所以我創(chuàng)建了下面的PowerShell腳本來幫助清理它們。它適用于所有的賬戶訂閱。如果您的賬戶中有很多人在管理不同的Azure Resource但其并而不是自己清理空的資源組,那么這個腳本就很有用

在很多情況下,當(dāng)我們使用完Azure資源組之后總是忘記刪除它們,所以我創(chuàng)建了下面的PowerShell腳本來幫助清理它們。它適用于所有的賬戶訂閱。如果您的賬戶中有很多人在管理不同的Azure Resource但其并而不是自己清理空的資源組,那么這個腳本就很有用

#Log in to Azure account
Login-AzureRmAccount #Global
Login-AzureRmAccount -Environment AzureChinaCloud #21V

#Get list of Azure Subscription ID's
$Subs = (get-AzureRMSubscription).ID
#Loop through the subscriptions to find all empty Resource Groups and store them in $EmptyRGs
ForEach ($sub in $Subs) {
Select-AzureRmSubscription -SubscriptionId $Sub
$AllRGs = (Get-AzureRmResourceGroup).ResourceGroupName
$UsedRGs = (Get-AzureRMResource | Group-Object ResourceGroupName).Name
$EmptyRGs = $AllRGs | Where-Object {$_ -notin $UsedRGs}

#Loop through the empty Resorce Groups asking if you would like to delete them. And then deletes them.
foreach ($EmptyRG in $EmptyRGs){
$Confirmation = Read-Host "Would you like to delete $EmptyRG '(Y)es' or '(N)o'"
IF ($Confirmation -eq "y" -or $Confirmation -eq "Yes"){
Write-Host "Deleting" $EmptyRG "Resource Group"
Remove-AzureRmResourceGroup -Name $EmptyRG -Force
}
}
}

登錄后復(fù)制

立即登錄,閱讀全文
版權(quán)說明:
本文內(nèi)容來自于51CTO,本站不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。文章內(nèi)容系作者個人觀點,不代表快出海對觀點贊同或支持。如有侵權(quán),請聯(lián)系管理員(zzx@kchuhai.com)刪除!
優(yōu)質(zhì)服務(wù)商推薦
更多