powershell - 如何通过 Get-AzureStorageBlobContent 仅下载最新文件

标签 powershell azure

我写了这段代码。我怎样才能只下载最新的文件?

$container_name = 'packageitems'
$destination_path = 'C:\pstest'
$connection_string = 'DefaultEndpointsProtocol=https;AccountName=[REPLACEWITHACCOUNTNAME];AccountKey=[REPLACEWITHACCOUNTKEY]'

$storage_account = New-AzureStorageContext -ConnectionString $connection_string

$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account

foreach ($blob in $blobs)
{
    New-Item -ItemType Directory -Force -Path $destination_path

    Get-AzureStorageBlobContent `
    -Container $container_name -Blob $blob.Name -Destination $destination_path `
    -Context $storage_account

}

最佳答案

试试这个代码:

$container_name = 'packageitems'
$destination_path = 'C:\pstest'
$connection_string = 'DefaultEndpointsProtocol=https;AccountName=[REPLACEWITHACCOUNTNAME];AccountKey=[REPLACEWITHACCOUNTKEY]'
$storage_account = New-AzureStorageContext -ConnectionString $connection_string
# Get the blobs list and then sort them by last modified date descending
$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account  | sort @{expression="LastModified";Descending=$true}
# First blob in that list would be the last modified.
$latestBlob = $blobs[0]
# Just download that blob
Get-AzureStorageBlobContent `
    -Container $container_name -Blob $latestBlob.Name -Destination $destination_path `
    -Context $storage_account

上面的代码的作用是列出 blob,然后根据上次修改日期按降序对它们进行排序。数组中的第一个元素将是最新的 blob。然后它会下载这个 blob。

关于powershell - 如何通过 Get-AzureStorageBlobContent 仅下载最新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43141949/

相关文章:

powershell - Azure powershell 插槽特定的应用程序设置

azure - Azure 数据工厂管道登录特定 URL 的技术是什么

postgresql - Postgres pg_dump 到 Azure Blob 存储

.net - 具有 Angular 前端和 .NET 后端的 Azure SSO 的 SAML 配置

powershell - Powershell带引号问题的文件路径

Powershell New-ADUser 错误处理密码复杂度(ActiveDirectory 模块)

powershell - 在不展开数组的情况下将参数分配给 cmdlet

powershell - 适用于 PowerShell 的 AWS 工具 - Get-EC2Vpc 未出现在 PowerGUI 中

azure - 是否可以使用 Azure AD Graph Api 仅列出某个组中的组织联系人?

Azure Function App - 尝试从 v2 工具发布到 v1 函数应用程序