如何使用PowerShell删除所有Azure资源

Ind*_*go8 5 powershell azure

我需要从所有资源中清空我的Azure帐户,并且在门户网站中单独删除太多.寻找PowerShell脚本来做到这一点.谢谢.

Edw*_*xon 9

由于Azure中的资源被分组为资源组(RG),这可能是最简单的方法.使用这些cmdlet执行此操作.

检索完所有RG后,可以使用|管道输出结果 删除cmdlet的字符,并使用ForEach循环遍历它们.试一试,这是最好的学习方式,而不是简单地在这里寻求解决方案.

或者,如果您不想使用powershell,只需从门户网站删除您的RG即可.我认为你认为这需要太长时间,因为你正在查看个人资源而不是他们的RG,但如果你确实拥有那么多RG,那么编写脚本是最好的.


Man*_*ava 5

#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
}
Run Code Online (Sandbox Code Playgroud)


Fra*_*her 5

这样的脚本可能非常有害……但也非常有用。

我已经创建了一个小脚本并在其上添加了很少的安全性以避免破坏错误的订阅。

该脚本要求您登录,然后列出该帐户有权访问的所有订阅。一旦指定了哪一个,它将列出按资源组分组的所有资源。然后作为最后的警告,它需要最后一次验证,然后再核对一切。

# 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
}
Run Code Online (Sandbox Code Playgroud)

代码可在GitHub 上获得:AzurePowerTools