我需要从所有资源中清空我的Azure帐户,并且在门户网站中单独删除太多了。寻找PowerShell脚本来做到这一点。感谢。
答案 0 :(得分:8)
由于Azure中的资源被分组为资源组(RG),这可能是最简单的方法。使用这些cmdlet执行此操作。
检索完所有RG后,可以使用|管道输出结果删除cmdlet的字符,并使用ForEach循环遍历它们。试一试,这是最好的学习方式,而不是简单地在这里寻求解决方案。
或者,如果您不想使用powershell,只需从门户网站删除您的RG即可。我认为你认为这需要太长时间,因为你正在查看个人资源而不是他们的RG,但如果你确实拥有那么多RG,那么编写脚本是最好的。
答案 1 :(得分:3)
#It will delete all resources without asking any confirmation
Login-AzureRmAccount
$rgName = Get-AzureRmResourceGroup
Foreach($name in $rgName)
{
Write-Host $name.ResourceGroupName
Remove-AzureRmResourceGroup -Name $name.ResourceGroupName -Verbose -Force
}
答案 2 :(得分:3)
这样的剧本可能真的很有害......但也很有用。
我已经创建了一个小脚本并在其上添加了一点安全性,以避免错误订阅。
脚本要求您登录,然后列出此帐户有权访问的所有订阅。指定哪一个后,它将列出按资源组分组的所有资源。然后作为最后的警告,它需要最后一次验证才能解决所有问题。
# Login
Login-AzureRmAccount
# Get a list of all Azure subscript that the user can access
$allSubs = Get-AzureRmSubscription
$allSubs | Sort-Object SubscriptionName | Format-Table -Property SubscriptionName, SubscriptionId, State
$theSub = Read-Host "Enter the subscriptionId you want to clean"
Write-Host "You select the following subscription. (it will be display 15 sec.)" -ForegroundColor Cyan
Get-AzureRmSubscription -SubscriptionId $theSub | Select-AzureRmSubscription
#Get all the resources groups
$allRG = Get-AzureRmResourceGroup
foreach ( $g in $allRG){
Write-Host $g.ResourceGroupName -ForegroundColor Yellow
Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow
$allResources = Find-AzureRmResource -ResourceGroupNameContains $g.ResourceGroupName
if($allResources){
$allResources | Format-Table -Property Name, ResourceName
}
else{
Write-Host "-- empty--`n"
}
Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow
}
$lastValidation = Read-Host "Do you wich to delete ALL the resouces previously listed? (YES/ NO)"
if($lastValidation.ToLower().Equals("yes")){
foreach ( $g in $allRG){
Write-Host "Deleting " $g.ResourceGroupName
Remove-AzureRmResourceGroup -Name $g.ResourceGroupName -Force -WhatIf
}
}
else{
Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}
上找到
答案 3 :(得分:0)
我知道问的是Powershell,但这里是否有人对Azure CLI感兴趣
#!/bin/bash
# NOTE: Be careful as this code in intended to delete ALL Resources in a subscription. Use at your own risk.
# Set The correct Subscription
az account set -s "<Subscription_name / Id>"
# Get All resource groups and loop to delete them
for rg_name in `az group list -o tsv --query [*].name`; do
echo Deleting ${rg_name}
az group delete -n ${rg_name} --yes --no-wait
done
答案 4 :(得分:0)
切换到Azure中的poweshell外壳并运行此命令以擦除所有内容。
Get-AzureRmResourceGroup | Remove-AzureRmResourceGroup -verbose -Force
答案 5 :(得分:0)
# Login
Connect-AzAccount
# Get a list of all Azure subscript that the user can access
$allSubs = Get-azSubscription
$allSubs | Sort-Object SubscriptionName | Format-Table -Property SubscriptionName, SubscriptionId, State
$theSub = Read-Host "Enter the subscriptionId you want to clean"
Write-Host "You select the following subscription. (it will be display 15 sec.)" -ForegroundColor Cyan
Get-azSubscription -SubscriptionId $theSub | Select-azSubscription
#Get all the resources groups
$allRG = Get-azResourceGroup
foreach ( $g in $allRG){
Write-Host $g.ResourceGroupName -ForegroundColor Yellow
Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow
$allResources = Get-azResource -ResourceGroupName $g.ResourceGroupName | FT
if($allResources){
$allResources | Format-Table -Property Name, ResourceName
}
else{
Write-Host "-- empty--`n"
}
Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow
}
$lastValidation = Read-Host "Do you wich to delete ALL the resouces previously listed? (YES/ NO)"
if($lastValidation.ToLower().Equals("yes")){
foreach ( $g in $allRG){
Write-Host "Deleting " $g.ResourceGroupName
Get-AzResourceGroup -Name $g.ResourceGroupName | Remove-AzResourceGroup -Verbose -Force
}
}
else{
Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}
答案 6 :(得分:0)
要从Azure资源组中删除所有资源,但要保留该组的设置,请执行以下操作:
Get-AzResource -ResourceGroupName $ResourceGroupName | Remove-AzResource -Force
答案 7 :(得分:0)
以下命令可用于删除所有资源
Get-AzResource | Remove-AzResource -force
答案 8 :(得分:0)
这是一行(请使用az登录名登录)
az group list | ConvertFrom-Json | % {az group delete --name $_.name -y}
答案 9 :(得分:-1)
带有 Az 模块的简单管道。
Get-AzResourceGroup | Remove-AzResourceGroup -Force