如何通过PowerShell获取Azure Blob版本的保留期详情
获取Azure Blob版本及其保留期的PowerShell解决方案
你当前的脚本仅能列出非最新的Blob版本,但无法直接获取保留期信息。原因是Get-AzStorageBlob返回的对象中,保留期相关的信息需要从Blob版本的属性字段中提取,以下是修正后的脚本:
Connect-AzAccount Set-AzContext -Subscription "xxxxxxxxxxxxxxx" $resourceGroup = "rg-dev-001" $storageAccountName = "stdev" $containerName = "offload" # 获取存储账户上下文 $stoAccount = Get-AzStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup $ctx = $stoAccount.Context # 获取所有非最新的Blob版本(包含已删除的) $blobVersions = Get-AzStorageBlob -Container $containerName -Context $ctx -IncludeVersion -IncludeDeleted | Where-Object { $_.IsLatestVersion -ne $true } # 提取版本详情及保留期相关信息 $versionDetails = $blobVersions | ForEach-Object { [PSCustomObject]@{ BlobName = $_.Name VersionId = $_.VersionId CreationTime = $_.Properties.CreationTime ExpiresOn = $_.Properties.ExpiresOn # 版本自动删除时间 RetentionPeriod = if ($_.Properties.ExpiresOn) { $_.Properties.ExpiresOn - $_.Properties.CreationTime } else { $null } IsDeleted = $_.IsDeleted DeletedTime = $_.Properties.DeletedTime } } # 输出结果 $versionDetails | Format-Table -AutoSize
关键说明:
ExpiresOn属性:如果存储账户启用了Blob版本保留策略,该字段会显示版本将被自动删除的时间;若未设置策略,该值为null,表示版本不会自动过期,需手动删除。RetentionPeriod:通过ExpiresOn与CreationTime的差值计算出该版本的实际保留时长,单位为时间跨度(例如:30.00:00:00表示保留30天)。- 筛选逻辑:
Where-Object { $_.IsLatestVersion -ne $true }确保只获取非最新版本,这些是你可能需要考虑删除的对象。
如果需要导出结果到CSV文件,可在脚本末尾添加:
$versionDetails | Export-Csv -Path "BlobVersionsRetention.csv" -NoTypeInformation -Encoding UTF8
内容的提问来源于stack exchange,提问作者SQLPRODDBA
相关产品推荐
相关产品推荐

