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

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

在很多情況下,當我們使用完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
}
}
}

登錄后復制

立即登錄,閱讀全文
版權說明:
本文內容來自于51CTO,本站不擁有所有權,不承擔相關法律責任。文章內容系作者個人觀點,不代表快出海對觀點贊同或支持。如有侵權,請聯系管理員(zzx@kchuhai.com)刪除!
優(yōu)質服務商推薦
更多
掃碼登錄
打開掃一掃, 關注公眾號后即可登錄/注冊
加載中
二維碼已失效 請重試
刷新
賬號登錄/注冊
個人VIP
小程序
快出海小程序
公眾號
快出海公眾號
商務合作
商務合作
投稿采訪
投稿采訪
出海管家
出海管家