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

如何使用powershell删除所有Azure资源,powershell,azure,Powershell,Azure,我需要从所有资源中清空我的Azure帐户,门户中有太多的资源需要单独删除。正在寻找一个powershell脚本来执行此操作。谢谢。由于Azure中的资源被分组为资源组(RG),这可能是最简单的方法。使用这些cmdlet可以执行此操作 检索完所有RG后,可以将带有|字符的结果通过管道传输到Remove cmdlet,并使用ForEach循环对其进行迭代。试一试,这是最好的学习方式,而不是简单地在这里寻求解决方案 或者,如果不想使用powershell,只需从门户中删除RGs即可。我假设您认

我需要从所有资源中清空我的Azure帐户,门户中有太多的资源需要单独删除。正在寻找一个powershell脚本来执行此操作。谢谢。

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

检索完所有RG后,可以将带有|字符的结果通过管道传输到Remove cmdlet,并使用ForEach循环对其进行迭代。试一试,这是最好的学习方式,而不是简单地在这里寻求解决方案


或者,如果不想使用powershell,只需从门户中删除RGs即可。我假设您认为这会花费太长时间,因为您查看的是单个资源,而不是它们的RG,但是如果您确实有那么多RG,那么脚本编写是最好的。

这样的脚本可能会非常有害。。。而且非常有用

#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
}
我创建了一个小脚本,并在其上添加了一些安全性,以避免导致错误订阅

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

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

该代码可在

上获得,我知道该请求是针对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
#/bin/bash
#注意:请小心,因为中的此代码旨在删除订阅中的所有资源。使用风险自负。
#设置正确的订阅
az帐户集-s“”
#获取所有资源组并循环删除它们
对于`az组列表-o tsv--query[*].name`中的rg_名称;做
echo删除${rg_name}
az组删除-n${rg_name}--是--否,请稍候
完成

切换到Azure中的poweshell shell并运行此命令以擦除所有内容

Get-AzureRmResourceGroup | Remove-AzureRmResourceGroup -verbose -Force
为新Azure PowerShell模块Az更新
要从Azure资源组中删除所有资源,但保留该组及其设置,请执行以下操作:

Get-AzResource -ResourceGroupName $ResourceGroupName | Remove-AzResource -Force

下面的命令可用于删除所有资源

Get-AzResource | Remove-AzResource -force
这是一行(请使用az登录登录)


你试过什么吗?你能展示一些代码吗?任何错误等?我是一个完全的PS初学者,不知道从哪里开始。不一定是PS,只是想让一切都消失…它一定是PowerShell吗?您不能通过门户删除订阅吗?删除订阅将杀死订阅中的所有资源。嘿!我在用你的剧本,谢谢你!直到它停止工作并出现以下错误:
Remove AzureRmResourceGroup:无法验证参数“Name”的参数。参数为null或为空。请提供一个不为null或空的参数,然后重试该命令。
知道它为什么会停止工作吗?可能是您的azure资源组为null是否有方法删除所选资源组,如;获取RG名称数组并删除一次。我不想删除所有的RG。如果有办法的话,请告诉我,如果你能在这里展示,我将不胜感激。我想你想要的是:$rgNames=“name1”,“name2”foreach($rgNames中的name){Get AzureRmResourceGroup | Where ResourceGroupName-like$name}。这将获取名称以该变量值开头的所有RG。然后,您可以执行与答案相同的操作,将结果传递给remove cmdlet以将其删除。
Get-AzResource | Remove-AzResource -force
az group list | ConvertFrom-Json | % {az group delete --name $_.name -y}