0

I need Powershell script to download file with current date as filename from Azure blob,like for example

app_09102021.txt
app_10102021.txt
app_11102021.txt

if i run the script on 11-10-2021, the file app_11102021.txt should get download. Could you please help.

I tried the below code, but error at 5th line $latestblob

$container_name = 'XXXX'
$destination_path = 'D:\test\trial'
$Ctx = New-AzStorageContext $XXXX -StorageAccountKey $YYYYYYY
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#$latestBlob = Get-AzStorageBlob  -context $Ctx -Container trial|@{$app_$((Get-Date).tostring("yyyyMMdd")).txt}
# Just download that blob
Get-AzStorageBlobContent -Context $Ctx -Container $container_name -Blob $latestBlob.Name -Destination $destination_path 
Ansuman Bal
  • 9,705
  • 2
  • 10
  • 27
  • Simply construct the filename from today's date like `$latestBlob = 'app_{0:ddMMyyyy}.txt' -f (Get-Date)` and then use as `-Blob $latestBlob` – Theo Dec 20 '21 at 09:58
  • Hi, thanks a lot, it works. Awesome. Also, I have to download the log file and delete the file from the blob once downloaded. need help in that, Please – Gopi Arumugam Dec 20 '21 at 11:56
  • So.. that's a new question.. – Theo Dec 20 '21 at 12:15
  • Raised new question for that, Please help. https://stackoverflow.com/q/70422675/17721455 – Gopi Arumugam Dec 20 '21 at 13:39
  • What should we do if we want match files with any name like 'app_21122021,bgv_21122021,cxb_21122021, cxb_22122021' so when run the script it has to download all 'app_21122021,bgv_21122021,cxb_21122021' irrespective of the name behind the date. @Theo – Gopi Arumugam Dec 21 '21 at 06:42
  • You will need to do that in a loop, as the `-Blob` parameter of `Get-AzStorageBlobContent` can only take one blob name at a time.. – Theo Dec 21 '21 at 15:00
  • Ya @theo, but the problem here is prefix name, where the date is same, but the Prefix is different. I tried using regex, but can't able to get. irrespective of the name behind the date, it has to filter the file which matches with the current date. i tried doing this '$Listblobs = '"{$_}"{0:ddMMyyyy}.txt' -f (Get-Date)' – Gopi Arumugam Dec 22 '21 at 03:54
  • @Wh1T3h4Ck5 Could you please help. What should we do if we want match files with any name like 'app_21122021,bgv_21122021,cxb_21122021, cxb_22122021' so when run the script it has to download all 'app_21122021,bgv_21122021,cxb_21122021'. – Gopi Arumugam Dec 23 '21 at 08:08
  • If the answer was helpful, Please [Accept it as an Answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), so that others who encounter the same issue can find this solution and fix their problem – AjayKumarGhose Jan 05 '22 at 07:50

1 Answers1

0

I need Powershell script to download file with current date as filename from Azure blob

To do that we can use the following cmd as suggested by @Theo

$container_name = 'test'
$destination_path = 'C:\Users\Desktop\test'
$Ctx = New-AzStorageContext 'name' -StorageAccountKey 'accesskey='
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$latestBlob = '*_{0:ddMMyyyy}.txt' -f (Get-Date) 

$bloblist = Get-AzStorageBlob -Container $container_name -Context $Ctx -Blob $latestBlob

Storage blob contains 3 files in my case:

enter image description here

Here is the output of todays current date :

enter image description here

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15