温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用PowerShell查找和删除所有Azure订阅中的空资源组

发布时间:2020-06-02 11:01:43 来源:网络 阅读:682 作者:wuyvzhang 栏目:云计算


在很多情况下,当我们使用完Azure资源组之后总是忘记删除它们,所以我创建了下面的PowerShell脚本来帮助清理它们。它适用于所有的账户订阅。如果您的账户中有很多人在管理不同的Azure Resource但其并而不是自己清理空的资源组,那么这个脚本就很有用使用PowerShell查找和删除所有Azure订阅中的空资源组

#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
}
}
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI