查找未使用的存储帐户Azure ARM

编程入门 行业动态 更新时间:2024-10-23 12:30:01
本文介绍了查找未使用的存储帐户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

本文发布于:2023-11-16 22:08:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帐户   Azure   ARM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!