Reputation: 75
I Have written this code. How can i download only latest file?
$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
}
Upvotes: 1
Views: 5107
Reputation: 136356
Try this code:
$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
What the code above does is lists the blobs and then sorts them in descending order based on the last modified date. The 1st element in the array will be the latest blob. It then downloads this blob.
Upvotes: 5