【问题标题】:Powershell - Azure Storage Accounts - Get Last Authentication Time/DatePowershell - Azure 存储帐户 - 获取上次身份验证时间/日期
【发布时间】:2021-03-23 12:39:11
【问题描述】:

尝试获取未使用的存储帐户列表。我试图走 LastModified 路线,但有几个问题,首先这仅适用于 Blob 存储,其次如果容器是用美元符号命名的(例如 $web)Get-AzStorageBlob 错误。

有人知道实现这一目标的更好方法吗?我在想,如果我可以列出上次对存储帐户进行身份验证的时间,可以给我我想要的东西,但是在尝试时我会画一个空白。

【问题讨论】:

    标签: azure powershell azure-storage-account


    【解决方案1】:

    我一直在使用以下逻辑,并成功满足了类似的要求。

    逻辑:

    您基本上可以遍历每个存储帐户,找到其中的每个容器,根据上次修改日期(降序)对它们进行排序 - 选择最上面的 - 检查它是否超过 90(根据您的要求任意天数) 天。如果是,请继续删除它们。

    片段代码:

    #Setting the AzContext for the required subscription
    Set-AzContext -SubscriptionId "<YOUR SUBSCRIPTION ID>"
    
    #Going through every storage account in the mentioned Subscription
    foreach ($storageAccount in Get-AzStorageAccount) 
    {
    
    #Storing the Account Name and Resource Group name which will be used in the below steps
    $storageAccountName = $storageAccount.StorageAccountName
    $resourceGroupName = $storageAccount.ResourceGroupName
    
    
    # Getting the storage Account Key - it could be any 1 of the key. Taken the first key for instance. 
    $storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
    
    # Create storage account context using above key
    $storagecontext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
    #Gets  all the Storage Container within the Storage Account
    #Sorts them in descending order based on the LastModified
    #Picks the Topmost or most recently modified date
    $lastModified = Get-AzStorageContainer -Context $storagecontext | Sort-Object -Property @{Expression = {$_.LastModified.DateTime}} | Select-Object -Last 1 -ExpandProperty LastModified
    
    # Remove storage account if it is has not been in past 90 days
    
        if ($lastModified.DateTime -lt (Get-Date).AddDays(-90)) 
        {
            Remove-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName -Force
        }
    }
    

    代码引用自thread

    注意:

    Get-AzStorageContainer - 不仅仅针对 Blob 存储。

    回到你的另一个问题: -

    如果容器是带有美元符号的命名(例如 $web),则 Get-AzStorageBlob 会出错。

    这已在更高版本的 Az.Storage 中处理。我建议您升级模块并试一试。这在thread 中已经讨论过了,Az.Storage 的更高版本应该能够处理名为“$web”的容器

    【讨论】:

    • 碰巧我在我的 LastModified 尝试中使用了这个确切的脚本。我发现的脚本问题,这就是为什么我不得不沿着 Blob 路线走的原因,是因为容器上的 Last Modified 不能反映所述容器的内容。如果其中的 Blob 已被修改,则容器上的 LM 不会更新。
    • 您能否确认您使用的是最新的 Az.storage 包?
    • 是的脚本 3.4.0 Az.Storage
    • 对此还有什么想法吗?
    猜你喜欢
    • 2018-10-11
    • 1970-01-01
    • 2020-11-06
    • 2014-02-20
    • 2017-03-01
    • 1970-01-01
    • 2020-02-27
    • 2012-08-24
    • 1970-01-01
    相关资源
    最近更新 更多