查找未使用的存储帐户Azure ARM [英] Find unused storage account azure ARM

查看:56
本文介绍了查找未使用的存储帐户Azure ARM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的订阅已达到250个存储帐户的上限.存储帐户是使用ARM创建的

I've reached the hard cap of 250 storage accounts for my subscription. Storage accounts were created using ARM

我需要一种方法来查找未使用的存储帐户并将其删除.基本上,我想查找包含90天内未访问过的容器的存储帐户并进行清理.

I need a way to find unused storage accounts and delete them. Basically I want to find storage accounts with containers that have not been accessed in 90 days to and do a clean up.

有没有一种方法可以检查上次访问的时间,或者有更好的方法使用PowerShell或最好是Azure cli进行清理

Is there a way to check last accessed time or a better way to clean up using PowerShell or preferably the azure cli

谢谢

推荐答案

您可以做的是从 LastModified 属性中获取最新的修改后的容器,然后检查此时间戳是否小于当前时间戳日期减去90天.我们需要检查容器级别和Blob级别的LastModified属性.

What you could do is get the most recent modified container from the LastModified property, then check if this timestamp is less than the current date minus 90 days. We would need to check both the container level and blob level LastModified properties.

# Set current context to subscription
Set-AzContext -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

# Go through every storage account in your subscription
foreach ($storageAccount in Get-AzStorageAccount) {
    $storageAccountName = $storageAccount.StorageAccountName
    $resourceGroupName = $storageAccount.ResourceGroupName

    # Get key1 storage account key
    $storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]

    # Create storage account context using above key
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

    # fetch all containers
    $containers = Get-AzStorageContainer -Context $context

    $deleteStorageAccount = $false
    foreach ($container in $containers) {

        # First check if container has been modified
        if ($container.LastModified.DateTime -lt (Get-Date).AddDays(-90)) {
            $deleteStorageAccount = $true
            break
        }

        # Get all blobs from container, including deleted blobs
        $blobs = Get-AzStorageBlob -Container $container.Name -Context $context -IncludeDeleted

        # Then check each blob in container
        foreach ($blob in $blobs) {
            if ($blob.LastModified.DateTime -lt (Get-Date).AddDays(-90)) {
                $deleteStorageAccount = $true
                break
            }
        }
    }

    # If this flag is set, storage account has been acccessed in last 90 days
    if ($deleteStorageAccount) {
        Remove-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -Force -WhatIf
    }
}

由于此操作可能非常有害,因此可以将 Remove-AzStorageAccount -WhatIf 一起运行,以查看要删除哪些存储帐户,然后再将其删除.

Since this action could be extremely harmful, you can run Remove-AzStorageAccount with -WhatIf to see what storage accounts will be deleted before deleting them for real.

这篇关于查找未使用的存储帐户Azure ARM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆