0

I have PowerShell script which downloads the file with current date as filename from Azure blob. But How to get the log file of the process and how to remove the file which is downloaded from Azure blob through script. Could someone help me in this, Please.

Example.

app_09102021.txt
app_10102021.txt
app_11102021.txt

Below is the script.

$container_name = '$XXX'
$destination_path = 'D:\test'
$Ctx = New-AzStorageContext $ZZZZ -StorageAccountKey $CCVCVCVCV
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Listblobs = 'app_{0:ddMMyyyy}.txt' -f (Get-Date)
# Just download that blob
Get-AzStorageBlobContent -Context $Ctx -Container $container_name -Blob $Listblobs -Destination $destination_path
  • 1
    Hi @AjayKumarGhose-MT Thanks a lot, it works in awesome way. Tones of Thanks. Really helpfull, and it strucks my mind several days, Thanks a lot dude. – Gopi Arumugam Jan 05 '22 at 07:28

1 Answers1

0

I have tested in my environment to download the blobs and delete by using below cmd and its successfully downloaded and got removed from Azure as well .

$container_name = 'testaj'
$destination_path = 'C:\Users\Desktop\test'
$Ctx = New-AzStorageContext 'accountname' -StorageAccountKey 'accountkeyrA=='
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Listblobs ='app_{0:ddMMyyyy}.txt' -f (Get-Date)

$blob = Get-AzStorageBlobContent -Context $Ctx -Container $container_name -Blob $Listblobs -Destination $destination_path -ErrorAction SilentlyContinue
if($blob  -ne $Null)
{
Write-Host ("The File $Listblobs has been downloaded to $destination_path")
Write-Host ("Proceeding to delete the downloaded file from $container_name in Azure!!!")

Remove-AzStorageBlob -Container $container_name -Blob $Listblobs -Context $Ctx
Write-Host ("deleted file from $Listblobs in Azure!!!") 
}
else{
write-Host ("The file does not exit")
}

Here is the Output for downloaded and deleted file from blob: enter image description here

If the file got deleted then it will be something like below: enter image description here

For more information please refer this MS DOC: Monitoring Azure Blob Storage

UPDATE:

I am looking for the solution to search & match the date with today's date in filename, not sorting by last modified date/time.

Tried with the below code to download all the blobs with todays date for e.g abc_04012022,app_04012022 and delete them from Azure

PS Script :-

$container_name = 'test'
$destination_path = 'C:\Users\Desktop\test'
$Ctx = New-AzStorageContext 'accountname' -StorageAccountKey 'accountkey=='
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$latestBlob = Get-Date -UFormat '%d%m%Y'

$bloblist = Get-AzStorageBlob -Container $container_name -Context $Ctx |select -Property Name

foreach($item in $bloblist){
            
            if($item.Name -match $latestBlob){
                    Write-Output "Here is the blobs"
                    $blob = Get-AzStorageBlobContent -Context $Ctx -Container $container_name -Blob $item.Name -Destination $destination_path -ErrorAction SilentlyContinue
                    if($blob -ne $Null)
                    {
                        Write-Output ("The File $item.Name has been downloaded to $destination_path")
                        Write-Output ("Proceeding to delete the downloaded file from $container_name in Azure!!!")
                        
                        Remove-AzStorageBlob -Container $container_name -Blob $item.Name -Context $Ctx

                         Write-Output ("deleted file from $item.Name in Azure!!!")
                    }
                    else{
                        write-Output ("The file does not exit")
                     }
            }
}

Screenshot for reference: enter image description here enter image description here

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15
  • Thanks lot @AjayKumarGose-MT , I am also looking for solution to download data from the blob which matches today's(current) date with filename irrespective of the letters before it. For example file in blob name is 'cqb01012022.txt; abc_01012022.txt; xyz01012022.txt'...the script should download all these three files matching with today's date. Looking for help. – Gopi Arumugam Jan 01 '22 at 11:15
  • Np @GopiArumugam , Please refer this [SO THREAD](https://stackoverflow.com/questions/44777851/is-it-possible-to-list-azure-blobs-where-last-modified-date-some-date) 2nd solution . – AjayKumarGhose Jan 02 '22 at 09:25
  • Hi @AjayKumarGhose-MT, I am looking for the solution to search & match the date with today's date in filename, not sorting by last modified date/time. Will be great full for your help in this. – Gopi Arumugam Jan 04 '22 at 04:14
  • Hello @GopiArumugam , I have update the answer in UPDATE section. Could you please refer this. Thank you:) – AjayKumarGhose Jan 04 '22 at 06:42
  • Thanks a lot @AjayKumarGhose-MT, It works in awesome way. Tones of Thanks. No words to express my happiness. – Gopi Arumugam Jan 05 '22 at 07:30
  • You are welcome @GopiArumugam , HTH. – AjayKumarGhose Jan 05 '22 at 07:33
  • 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:35