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 *
}
)
