question

JohnKuntz-6979 avatar image
0 Votes"
JohnKuntz-6979 asked VasudevReddyGanganna-9013 commented

Deleting Files in Azure File Share older than X Days

I'm currently trying to write a powershell script to delete files older than 14 days from our Azure file shares. The problem I'm running into is, the files don't return a last modified date, except in the actual UI in portal.azure.com. I have created a context using my access keys, but this still doesn't seem to work. Below is the code I'm using (sanitized for privacy) so far.

$StorageAccountKey = "<ACCESS KEY REMOVED>"
$Subscription = '<SUBSCRIPTION REMOVED>'
$Date = (Get-Date).AddDays(-14) ## Threshold of 2 weeks

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery
Import-Module Az
Connect-AzAccount -Subscription $Subscription
$Context = New-AzStorageContext -StorageAccountName "<STORAGE ACCOUNT REMOVED>" -StorageAccountKey $StorageAccountKey
$TestShare = Get-AZStorageShare -Context $Context -Name "test"
$ProdShare = Get-AZStorageShare -Context $Context -Name "production"
$TestFolders = Get-AZStorageFile -ShareName $TestShare.Name -Context $Context
$TestFiles = @()
@($TestFolders).Foreach(
{
$TestFiles += Get-AZStorageFile -ShareName $TestShare.Name -Context $Context -Path $_.Name | Get-AZStorageFile | Select Name,LastModifiedDate
}
)

$ProdFolders = Get-AZStorageFile -ShareName $ProdShare.Name -Context $Context
$ProdFiles = @()
@($ProdFolders).Foreach(
{
$ProdFiles += Get-AZStorageFile -ShareName $ProdShare.Name -Context $Context -Path $_.Name | Get-AZStorageFile | Select *
}
)

azure-storage-accounts
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@JohnKuntz-6979
There is a similar question with some CLI sample script here which you might find helpful. I am investigating to see how this might be done with PowerShell and will get back to you.


0 Votes 0 ·

I'm not seeing anything in properties that would give me the date.

116831-image.png


0 Votes 0 ·
image.png (41.8 KiB)

1 Answer

deherman-MSFT avatar image
0 Votes"
deherman-MSFT answered VasudevReddyGanganna-9013 commented

@JohnKuntz-6979

Apologies for the delayed response. For “Get-AzStorageFile”:
• Without “-Path”: It will list file/FileDir which directly in a share/fileDir. To list recursively in a share/FileDir, you need to call it recursively.
• With “-Path”: It will get an instance of a directory or file (with file properties like LMT) in the specified path.

The following script will list files/FileDir recursively in a file share, and delete the files older than 14 days.

 $ctx = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $key
 $shareName = <shareName>
    
 $DirIndex = 0
 $dirsToList = New-Object System.Collections.Generic.List[System.Object]
    
 # Get share root Dir
 $shareroot = Get-AzStorageFile -ShareName $shareName -Path . -context $ctx 
 $dirsToList += $shareroot 
    
 # List files recursively and remove file older than 14 days 
 While ($dirsToList.Count -gt $DirIndex)
 {
     $dir = $dirsToList[$DirIndex]
     $DirIndex ++
     $fileListItems = $dir | Get-AzStorageFile
     $dirsListOut = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFileDirectory"}
     $dirsToList += $dirsListOut
     $files = $fileListItems | where {$_.GetType().Name -eq "AzureStorageFile"}
    
     foreach($file in $files)
     {
         # Fetch Attributes of each file and output
         $task = $file.CloudFile.FetchAttributesAsync()
         $task.Wait()
    
         # remove file if it's older than 14 days.
         if ($file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-14))
         {
             ## print the file LMT
             # $file | Select @{ Name = "Uri"; Expression = { $_.CloudFile.SnapshotQualifiedUri} }, @{ Name = "LastModified"; Expression = { $_.CloudFile.Properties.LastModified } } 
    
             # remove file
             $file | Remove-AzStorageFile
         }
     }
     #Debug log
     # Write-Host  $DirIndex $dirsToList.Length  $dir.CloudFileDirectory.SnapshotQualifiedUri.ToString() 
 } 

Hope this helps! Let us know if you run into issues or have further questions.



Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Deherman, i also have similar requirement what John has. here is my requirement. i am maintaining the single fileshare in storage account. one application(let say A) upload the file to file share in one folder and another application(let say B) will consume the file with in 5 mins( another application has ADF connection). now i have to generate an alert if application B could not consume the file with in 5 mins then send an alert to application A team. for this i have used some of the part of your code written in this portal but it is not throwing any error but went to infinite loop. i am trying this cloud powershell.
i am not sure why it went to infinite loop. could you suggest on this

any one has a powershell script already to satisfy my requirement, please share it. thanks for your help
Thanks,
Vasu

0 Votes 0 ·