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

來源: 51CTO
作者:wuyvzhang
時間:2021-04-21
17504
在很多情況下,當(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)容系作者個人觀點(diǎn),不代表快出海對觀點(diǎn)贊同或支持。如有侵權(quán),請聯(lián)系管理員(zzx@kchuhai.com)刪除!
優(yōu)質(zhì)服務(wù)商推薦
更多
掃碼登錄
打開掃一掃, 關(guān)注公眾號后即可登錄/注冊
加載中
二維碼已失效 請重試
刷新
賬號登錄/注冊
個人VIP
小程序
快出海小程序
公眾號
快出海公眾號
商務(wù)合作
商務(wù)合作
投稿采訪
投稿采訪
出海管家
出海管家